3D Trajectory using ode45
Show older comments
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
Answers (1)
Jan
on 7 Feb 2017
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.
Categories
Find more on Analog Filters 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!