Plotting multiple curves using ODE45

1.8k Views Asked by At

I am a beginner to using MATLAB, and I need a lot of help to understand what to do here.

I would like to plot multiple curves using different initial conditions to my system of ODEs.

Here is my code:

f = @(t,x) [-1*x(1)-x(3)*x(3);2*x(2);x(3)-x(2)*x(2)];

[t,xa] = ode45(f,[0 5],[-1 0 0]);

plot3(xa(:,1),xa(:,2),xa(:,3));

grid on

title('Solution curve')

Does anybody know how I could do this?

It would make plotting phase planes a lot easier.

1

There are 1 best solutions below

4
On

As far as I've understood, you have problems with plotting multiple trajectories. Here is your modified code for the case of two trajectories:

title('Solution curve')

f = @(t,x) [-1*x(1)-x(3)*x(3);2*x(2);x(3)-x(2)*x(2)];

[t,xa] = ode45(f,[0 5],[-1 0 0]);

plot3(xa(:,1),xa(:,2),xa(:,3));

grid on

hold on 

[t,xa] = ode45(f,[0 5],[-2 0 0]);

plot3(xa(:,1),xa(:,2),xa(:,3));

However, it's better to use loops when you have many trajectories.