ode45 - plotting issue for two functions
Show older comments
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
Walter Roberson
on 6 Sep 2019
You forgot subplot(3,1,1) before plotting for 2nd part
Khalilullah Mayar
on 6 Sep 2019
Torsten
on 6 Sep 2019
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;
Khalilullah Mayar
on 6 Sep 2019
Walter Roberson
on 6 Sep 2019
hold on
Only applies to the Current Axes. You need on for each subplot.
Khalilullah Mayar
on 6 Sep 2019
Torsten
on 6 Sep 2019
x0 = [x(end,1);x(end,2);x(end,3)]
Khalilullah Mayar
on 6 Sep 2019
Khalilullah Mayar
on 6 Sep 2019
Torsten
on 6 Sep 2019
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.
Khalilullah Mayar
on 6 Sep 2019
Khalilullah Mayar
on 30 Sep 2019
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!