Solving two equations with ODE45

Hi, I used the following command to solve one ODE
fun = @(x,y)[y(2);y(3);(y(1)*(1-x^2))];
y0 = [1 0 8]; %y, y', y''
xspan = [-3 3];
[X,Y] = ode45(fun,xspan,y0);
plot(X,Y(:,1)),,% X,Y(:,2),X,Y(:,3),X,Y(:,1).* (2 + X .^ 2));
However, I would like to solve this pair of equations instead of the given "fun":
How do I add two equations in the fun line?
Thanks!

 Risposta accettata

fun=@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);y(6);(1+x^2)*y(1)]; %psi2,psi2',psi2'',psi1,psi1',psi1''
Best wishes
Torsten.

9 Commenti

Sergio Manzetti
Sergio Manzetti il 4 Apr 2018
Modificato: Sergio Manzetti il 4 Apr 2018
Thanks Torsten! I want to make sure you saw the right equation, it is only composed of third order derivative!
I get an error:
Index exceeds matrix dimensions.
Error in Schroding_plot>@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);y(6);(1+x^2)*y(1)]
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Plot (line 8)
[X,Y] = ode45(fun,xspan,y0);
with
fun=@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);y(6);(1+x^2)*y(1)]; %psi2,psi2',psi2'',psi1,psi1',psi1''
y0 = [1 0 8]; %y, y', y''
xspan = [-3 3];
[X,Y] = ode45(fun,xspan,y0);
plot(X,Y(:,1)), X,Y(:,2),X,Y(:,3),X,Y(:,1).* (2 + X .^ 2);
Torsten
Torsten il 4 Apr 2018
Modificato: Torsten il 4 Apr 2018
You must change y0 to a 6x1 column vector.
And I saw the equations for psi1 and psi2.
How would that look like in the plot part?
I wrote now:
plot(X,Y(:,1)), X,Y(:,2),X,Y(:,3),X,Y(:,1);
plot(X,Y(:,2)), X,Y(:,2),X,Y(:,3),X,Y(:,1);
both should have the same start conditions, but I am not sure here how to distinguish y_1 and y_2 in the plot
Torsten
Torsten il 4 Apr 2018
Modificato: Torsten il 4 Apr 2018
The examples under
https://de.mathworks.com/help/matlab/ref/ode45.html
help you to distinguish between plot curves.
Best wishes
Torsten.
Ok, now its more clear with your link:
I used this :
fun=@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);(1+x^2)*y(1)]; %psi2,psi2',psi2'',psi1,psi1',psi1''
y0 = [1 0 8]; %y, y', y''
xspan = [-3 3];
[X,Y] = ode45(fun,xspan,y0);
plot(X,Y(:,1)), X,Y(:,2),X,Y(:,3),X,Y(:,1), '-o',X,Y(:,2), X,Y(:,2),X,Y(:,3),X,Y(:,1);
but then I got that again :
Index exceeds matrix dimensions.
Error in plot>@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);(1+x^2)*y(1)]
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in plot (line 8)
[X,Y] = ode45(fun,xspan,y0);
isn't the use of the '-o' what I needed here?
fun is incorrect (5 instead of 6 components), y0 is inconsistent (3 instead of 6 initial values), plot(X,Y(:,1)),X,... doesn't make sense.
Thanks, following your link I used a new y0 and got a result, but plot only plots one form.
fun=@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);y(6);(1+x^2)*y(1)]; %psi2,psi2',psi2'',psi1,psi1',psi1''
y0 = [1 0 8 1 0 8]; %y, y', y''
xspan = [-3 3];
[X,Y] = ode45(fun,xspan,y0);
plot(X,Y(:,1)), X,Y(:,2),X,Y(:,3),X,Y(:,1), '-o',X,Y(:,2), X,Y(:,2),X,Y(:,3),X,Y(:,1);
What is wrong in the plot section really?
Hi Torsten, I edited this finally:
fun=@(x,y)[y(2);y(3);-(1+x^2)*y(4);y(5);y(6);(1+x^2)*y(1)]; %psi2,psi2',psi2'',psi1,psi1',psi1''
y0 = [1 0 8 1 0 8]; %y, y', y''
xspan = [-3 3];
[X,Y] = ode45(fun,xspan,y0);
plot(X,Y(:,1)), X,Y(:,2),X,Y(:,3),X,Y(:,1);
So it shows a plot. But this plot is identical to the plot of only one function present in the command (remove the second function and decrease to three dimensions, and it is the same plot).
Is that correct? I would have expected a different solution.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by