Solving Differential Equations using ODE45

1 次查看(过去 30 天)
Hello!
I am trying to find S from t= 0 to 10 but I couldn't manage to sort the code with the tutorials, S depends on X and I keep getting the error that I need an array of length 21 as an initial condition:
Equations:
ds/dt= D*(10-S)+rs
dx/dt= -D*X+um*X;
rs=-(ms*X+u*X/Y)
u=um*S/(k+S);
Code:
%Constants:
um=0.8; k=0.01; ms=0.05; Y=0.44; D=0.3; So=10;
t=0:0.5:10;
dxdt=@(t,X) -D*X+um*X;
[t,X]=ode45(dxdt, t, 1);
rs=-(ms*X+u*X/Y);
u=um*S/(k+S);
dsdt=@(t,S) D*(10-S)+rs;
[t,S]=ode45(dsdt, t, 10);

采纳的回答

Star Strider
Star Strider 2019-11-9
I decided to let the Symbolic Math Toolbox have a go at your equations, since they involved some substitutions:
syms S(t) X(t) um k ms Yc D So Y t
u = um*S/(k+S);
rs = -(ms*X+u*X/Yc);
Eqns = [diff(S) == D*(10-S)+rs; diff(X) == -D*X+um*X];
[VF,Sbs] = odeToVectorField(Eqns)
odefcn = matlabFunction( VF, 'Vars',{t,Y, um,k,ms,Yc,D})
um=0.8; k=0.01; ms=0.05; Yc=0.44; D=0.3; So=10;
tspan=0:0.5:10;
ic = [10, So];
[T,Y] = ode45(@(t,Y)odefcn(t,Y,um,k,ms,Yc,D), tspan, ic);
figure
plot(T, Y)
grid
legend(string(Sbs), 'Location','S')
I included the constants as arguments, rather than hard-coding them in your equations. I also assume that ‘So’ is the initial condition for ‘S(t)’. In this code, ‘S(t)’ is the second column of ‘Y’.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by