Error using ode45() to solve a couple of ODE's

1 回表示 (過去 30 日間)
david sriker
david sriker 2018 年 11 月 28 日
コメント済み: madhan ravi 2018 年 11 月 28 日
Hello,
Im trying to solve a set of ODE's with inital condition but for some reason the code fails and i cant seem to find the problem, would appreciate some fresh eye to see where im wrong.
the code:
%ODE45 Method
%Const
N0=25;
G1=1;G2=1;
a1=6;a2=3;
k1=1;k2=4;
ODE_Set=@(t,X) [G1.*(N0-a1.*X(1)-a2.*X(2)).*X(1)-k1.*X(1),...
G2.*(N0-a1.*X(1)-a2.*X(2)).*X(2)-k2.*X(2)];
%start position = (0,1)
ODE45_1_Ti=0;
ODE45_1_Tf=10;
ODE45_1_start=[0 1];
[ODE45_1_T,ODE45_1_X]=ode45(@(t,X) ODE_Set,[ODE45_1_Ti ODE45_1_Tf],ODE45_1_start);
now the error it raises:
Error using odearguments (line 95)
@(T,X)ODE_SET returns a vector of length 1, but
the length of initial conditions vector is 2.
The vector returned by @(T,X)ODE_SET and the
initial conditions vector must have the same
number of elements.
now i dont understand whats wrong with the initial conditions
Thanks in advance

採用された回答

madhan ravi
madhan ravi 2018 年 11 月 28 日
編集済み: madhan ravi 2018 年 11 月 28 日
dxdt=[G1.*(N0-a1.*X(1)-a2.*X(2)).*X(1)-k1.*X(1);...
% ^------ should be a semicolon instead of a comma
G2.*(N0-a1.*X(1)-a2.*X(2)).*X(2)-k2.*X(2)];
%start position = (0,1)
ODE45_1_Ti=0;
ODE45_1_Tf=10;
ODE45_1_start=[1 1];
[ODE45_1_T,ODE45_1_X]=ode45(@ODE_Set,[ODE45_1_Ti ODE45_1_Tf],ODE45_1_start); %function call
plot(ODE45_1_T,ODE45_1_X)
function dxdt = ODE_Set(t,X) %function definition
N0=25;
G1=1;G2=1;
a1=6;a2=3;
k1=1;k2=4;
dxdt=[G1.*(N0-a1.*X(1)-a2.*X(2)).*X(1)-k1.*X(1);...
G2.*(N0-a1.*X(1)-a2.*X(2)).*X(2)-k2.*X(2)];
end
  2 件のコメント
david sriker
david sriker 2018 年 11 月 28 日
Yep you are right,
but there is no need to seperate to function
ODE_Set=@(t,X) [G1.*(N0-a1.*X(1)-a2.*X(2)).*X(1)-k1.*X(1);...
G2.*(N0-a1.*X(1)-a2.*X(2)).*X(2)-k2.*X(2)];
%start position = (0,1)
ODE45_1_Ti=0;
ODE45_1_Tf=10;
ODE45_1_start=[0 1];
[ODE45_1_T,ODE45_1_X]=ode45(ODE_Set,[ODE45_1_Ti ODE45_1_Tf],ODE45_1_start);
plot(ODE45_1_X(:,1),ODE45_1_X(:,2));
and now it working fine
but thanks
madhan ravi
madhan ravi 2018 年 11 月 28 日
Anytime :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by