フィルターのクリア

Defining ODE function as function file

5 ビュー (過去 30 日間)
Carey n'eville
Carey n'eville 2020 年 11 月 23 日
コメント済み: Jon 2020 年 11 月 23 日
Hi everybody
I wrote a code of first order ODE but it doesn't work, how could I fix it? Could you help me please? Additionally I need to draw a graph of these three value in one graph. The code is given below.
Also I get Errors:
Error using ConcentrationsofXYZ>ConcC
Too many input arguments.
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 ConcentrationsofXYZ (line 17)
[t,Z] = ode45(@ConcC,tspan,X0,Y0,Z0);
clear all;
clc;
close all;
X0=1;
tspan = [0 24];
[t,X] = ode45(@ConcA,tspan,X0);
X0=1;
Y0=4;
tspan = [0 24];
[t,Y] = ode45(@ConcB,tspan,X0,Y0);
X0=1;
Y0=4;
Z0=6;
tspan = [0 24];
[t,Z] = ode45(@ConcC,tspan,X0,Y0,Z0);
function X=ConcA(t,X)
k1=1.26;
X0=1;
X=X0*exp(-k1*t);
end
function Y=ConcB(t,Y)
k1=1.26;
k2=0.74;
X0=1;
Y0=4;
Y=Y0*exp(-k2*t)+((X0*k1)/(k2-k1)*(exp(-k1*t)-exp(-k2*t)));
end
function Z=ConcC(t,Z)
k1=1.26;
k2=0.74;
k3=0.22;
X0=1;
Y0=4;
Z0=6;
Z=Z0*exp(-k3*t)+((Y0*k2)/(k3-k2)*(exp(-k2*t)-exp(-k3*t)))+X0*k1*k2*((exp(-k1*t))/((k2-k1)*(k3-k1))-(exp(-k2*t))/((k2-k1)*(k3-k2))-(exp(-k3*t))/((k3-k1)*(k3-k2)));
end

採用された回答

Jon
Jon 2020 年 11 月 23 日
Looking briefly at your function definitions, it seems that maybe you are misunderstanding what this function is to compute. It should give the value of the rate of change dx/dt of the variable x, as a function of x and perhaps time (if the current rate of change depends not only only on the value of x, but also time). In your case I don't see any dependence of dx/dt ,dy/dt, or dz/dt upon the concentration values, they only seem to depend upon time. Could you please confirm that your functions are correct in this respect aside from any MATLAB coding errors you may have.
  2 件のコメント
Carey n'eville
Carey n'eville 2020 年 11 月 23 日
編集済み: Carey n'eville 2020 年 11 月 23 日
I have fixed the code according to this information thank you so much :) But I splitted this one into 3 codes, If I want to run this code in single m.file, what should I do? It doesn't work in single m.file with fixed version.
Jon
Jon 2020 年 11 月 23 日
Hi I'm glad you are making good progress now. I'm not sure exactly what you mean by running it in a single m file. I think maybe you are trying to have all of the concentrations calculated in just one function (one file).
You can definitely do this as ode45 will evaluate an expressions that returns a 3 element vector whose elements are repectively dx/dt, dy/dt, dz/dt. So you could have something like
function dCdT=ConcA(t,C)
% assign your rate coefficient etc here
dCdT(1) = % put your expressions for dX/dt here
dCdT(2) = % put your expressions for dY/dt here
dCdt(3) = % put your expressions for dZ/dt here
end
Then when you call ode45, you will pass it a vector with the initial concentrations of x,y, and z. You will get back a single time vector, and an array where each row gives the concentrations of x, y and z respectively at each of the computed times

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

その他の回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 11 月 23 日
The way you've coded ConcA it should solve an ODE that looks like this:
That might very well be the ODE you need to solve, but if that's the case why not straight integrate it by hand. It seems more likely that If you have an ODE that looks something like this:
But that is something you have to make clear.
HTH

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by