ode45 - plotting issue for two functions

Hi everyone,
Could anyone help how to get a continuous plot of ode45 results for two different tspans of [0 10] and [10 20] for two slightly different functions.
The two functions are for linear kinematic motion with linear varying acceleration and constant acceleration scenarios as follows.
Linear varying acceleration motion function
function dxdt = vdp6(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 2;
Constant acceleration motion function
function dxdt = vdp7(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= 2;
dxdt(3)= 0;
The part I struggle with is the second section of the code to get a continous graph for the behavior variables over the entire [0 20] tspane:
tspan=[0 10];
x0=[0;0;4];
[t,x] = ode45(@vdp6,tspan,x0);
subplot(3,1,1);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
subplot(3,1,3);
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
% 2nd part
hold on;
tspan=[10 20];
x0=[0;0;0];
[t,x] = ode45(@vdp7,tspan,x0);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
subplot(3,1,3); % Notes
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
hold off;
Thank you!

12 Comments

You forgot subplot(3,1,1) before plotting for 2nd part
Added. Still the subplot(3,1,1) displaying tspan of [10 20] not [0 20]?
Constant acceleration motion function
function dxdt = vdp7(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 0;
Linear varying acceleration motion function
function dxdt = vdp6(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 2;
Corrected but still only the [10 20] tspan is displlayed:
functions:
function dxdt = vdp6(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 2;
function dxdt = vdp7(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 0;
code:
tspan=[0 10];
x0=[0;0;4];
[t,x] = ode45(@vdp6,tspan,x0);
subplot(3,1,1);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
subplot(3,1,3);
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
tspan=[10 20];
x0=[0;0;0];
[t,x] = ode45(@vdp7,tspan,x0);
hold on;
subplot(3,1,1);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
subplot(3,1,3);
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
hold off;
hold on
Only applies to the Current Axes. You need on for each subplot.
Thanks that worked well. One last thing, the 2nd graph doesn't pick up from the point where the 1st one ends (t=10sec). Although I did it manually by selecting the initial condition for the 2nd graph as the end point for 1st one: x0=[333.3;100;20]. Is there any way this could be coded?
tspan=[10 20];
x0=[333.3;100;20];
[t,x] = ode45(@vdp7,tspan,x0);
x0 = [x(end,1);x(end,2);x(end,3)]
Back with another issue. Consider the first component of motion with constant acceleration and then the 2nd one with constant velocity.
I revised the function as follows:
Constant acceleration:
function dxdt = vdp7(t,x)
dxdt= zeros(3,1)
dxdt(1)= x(2);
dxdt(2)= x(3);
dxdt(3)= 0;
Constant velocity:
function dxdt = vdp8(t,x)
dxdt= zeros(3,1);
dxdt(1)= x(2);
dxdt(2)= 0;
dxdt(3)= 0;
Code:
tspan=[0 10];
x0=[0;0;2];
[t,x] = ode45(@vdp7,tspan,x0);
hold on;
subplot(3,1,1);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
hold on;
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
hold on;
subplot(3,1,3);
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
tspan=[10 20];
x0=[x(end,1);x(end,2);x(end,3)];
[t,x] = ode45(@vdp8,tspan,x0);
hold on;
subplot(3,1,1);
plot(t,x(:,1),'-',t,x(:,2),'-',t,x(:,3),'-')
title('Behaviour Variables vs Time');
xlabel('Time t');
ylabel('x, v, a');
legend('x_1(x)','x_2(v)','x_3(a)')
hold on;
subplot(3,1,2);
plot(x(:,1),x(:,2),'-')
title('Distance vs Velocity');
xlabel('Distance');
ylabel('Velocity');
hold on;
subplot(3,1,3);
plot(x(:,2),x(:,3),'-')
title('Velocity vs Acceleration');
xlabel('Velocity');
ylabel('Acceleration');
hold off;
Now at point t=10 sec the acceleratoin plot should drop to 0 which didn't apear so (plot enclosed) rather continues with its previous value of 2. plot.jpg
Why should x(3) drop to 0 if you specify x0(3) = x(end,3) ( = 2) and dx3/dt = 0 ?
x(3) will remain at its value at t=10, namely x(3) = 2.
But since position only depends on velocity and velocity is kept constant, the value you prescribe for acceleration is irrelevant for the result.
I did manually set x0(3) to zero, now the graph got fixed for subplot(1) but for subplot (3) none of the changes reflects - still a horizontal line at value 2 all the way long.
Hi Torsten,
Any help on following is highly appreciated:
https://www.mathworks.com/matlabcentral/answers/481828-idm-car-following-microspoic-model?s_tid=prof_contriblnk

Sign in to comment.

Answers (0)

Asked:

on 6 Sep 2019

Commented:

on 30 Sep 2019

Community Treasure Hunt

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

Start Hunting!