Error using dsolve in system of first order differential equation

2 次查看(过去 30 天)
Hi I am trying to solve a system of ODEs using matlab but am getting errorrs. Here is my code.
a and b are constants
syms S(t) I(t) R(t) a b
eqns = [diff(S,t)== -a*S*I, diff(I,t)==a*S*I-b*I,diff(R,t)==b*I];
ans = dsolve(eqns)
Warning: Unable to find explicit solution.
> In dsolve (line 201)
then I tried this
ans = dsolve(eqns,'Implicit',true)
and got the following
Error using char
Conversion to char from logical is not possible.
Error in dsolve>mupadDsolve (line 286)
if isvarname(char(args{end}))
Error in dsolve (line 194)
sol = mupadDsolve(args, options);

回答(1 个)

Vaibhav
Vaibhav 2024-5-29
Hi Arjun
It seems you are facing issues with solving a system of ordinary differential equations (ODEs).
Due to the nature of your system, an explicit solution might not be available. In such cases, numerical methods are typically used. Functions like ode45, ode23, and ode113 are suitable for obtaining numerical solutions.
Here is how we can approach it:
% Define constants
a = 0.1; % Example value
b = 0.05; % Example value
% Initial conditions [S0, I0, R0]
y0 = [0.99, 0.01, 0]; % Example initial conditions
% Time span
tspan = [0 100]; % From time 0 to 100
% Solve the system
[t, y] = ode45(@(t, y) SIRModel(t, y, a, b), tspan, y0);
% Plot the results
plot(t, y)
legend('S', 'I', 'R')
xlabel('Time')
ylabel('Population')
title('SIR Model Solution')
function dydt = SIRModel(t, y, a, b)
S = y(1);
I = y(2);
R = y(3);
dydt = [-a*S*I; a*S*I - b*I; b*I];
end
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by