3D Trajectory using ode45

Hye,
I have a problem to plot the trajectories of this three variables dynamical system. I know we can use quiver3 but I am not pretty sure how to do it. Can anyone help me? Here is the matlab code
function myode
[t,xa] = ode45(@f,[0 500],[1 0 -1]);
plot3(xa(:,1),xa(:,2),xa(:,3))
grid on
end
function ydot = f(t,x)
if t < -1
z = x(1);
elseif t <= 0
z = x(1) + 1;
else
z = x(1) + 2;
end
ydot = [x(1)+x(2)+z; x(1)+x(2)+x(3); x(3)];
end

2 Comments

Jan
Jan on 7 Feb 2017
Edited: Jan on 7 Feb 2017
Are you sure, that you want to integrate from t=0 to t=500? Then checking for t<-1 inside f() is not meaningful.
sorry, this is not the real functions that I want to solve. I change to simple one for simplicity. The main problem is plotting the directional field of three variables system. Please help me if you have some ideas. Much appreciated. Thanks

Sign in to comment.

Answers (1)

To get the velocities as input for quiver3, the function to be integrated must be modified:
function myode
[t, xa] = ode45(@f, [0 500], [1, 0, -1]);
dxa = f(t, xa.').';
figure;
quiver3(xa(:,1),xa(:,2),xa(:,3), dxa(:,1),dxa(:,2),dxa(:,3));
grid on
end
function ydot = f(t, x)
if t < -1
z = x(1, :);
elseif t <= 0
z = x(1, :) + 1;
else
z = x(1, :) + 2;
end
ydot = [x(1, :) + x(2, :) + z; x(1, :) + x(2, :) + x(3, :); x(3, :)];
end
Matlab's integrators cannot handle discontinuities. You will get a final position, but from the viewpoint of a scientific application of numerical methods, this is not a trustworthy result. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 . As long as you integrate from 0 to 500, the problem concerns the first point t<=0 only, but it is not clean.
Your trajectory explodes at t==270.0578 and you get NaNs.

Tags

Asked:

on 7 Feb 2017

Commented:

on 7 Feb 2017

Community Treasure Hunt

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

Start Hunting!