Generate Plot Based on Interval Data

7 visualizzazioni (ultimi 30 giorni)
Philip So
Philip So il 9 Ago 2018
Modificato: jonas il 10 Ago 2018
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.

Risposta accettata

jonas
jonas il 9 Ago 2018
Modificato: jonas il 9 Ago 2018
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 Commenti
jonas
jonas il 10 Ago 2018
Modificato: jonas il 10 Ago 2018
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!
jonas
jonas il 10 Ago 2018
Modificato: jonas il 10 Ago 2018
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by