Solving coupled ode in MATLAB

8 ビュー (過去 30 日間)
Lada Nuzhna
Lada Nuzhna 2020 年 1 月 27 日
コメント済み: Star Strider 2021 年 1 月 12 日
I am trying solve a system of coupled differential equantions (one of which is second order). I am searching for a numerical answer. However, code keeps giving me an error and I can't track it down. Would be greatful for any help! The code is below:
%function definitions
syms r(T) p(T);
ode1 = diff(r,T,2) == E^2 / ((m^2)*(c^2)) - (1- rs/r)*(c^2 + (h^2)/(r^2));
ode2 = diff(p,T) == (1/r^2) * L;
%function setup
f1= odeToVectorField(ode1);
F1= matlabFunction(f1);
f2= odeToVectorField(ode2);
F2= matlabFunction(f2);
odes = {F1,F2};
%initial conditions
r_0= 5*rs;
r_prime_0 = 5*rs;
r_double_prime_0 = 5*rs;
p_0 = 0;
p_prime_0 = PI/100;
conditions = [r_0;r_prime_0;r_double_prime_0;p_0;p_prime_0];
span = [0 100];
%solver
[rsol,psol] = ode45(odes,span,conditions);

採用された回答

Star Strider
Star Strider 2020 年 1 月 27 日
This should get you started:
% %function definitions
syms r(T) p(T) c E h L m rs T Y
ode1 = diff(r,T,2) == E^2 / ((m^2)*(c^2)) - (1- rs/r)*(c^2 + (h^2)/(r^2));
ode2 = diff(p,T) == (1/r^2) * L;
% %function setup
[f1, Subs]= odeToVectorField([ode1 ode2]);
F1= matlabFunction(f1, 'Vars',{T,Y, c, E, h, L, m, rs})
% %initial conditions
r_0= 5*rs;
r_prime_0 = 5*rs;
r_double_prime_0 = 5*rs;
p_0 = 0;
PI = rand; % Provide Value
p_prime_0 = PI/100;
c = rand; % Provide Value
E = rand; % Provide Value
h = rand; % Provide Value
L = rand; % Provide Value
m = rand; % Provide Value
rs = rand; % Provide Value
conditions = [rand(3,1)]; % Three Equations = Three Initial Conditions
span = [0 100];
% %solver
[rsol,psol] = ode45(@(T,Y)F1(T,Y,c,E,h,L,m,rs),span,conditions);
figure
plot(rsol, psol)
grid
Note that if you want a numerical solution, the required parameter values must be defined first, before calling any of the numeric ODE solvers (such as ode45).
  2 件のコメント
Lada Nuzhna
Lada Nuzhna 2020 年 1 月 27 日
Thank you so so much!
Star Strider
Star Strider 2020 年 1 月 27 日
As always, my pleasure!

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

その他の回答 (1 件)

Pitt Lucht
Pitt Lucht 2021 年 1 月 12 日
Hello,
I have a similar problem of three coupled differential equations. I have already adapted my code to fit the suggested code from Star Strider, but I'm still runnning into problems. The current error code I'm getting is "too many input arguments", my guess is there is a problem with the way I set up my initial conditions? If somebody has the time to have a quick look over my code it would be highly appreciated!
global kr kf lf dr dm p1 p2
% Parameter
kr = 1; % formation R-centers
kf = 0.1; % formation M-centers
lf = 100; % loss of F-centers
dr = 0.1; % decay R-center
dm = 1; % decay M-center
p1 = 10000;
p2 = 0;
% function definiton
syms R(T) M(T) F(T) T Y;
ode1 = diff(R,T) == -(dr*R) + (kr*M*F);
ode2 = diff(M,T) == (dr*R) - (dm*M) + (kf*(F^2)) - (kr*M*F);
ode3 = diff(F,T,2) == (dr*R) + (2*dm*M) - (kr*M*F) - (2*kf*(F^2)) - (lf*F)+p1;
% function setup
[f1, Subs] = odeToVectorField([ode1 ode2 ode3]);
F1 = matlabFunction(f1, 'Vars',{T, Y});
% initial states
R0 = 84.99;
M0 = 1.675;
F0 = 9.975;
conditions = [R0; M0; F0];
span = [0 10];
% solver
tic;
[Rsol, Msol, Fsol] = ode23s(@(T,Y)F1(T,Y,kr,kf,lf,dr,dm,p1),span,conditions);
toc;
%Log. plotting
loglog(Rsol, Msol, Fsol);
grid on;
  5 件のコメント
Pitt Lucht
Pitt Lucht 2021 年 1 月 12 日
It seems to not be possible to vote for comments, therefore I voted for your original answer to Lada Nuzhna since that helped out a lot too
Star Strider
Star Strider 2021 年 1 月 12 日
Thank you!
Yes, voting for an Answer is the only option in this instance.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by