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!
