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 commentaires

Jan
Jan le 7 Fév 2017
Modifié(e) : Jan le 7 Fév 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.
Peter DH
Peter DH le 7 Fév 2017
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

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 7 Fév 2017

0 votes

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

Question posée :

le 7 Fév 2017

Commenté :

le 7 Fév 2017

Community Treasure Hunt

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

Start Hunting!

Translated by