Error using Dsolve: "Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and..."
21 views (last 30 days)
Show older comments
Hello,
Upon executing the dsolve function using the code provided below, I receive the error message, "Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression." My indexing appears to be correct and I do not believe that my inputs go againt that of the function defintion, so I am unsure why this error message is given.
syms L(t) S(t) B(t)
total=10000;
f1=20;
f2=10;
f3=0.00095;
r1=0.1959;
r2=0.095;
r3=0.110;
c1=rand;
c2=rand;
c3=rand;
M1=rand;
M2=rand;
M3=rand;
gamma1=0.0001;
gamma2=0.0001;
gamma3=0.01;
k1=0.02;
k2=0.02;
k3=0;
ode1 = diff(L) == f1/(f1+f2+f3)*k1*L + f1/(f1+f2+f3)*k2*S + r1*L*(1-(L/M1)) - k1*L - (c1*L/(1+gamma1*L));
ode2 = diff(S) == f2/(f1+f2+f3)*k1*L + f2/(f1+f2+f3)*k2*S + r2*S*(1-(S/M2)) - k2*S - (c2*S/(1+gamma2*S));
ode3 = diff(B) == f3/(f1+f2+f3)*k1*L + f3/(f1+f2+f3)*k2*S + r3*B*(1-(B/M3)) - k3*B - (c3*B/(1+gamma3*B));
odes = [ode1; ode2; ode3]
cond1 = L(0) == (20/30.00095)*total;
cond2 = S(0) == (10/30.00095)*total;
cond3 = B(0) == (0.00095/30.00095)*total;
conds = [cond1; cond2; cond3];
[LSol(t),SSol(t),BSol(t)] = dsolve(odes,conds)
0 Comments
Answers (1)
Star Strider
on 4 Jan 2023
The differential equations are nonlinear, so it is highly unlikely that there is an analytic solution for them.
Integrate them numerically instead (using the appropriate initial conditions and time span) —
syms L(t) S(t) B(t) T Y
total=10000;
f1=20;
f2=10;
f3=0.00095;
r1=0.1959;
r2=0.095;
r3=0.110;
c1=rand;
c2=rand;
c3=rand;
M1=rand;
M2=rand;
M3=rand;
gamma1=0.0001;
gamma2=0.0001;
gamma3=0.01;
k1=0.02;
k2=0.02;
k3=0;
ode1 = diff(L) == f1/(f1+f2+f3)*k1*L + f1/(f1+f2+f3)*k2*S + r1*L*(1-(L/M1)) - k1*L - (c1*L/(1+gamma1*L));
ode2 = diff(S) == f2/(f1+f2+f3)*k1*L + f2/(f1+f2+f3)*k2*S + r2*S*(1-(S/M2)) - k2*S - (c2*S/(1+gamma2*S));
ode3 = diff(B) == f3/(f1+f2+f3)*k1*L + f3/(f1+f2+f3)*k2*S + r3*B*(1-(B/M3)) - k3*B - (c3*B/(1+gamma3*B));
odes = [ode1; ode2; ode3]
cond1 = L(0) == (20/30.00095)*total;
cond2 = S(0) == (10/30.00095)*total;
cond3 = B(0) == (0.00095/30.00095)*total;
conds = [cond1; cond2; cond3];
% [LSol(t),SSol(t),BSol(t)] = dsolve(odes,conds)
[VF,Sbs] = odeToVectorField(odes)
odes_fcn = matlabFunction(VF, 'Vars',{T,Y})
[t,y] = ode15s(odes_fcn, [0 50], zeros(1,3)+0.1);
figure
plot(t,y)
grid
legend(string(Sbs), 'Location','best')
.
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!