Generate Plot Based on Interval Data

How would I generate a plot if I have data from intervals. I'm not sure if 'interval' is the right technical term.
What I mean is I have data like this
[210 320 430 250 110 ...] <-represents distance
[30 50 10 20 40...] <-represents speed limit
For the first 210 metres, the speed limit is 30km/h. For the subsequent 320m, the speed limit is 50km/h, and so on.
It would look like the blue line in this figure.
Is there a simple way to plot such data? Of course, I can set a loop and interpolate the speed at a distance interval of say, 1 metres. Then subsequently, use the plot(distance,speed) function. But would there be a more elegant way?
Subsequently, I would also want to modify the curve. It's impossible for a car to follow the blue speed curve as it requires infinite acceleration/deceleration. I would like to limit the speed curve to the red curve, by specifying a constant acceleration/deceleration rate of e.g. 1m/s^2.
Would there be a simple way to do this as well?
I've thought of a long method, which is to construct the red slope using y=mx+c. Then define which sections the y=mx+c is valid for, and lastly take the minimum of the blue lines and y=mx+c at every interval, say 1 metres. And this would be repeated for every red slope which exists.

 采纳的回答

jonas
jonas 2018-8-9
编辑:jonas 2018-8-9
the stairs function will do it for you. Here is something you can start with:
%%Data
x=[0 210 320 430 250 110]; %NOTE EXTRA 0
x=[cumsum(x)];
y=[30 50 10 20 40 NaN]; %NOTE EXTRA NaN
figure;;hold on
axis([min(x) max(x)+120 0 100])
%%Change to stairstep
[xb,yb]=stairs(x,y)
plot(xb,yb)
%%Adjust x
dy=diff(yb)
SlowDown=find(dy<0);
SpeedUp=find(dy>0);
xb(SlowDown)=xb(SlowDown)-10;
xb(SpeedUp+1)=xb(SpeedUp+1)+10;
set(gca,'xtick',x(1:end-1))
plot(xb,yb,'r')
ytickformat('%g km/h')
xtickformat('%g m')
If you want a constant slope, then you just make the change (currently 10) proportional to dy.

7 个评论

Thanks for the answer! It works perfectly.
I have an additional question. What if the acceleration isn't constant but is dependent on a polynomial.
For simplicity, say, it depends on x^2, i.e. for every acceleration, speed = -(distance from start of acceleration)^2 for every acceleration, and for deceleration, speed = (distance from start of deceleration)^2, as seen in the red curve below (sorry, drawn in Paint, not perfect x^2 curves)
So what would be the best method to adjust the speed curve?
I would like to automate this as much as possible, because in each set of speed-distance data, I would need to make ~50 acceleration/deceleration adjustments, and I have 1000 of such speed-distance data sets. In addition, the polynomial may not be a simple x^2 polynomial, but may be more complicated.
At the moment, the only method I can think of is to interpolate the data, say at 1 metre intervals, then process each subsection individually (compare with polynomial, etc).
If interpolation is indeed the best method to adjust the speed curve, then what would be the best method of interpolating the stairs function data?
Thanks in advance!
If you interpolate, you don't need the stairs function at all. Just use interp1 with the 'interpolationmethod' set to 'previous'. It should give the same result as the stairs function but with more x-values. The subsequent linear adjustment will crash, but I suppose you want to replace it with this new polynomial adjustment anyway.
After you solve this issue, I foresee that your next question will be: what if the target speed is not reached before the next deacceleration? You should consider building yourself an incremental model instead of stitching together an algorithm.
It is quite simple. All you need as input is the initial velocity and acceleration (can be different for break and speedup). In each time-step you calculate the new velocity (when velocity =/= target velocity) and the distance traveled. My suggestion is to try this, and I will help you when/if you get stuck.
The only problem I see is that you need to calculate the distance from the speed-reduction (530m in your figure), as the breaking begins before the target velocity changes. So, in each time step you will need to calculate this distance, which depends on current velocity, next target velocity and deacceleration constant. If this distance is larger than the distance to the next target velocity, then accelerate, otherwise begin deacceleration. .
Thank you for the reply, I think that's an excellent suggestion.
So instead of modifying the blue curve to become the red curve, I would generate the red curve from scratch starting from distance zero by recalculating the velocity at every interval (e.g. 1 metres)
Yes, it should be quite straight-forward. I would suggest time-steps. It will also be easy to vary the acceleration function this way.
I can give it a go if you give me some more data.
Thanks for the offer, but I have already completed it!
For the deceleration part, I flipped the speed vector and regenerated it from the back (destination).
Then I flipped the vector back again, and took a min() of both the regenerated acceleration and deceleration profiles.
Your help has been much appreciated indeed!
Nice, that's smart! Your method should work even for the special case I brought up.
Anyway, I figured it was a fun project so started building a model. If I decide to finish it I will post it anyway!
I was so close so figured I'd finish it :)
You can try running the attached script, but you need the fileexchange function InterX ( link ). I have only tested it against a few data points but it looks ok. Figure is for constant acceleration (m/s^2) , but it is very easy to change.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

版本

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by