1

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.

dustin
  • 8,241
Marc
  • 11

1 Answers1

1

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.

Evgeny
  • 5,755
  • It would be better to have a script that calls a function that will do this. That way you dont need to write a loop every time you want the phase plane. One would aimple change the function definition and hit run. – dustin Nov 13 '14 at 14:40
  • I agree. The loop here is just for handling many initial conditions. – Evgeny Nov 13 '14 at 14:42
  • This code isn't working, it is only plotting one curve. – Marc Nov 13 '14 at 14:56
  • 1
    @Marc It works, but graphically, only one curve is visible because they are superimposed. For instance, add the 'o' option to one of the plot3 calls, e.g. plot3(xa(:,1),xa(:,2),xa(:,3),'o');. – EditPiAf Sep 01 '17 at 11:14