How to solve two differential equations using ode45.

My system is this
x"+ x'+ x + y'=0;
y"+ y'+ y + x'=0;
i need to solve these differential equations using ode's.
thanks in advance.

2 件のコメント

Torsten
Torsten 2018 年 2 月 22 日
You will need four initial conditions (for x,x',y,y') if you want to use ODE45 as numerical solver.
Ebraheem Menda
Ebraheem Menda 2018 年 2 月 22 日
Thank you Torsten. i have the initial conditions. but my question is how to convey these equations to ode45 or any other solver. Because they are coupled equations. thanks for your help.

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2018 年 2 月 22 日

5 投票

fun=@(t,y)[y(2);-y(2)-y(1)-y(4);y(4);-y(4)-y(3)-y(2)];
x0=...; %supply x(t0)
x0prime=...; %supply x'(t0)
y0=...; %supply y(t0)
y0prime=...; %supply y'(t0)
Y0=[x0; x0prime; y0; y0prime];
tspan=[0 5];
[T,Y]=ode45(fun,tspan,Y0);
plot(T,Y(:,1)) %plot x
plot(T,Y(:,3)) %plot y
Best wishes
Torsten.

9 件のコメント

Ebraheem Menda
Ebraheem Menda 2018 年 2 月 22 日
Thank you Mr.Torsten. i will try it and give reply.
Ebraheem Menda
Ebraheem Menda 2018 年 2 月 24 日
Hello Torsten it seems you have applied change of variable. like x'=y(1),x''=y(2) and y'=y(4) then what is 'y(3)'. if what i understood from your answer is right then the equations become fun=@(t,y)[y(2);-y(2)-y(1)-y(4);y(4);-y(4)-y(1)-y(2)] not fun=@(t,y)[y(2);-y(2)-y(1)-y(4);y(4);-y(4)-y(3)-y(2)] am i correct? thank you in advance.
Ebraheem Menda
Ebraheem Menda 2018 年 2 月 24 日
Thank you Torsten.
Torsten
Torsten 2018 年 2 月 26 日
y1=x, y2=x', y3=y, y4=y'.
Thus my ODE-function looks correct, doesn't it ?
Best wishes
Torsten.
Ebraheem Menda
Ebraheem Menda 2018 年 2 月 26 日
Yes you are correct Sir. Thank you.
Joe Byrne
Joe Byrne 2020 年 3 月 13 日
Hi Torsten, I was just wodering what was going on in the first line of code here? I am attempting a similar problem but with different ODEs, was just wondering what the relevance of this line was to the rest of the code, i.e. what variables were assigned to what, etc.
Cheers
Joe Byrne
Joe Byrne 2020 年 3 月 13 日
I mean to say what is the significance of y(2) and y(4) given that they have their own row in the array?
Andreas Zimmermann
Andreas Zimmermann 2020 年 4 月 16 日
編集済み: Andreas Zimmermann 2020 年 4 月 16 日
Hi Joe,
Using Torsten's relations
x = y(1)
x' = y(2)
y = y(3)
y' = y(4)
the first line
fun=@(t,y)[y(2); -y(2)-y(1)-y(4); y(4); -y(4)-y(3)-y(2)];
can be translated to the following, using Torsten's relations:
[x'; -x'-x-y'; y'; -y'-y-x']
which is essentially
[x'; x"; y'; y"].
He got these relations by solving for x" and y":
x"+ x'+ x + y'=0; => x" = -x' -x -y' = -y(2) -y(1) -y(4)
y"+ y'+ y + x'=0; => y" = -y' -y +x' = -y(4) -y(3) -y(2)
I have tried to use ode45 to solve the SIR model from this fantastic Geogebra Tutorial: (https://youtu.be/k6nLfCbAzgo)
% Just some start parameters and coefficients
Istart = 0.01;
Sstart = 0.99;
Rstart = 0;
transm = 3.2;
recov = 0.23;
maxT = 20;
% define S',I',R'
% S' = - transm * S * I
% I' = transm * S I - recov * I
% R' = recov * I
% S is y(1)
% I is y(2)
% R is y(3)
%so here fun = @(t,y)[S'; I'; R'];
fun = @(t,y)[-transm*y(1)*y(2); (transm*y(1)*y(2))-(recov*y(2)); recov*y(2)];
% Provide the starting parameters
Y0 = [Sstart; Istart; Rstart;];
% Define the range of t
tspan = [0 maxT];
% Magic happens and matrix Y contains S,I,R
[T,Y] = ode45(fun,tspan,Y0);
% Plot plot
figure
plot(T,abs(Y(:,1)),'b-') % S
hold on
plot(T,abs(Y(:,2)),'r-') % I
plot(T,abs(Y(:,3)),'g-') % R
xlim([0 23])
%Hope this helps :)
% Many thanks to Torsten and Ebraheem for your very very useful discussion!
Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
👍

サインインしてコメントする。

その他の回答 (3 件)

Abraham Boayue
Abraham Boayue 2018 年 2 月 24 日

0 投票

Hey Ebraheem There are many excellent methods that you can use to solve your problem, for instance, the finite difference method is a very powerful method to use. I can try with that.The ode45 function is a matlab built in function and was designed to solve certain ode problems, it may not be suitable for a number of problems. What are the initial values of your equations? Do you have any plot of the solution that one can use as a guide?

1 件のコメント

Ebraheem Menda
Ebraheem Menda 2018 年 2 月 24 日
thank you Abraham for your response. i am yet to solve those equations and obtain initial conditions. once i got them i will post the equations. Meanwhile what is this finite difference method ? is it available in matlab 2009b ?

サインインしてコメントする。

Abraham Boayue
Abraham Boayue 2018 年 2 月 24 日

0 投票

The finite difference method is used to solve differential and partial equations. It is easier to implement in matlab. You can do the coding in any version of matlab, I have taken a course in numerical mathematics before and have a fairly good knowledge of how to solve such problems.

5 件のコメント

Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
👍
Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
👏
Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
please post your email Abraham
Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
will contact you personally.
madhan ravi
madhan ravi 2020 年 7 月 19 日
You can email him by clicking his profile.

サインインしてコメントする。

Abraham Boayue
Abraham Boayue 2020 年 7 月 19 日

0 投票

Hi Ebraheem Is there anything specific that you want me to do for you?

2 件のコメント

Ebraheem Menda
Ebraheem Menda 2020 年 7 月 19 日
Thank you. If I need will let you know for sure.
Ebraheem Menda
Ebraheem Menda 2021 年 7 月 1 日
fun=@(t,y)[y(2);-y(2)-y(1)-y(4);y(4);-y(4)-y(3)-y(2)];
x0=...; %supply x(t0)
x0prime=...; %supply x'(t0)
y0=...; %supply y(t0)
y0prime=...; %supply y'(t0)
Y0=[x0; x0prime; y0; y0prime];
tspan=[0 5];
[T,Y]=ode45(fun,tspan,Y0);
plot(T,Y(:,1)) %plot x
plot(T,Y(:,3)) %plot y
hi Abraham i want to put this code in for loop and run it for 2 or more tspan. Could you please help out on this ?

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by