MATLAB Answers

ode45 gave errors

조회 수: 1(최근 30일)
Carey n'eville
Carey n'eville 2020년 12월 19일
댓글: Carey n'eville 2020년 12월 19일
Hello everyone, I have a case about two concentration which are ode functions. So, I guess I have to use ode 45 but in the code given below I couldn't figure out how to draw X(biomass) and S(substrate) concentration in time (in same graph). Could you help me?
clear all;
clc;
close all;
X0=2;
tspan = [0 40];
[tX,X]=ode45(@biomass, tspan, X0);
S0=1000;
tspan = [0 40];
[tS,S]=ode45(@substrate, tspan, S0);
figure
hold on
plot(tX,X)
plot(tS,S)
xlabel('time (year)')
ylabel('Concentration (mg/L)')
legend('Biomass','Substrate','location','southeast')
function biomassgrowth=biomass(t,X)
X0=2;
Sin=1000;
So=0;
S0=So+Sin;
V=10;
TH=40;
ko=0.2;
kd=0.01;
Ks=150;
Y=0.5;
S=-((X0/Y)*ko*TH)+S0-(Ks*ln(S/S0));
biomassgrowth=((ko*S*X)/(Ks+S))-(kd*X);
end
function substratutilize=substrate(t,S)
X0=2;
Sin=1000;
So=0;
S0=So+Sin;
ko=0.2;
Ks=150;
Y=0.5;
substratutilize=-((ko*X0*S)/(Y*(Ks+S)));
end
ERRORS I HAVE GOT:
Unrecognized function or variable 'S'.
Error in ikinciHW6Q2>biomass (line 32)
S=-((X0/Y)*ko*TH)+S0-(Ks*ln(S/S0));
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 ikinciHW6Q2 (line 7)
[tX,X]=ode45(@biomass, tspan, X0);

채택된 답변

Alan Stevens
Alan Stevens 2020년 12월 19일
편집: Alan Stevens 2020년 12월 19일
You use S in calculating X, so you need to solve them together, not one after the other.
Also ln is log in MATLAB.
Try
XS0=[2, 1000];
tspan = [0 40];
[t,XS]=ode45(@BSfn, tspan, XS0);
figure
hold on
plot(t,XS(:,1),t,XS(:,2))
xlabel('time (year)')
ylabel('Concentration (mg/L)')
legend('Biomass','Substrate','location','southeast')
function dXSdt=BSfn(~,XS)
X = XS(1); S = XS(2);
X0=2;
Sin=1000;
So=0;
S0=So+Sin;
V=10;
TH=40;
ko=0.2;
kd=0.01;
Ks=150;
Y=0.5;
S=-((X0/Y)*ko*TH)+S0-(Ks*log(S/S0));
biomassgrowth=((ko*S*X)/(Ks+S))-(kd*X);
substratutilize = -((ko*X0*S)/(Y*(Ks+S)));
dXSdt = [biomassgrowth; substratutilize];
end
  댓글 수: 4
Carey n'eville
Carey n'eville 2020년 12월 19일
Yes same code with tspan=[0 10]= graph 1 for this case TH=10
tspan=[0 20]= graph 2 for this case TH=20
tspan=[0 40]= graph 3 for this case TH=40 (you'have already wrote it)
I mention plotting these 3 code in unique graph

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Alan Stevens
Alan Stevens 2020년 12월 19일
If you want them in different colours on one graph then just plot(t(1:n),XS(1:n,1),'r',t(n+1:m),XS(n+1:m,1),'b', t(m+1:end),XS(m+1:end,1),'g') where n is the index corresponding to a time of 10, and m corresponds to a time of 20. I'm not sure I've understood what you are trying to achieve though!
  댓글 수: 1
Carey n'eville
Carey n'eville 2020년 12월 19일
Thanks I guess this is true!

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by