MATLAB Answers

Symbolic ODEs aren't symbolic?

3 views (last 30 days)
I am trying to solve a system of ODEs with initial conditions and graph them, but the solver isn't accepting my ODEs
Here is my code
clear
t_interval = [0, 1200];
k1 = 1.08e-3;
k2 = 1.19e-3;
k3 = 1.54e-3;
syms A(t) R(t) S(t)
ode1 = diff(A) == -k1*A;
ode2 = diff(R) == k1*A - k2*R - k3*R*S;
ode3 = diff(S) == k2*R + k3*R*S;
odes = [ode1, ode2, ode3];
initialA = A(0) == 0.019;
initialR = R(0) == 0;
initialS = S(0) == 0;
ICs = [initialA, initialR, initialS];
[Asol(t), Rsol(t), Ssol(t)] = dsolve(odes, ICs);
Warning: Unable to find symbolic solution.
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.
hold on
grid on
fplot(Asol,t_interval);
fplot(Rsol,t_interval);
plot(Ssol,t_interval);
Legend('A(t)','R(t)', 'S(t)');
However, I keep getting the above error message. Does this mean that an analytical solution doesn't exist? or am I doing something wrong in my equations?
After browsing this forum, I saw common problems that led to this error were not seperating variables and values with mathematical symbols or using undeclared symbols, but I don't think I have done any of that.

Accepted Answer

Star Strider
Star Strider on 20 Oct 2021
Because of the ‘R*s’ terms, the system is nonlinear. The vast majority of nonlinear differential equations do not have analytic solutions.
Integrate it numerically. Use odeToVectorField to characterise it as a vector field, and matlabFunction to create an anonymous function from it that the numeric ODE solvers can work with.
k1 = 1.08e-3;
k2 = 1.19e-3;
k3 = 1.54e-3;
syms A(t) R(t) S(t) Y T
ode1 = diff(A) == -k1*A;
ode2 = diff(R) == k1*A - k2*R - k3*R*S;
ode3 = diff(S) == k2*R + k3*R*S;
odes = [ode1, ode2, ode3];
[VF,Subs] = odeToVectorField(odes)
VF = 
Subs = 
odefcn = matlabFunction(VF, 'Vars',{T,Y})
odefcn = function_handle with value:
@(T,Y)[Y(1).*(-1.19e-3)+Y(2).*1.08e-3-Y(1).*Y(3).*1.54e-3;Y(2).*(-1.08e-3);Y(1).*1.19e-3+Y(1).*Y(3).*1.54e-3]
t_interval = [0, 1200];
ic = [0, 0.019, 0];
[t,y] = ode45(odefcn, t_interval, ic);
figure
plot(t, y)
grid
legend(string(Subs))
Experiment to get different results.
.

More Answers (1)

Paul
Paul on 20 Oct 2021
I think the fundamental problem is that dsolve() can't find a solution. Then the error results because it doesn't know how to deal the LHS of the assignment in this case. Use the single variable form for the LHS and no error is shown
clear
t_interval = [0, 1200];
k1 = 1.08e-3;
k2 = 1.19e-3;
k3 = 1.54e-3;
syms A(t) R(t) S(t)
ode1 = diff(A) == -k1*A;
ode2 = diff(R) == k1*A - k2*R - k3*R*S;
ode3 = diff(S) == k2*R + k3*R*S;
odes = [ode1, ode2, ode3];
initialA = A(0) == 0.019;
initialR = R(0) == 0;
initialS = S(0) == 0;
ICs = [initialA, initialR, initialS];
% [Asol(t), Rsol(t), Ssol(t)] = dsolve(odes, ICs);
sol = dsolve(odes,ICs)
Warning: Unable to find symbolic solution.
sol = [ empty sym ]

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by