Hi. This is the code i've been working on, to find values of a column vector concentration c, that varies with time t, as a chemical reaction happens, which basically means solving three rate equations simultaneously.
The error is given below.
The code is given below here.
clear
global k1 k2 %rate constants
k1=2; k2=1; ca0=2; tfin=(1/3);
C0(1)=ca0;C0(2)=0;C0(3)=0; %initial condition of c
timspan=[0,tfin];
[t,C]=ode45(@exampleode,timspan,C0);
%function to calculate rate of change of elements in "c"
function dc_dt=exampleode(C)
global k1 k2
dc_dt(1)=-k1*C(1)-k2*C(1)*C(1);
dc_dt(2)=k1*C(1);
dc_dt(3)=k2*C(1)^2;
end

 採用された回答

Cris LaPierre
Cris LaPierre 2021 年 3 月 13 日
編集済み: Cris LaPierre 2021 年 3 月 13 日

0 投票

The first input to your odefun needs to be t.
Also note that the output of your odefun needs to be a column vector.
Final comment is to not use global variables. Here I use nested functions, but you can also see this example for a way to pass them in the function declaration.
function Ct()
k1=2; k2=1; ca0=2; tfin=(1/3);
C0(1)=ca0;C0(2)=0;C0(3)=0; %initial condition of c
timspan=[0,tfin];
[t,C]=ode45(@exampleode,timspan,C0);
%function to calculate rate of change of elements in "c"
function dc_dt=exampleode(t,C)
dc_dt(1,1)=-k1*C(1)-k2*C(1)*C(1);
dc_dt(2,1)=k1*C(1);
dc_dt(3,1)=k2*C(1)^2;
end
end

1 件のコメント

SAI MAADESH KUMAR N R
SAI MAADESH KUMAR N R 2021 年 3 月 13 日
Thanks a ton, sir! It worked!!!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 13 日

1 投票

The ode function always gets passed time and boundary conditions. It is not required to pay attention to either, but it must be prepared to receive them. You can create a function that does not pass through t but does pass through k1, k2 so that you can get rid of the globals:

1 件のコメント

SAI MAADESH KUMAR N R
SAI MAADESH KUMAR N R 2021 年 3 月 13 日
Thanks a lot sir! It worked!

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

カテゴリ

製品

リリース

R2019b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by