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 个评论
Walter Roberson
2019-9-6
You forgot subplot(3,1,1) before plotting for 2nd part
Khalilullah Mayar
2019-9-6
Torsten
2019-9-6
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
2019-9-6
Walter Roberson
2019-9-6
hold on
Only applies to the Current Axes. You need on for each subplot.
Khalilullah Mayar
2019-9-6
Torsten
2019-9-6
x0 = [x(end,1);x(end,2);x(end,3)]
Khalilullah Mayar
2019-9-6
Khalilullah Mayar
2019-9-6
Torsten
2019-9-6
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
2019-9-6
Khalilullah Mayar
2019-9-30
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
