Symbolic ODEs aren't symbolic?

4 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2021-10-20
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.
.

更多回答(1 个)

Paul
Paul 2021-10-20
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 ]

类别

Help CenterFile Exchange 中查找有关 Equation Solving 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by