Matlab code for Perturbation Analysis

14 次查看(过去 30 天)
% Parameters for the SIR model beta = 0.3; % Infection rate gamma = 0.1; % Recovery rate mu = 0.05; % Natural birth rate nu = 0.02; % Natural death rate % System of equations syms S(t) I(t) R(t) eps eq1 = diff(S,t) == mu - beta*S*I - mu*S + eps*(mu - beta*S*I - mu*S); eq2 = diff(I,t) == beta*S*I - gamma*I - nu*I + eps*(beta*S*I - gamma*I - nu*I); eq3 = diff(R,t) == gamma*I - mu*R + eps*(gamma*I - mu*R); % Initial conditions S0 = 0.9; I0 = 0.1; R0 = 0;
conditions = [S(0)==S0, I(0)==I0, R(0)==R0];
% Solve the system of equations [SSol, ISol, RSol] = dsolve([eq1, eq2, eq3, conditions]);
% Perturbation terms S_perturb = SSol + eps*dsolve(diff(S,t) == mu - beta*SSol*ISol - mu*SSol); I_perturb = ISol + eps*dsolve(diff(I,t) == beta*SSol*ISol - gamma*ISol - nu*ISol); R_perturb = RSol + eps*dsolve(diff(R,t) == gamma*ISol - mu*RSol);
% Plot the solutions tspan = 0:0.1:100; s = subs(S_perturb, eps, 0.1); i = subs(I_perturb, eps, 0.1); r = subs(R_perturb, eps, 0.1);
figure; plot(tspan, s, 'b', tspan, i, 'r', tspan, r, 'g'); title('Perturbation Analysis of SIR Model'); xlabel('Time'); ylabel('Population'); legend('Susceptible', 'Infected', 'Recovered');
Error in dsolve (line 193) sol = mupadDsolve(args, options);
Error in Perturbation (line 22) S_perturb = SSol + eps*dsolve(diff(S,t) == mu - beta*SSol*ISol - mu*SSol); Please help fix the error.

回答(1 个)

Arnav
Arnav 2024-9-23
Hi,
I understand that you are facing an error while solving the symbolic differential equations using dsolve. This is due to dsolve not being able to find a symbolic solution to the equations.
This can be solved by using a numeric solver like ode45 as:
% Parameters for the SIR model
beta = 0.3; gamma = 0.1; mu = 0.05; nu = 0.02; eps = 0.1;
% Define the system of differential equations
sir_ode = @(t, y, eps) [
mu - beta * y(1) * y(2) - mu * y(1) + eps * (mu - beta * y(1) * y(2) - mu * y(1));
beta * y(1) * y(2) - gamma * y(2) - nu * y(2) + eps * (beta * y(1) * y(2) - gamma * y(2) - nu * y(2));
gamma * y(2) - mu * y(3) + eps * (gamma * y(2) - mu * y(3))
];
% Initial conditions
y0 = [0.9; 0.1; 0]; tspan = [0 100];
% Solve the system using ode45
[t, y] = ode45(@(t, y) sir_ode(t, y, eps), tspan, y0);
You can refer to the below documentation for more information on dsolve function:
Hope it helps!
  1 个评论
Sam Chak
Sam Chak 2024-9-23
I very understand that you wish to provide technical assistance to the original poster (@Sunday) on the S.I.R. model while partially supplying the code for solving the S.I.R. differential equations using dsolve(). However, your code does not address the perturbation analysis as requested by the OP.
You can refer to the documentation below for more information on perturbation analysis:
Hope it helps!

请先登录,再进行评论。

类别

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

标签

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by