Error with Invalid indexing in system of differential equations

1 view (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 1 Mar 2022
Edited: Star Strider on 1 Mar 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)
VF = 
Subs = 
odesfcn = matlabFunction(VF, 'Vars',{T,Y})
odesfcn = function_handle with value:
@(T,Y)[Y(3).*(1.9e+1./1.0e+1)-Y(1).*Y(2);Y(3).*(4.0./5.0)-Y(1).*Y(2);Y(3).*(-1.9e+1./1.0e+1)+Y(1).*Y(2);Y(3).*(1.1e+1./1.0e+1)]
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.
.

More Answers (1)

Davide Masiello
Davide Masiello on 1 Mar 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.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!