Defining ODE function as function file

조회 수: 9 (최근 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 CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by