Error with Invalid indexing in system of differential equations
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sara Westerlund
am 1 Mär. 2022
Bearbeitet: Star Strider
am 1 Mär. 2022
I want to solve an ODE and getting this error message:
Error using sym/subsindex (line 953)
Invalid indexing or function definition. Indexing must follow MATLAB indexing.
Function arguments must be symbolic variables, and function body must be sym
expression.
Error in kinetik_3 (line 28)
[sSol(t),eSol(t),cSol(t),pSol(t)] = dsolve(odes);
k1 = 1.0;
k2 = 0.8;
k3 = 1.1;
syms s(t) e(t) c(t) p(t)
ode1 = diff(s) == -k1*s*e + k2*c;
ode2 = diff(e) == -k1*s*e + k2*c + k3*c;
ode3 = diff(c) == k1*s*e - k2*c - k3*c;
ode4 = diff(p) == k3*c;
odes = [ode1; ode2; ode3; ode4];
cond1 = s(0) == 2.9;
cond2 = e(0) == 1.3;
cond3 = c(0) == 0;
cond4 = p(0) == 0;
conds = [cond1; cond2; cond3; cond4];
[sSol(t),eSol(t),cSol(t),pSol(t)] = dsolve(odes,conds);
Can someone please explain?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 1 Mär. 2022
Bearbeitet: Star Strider
am 1 Mär. 2022
Remove the ‘(t)’ in the dsolve output and the error disappears. However, the system is nonlinear, so an analytic solution likely does not exist. Numerically integrate it instead.
k1 = 1.0;
k2 = 0.8;
k3 = 1.1;
syms s(t) e(t) c(t) p(t) T Y
ode1 = diff(s) == -k1*s*e + k2*c;
ode2 = diff(e) == -k1*s*e + k2*c + k3*c;
ode3 = diff(c) == k1*s*e - k2*c - k3*c;
ode4 = diff(p) == k3*c;
odes = [ode1; ode2; ode3; ode4];
cond1 = s(0) == 2.9;
cond2 = e(0) == 1.3;
cond3 = c(0) == 0;
cond4 = p(0) == 0;
conds = [cond1; cond2; cond3; cond4];
% [sSol,eSol,cSol,pSol] = dsolve(odes,conds);
[VF,Subs] = odeToVectorField(odes)
odesfcn = matlabFunction(VF, 'Vars',{T,Y})
ics = [1.3; 2.9; 0; 0]; % Initial Conditio9ns Vector (See 'Subs' To Understand The Order)
tspan = linspace(0,20,250); % Time Vector For Integration
[t,y] = ode45(odesfcn, tspan, ics); % Integrate
figure
plot(t, y, 'LineWidth',1.5)
grid
legend(string(Subs), 'Location','best')
Change as necessary to get the desired result.
EDIT — (1 Mar 2022 at 22:12)
Changed ‘tspan’ to produce smoother curves.
.
0 Kommentare
Weitere Antworten (1)
Davide Masiello
am 1 Mär. 2022
I think the problem is in the syntax of your last line.
You should simply write
sSol = dsolve(odes,conds);
I run this on MatLab online and it does not throw an error any longer.
But it still cannot find a symbolic solution.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!