how to change the coefficient of system of ODE with respect to time ?

4 ビュー (過去 30 日間)
Deva Narayanan
Deva Narayanan 2020 年 9 月 10 日
コメント済み: Star Strider 2020 年 9 月 11 日
i recently found a matlab code in https://in.mathworks.com/. i have enclosed that code in this section. in this code, i want to change the coefficient value (transm and recov) with respect to time as given in excel file . Is it possible to change this code with respect to time ?
% Just some start parameters and coefficients
Istart = 0.01;
Sstart = 0.99;
Rstart = 0;
transm = 3.2;
recov = 0.23;
maxT = 20;
% define S',I',R'
% S' = - transm * S * I
% I' = transm * S I - recov * I
% R' = recov * I
% S is y(1)
% I is y(2)
% R is y(3)
%so here fun = @(t,y)[S'; I'; R'];
fun = @(t,y)[-transm*y(1)*y(2); (transm*y(1)*y(2))-(recov*y(2)); recov*y(2)];
% Provide the starting parameters
Y0 = [Sstart; Istart; Rstart;];
% Define the range of t
tspan = [0 maxT];
% Magic happens and matrix Y contains S,I,R
[T,Y] = ode45(fun,tspan,Y0);
% Plot plot
figure
plot(T,abs(Y(:,1)),'b-') % S
hold on
plot(T,abs(Y(:,2)),'r-') % I
plot(T,abs(Y(:,3)),'g-') % R

採用された回答

Star Strider
Star Strider 2020 年 9 月 10 日
Interpolate from the Excel file, and choose a stiff solver:
% Just some start parameters and coefficients
Istart = 0.01;
Sstart = 0.99;
Rstart = 0;
maxT = 20;
T1 = readtable('data.xls');
interptransm = @(t) interp1(T1.t, T1.transm, t, 'linear','extrap'); % Interpolate
interprecov = @(t) interp1(T1.t, T1.recov, t, 'linear','extrap'); % Interpolate
%so here fun = @(t,y)[S'; I'; R'];
fun = @(t,y)[-interptransm(t)*y(1)*y(2); (interptransm(t)*y(1)*y(2))-(interprecov(t)*y(2)); interprecov(t)*y(2)];
% Provide the starting parameters
Y0 = [Sstart; Istart; Rstart;];
% Define the range of t
tspan = [0 maxT];
% Magic happens and matrix Y contains S,I,R
[T,Y] = ode15s(fun,tspan,Y0);
% Plot plot
figure
semilogy(T,abs(Y(:,1)),'b-') % S
hold on
plot(T,abs(Y(:,2)),'r-') % I
plot(T,abs(Y(:,3)),'g-') % R
hold off
grid
legend('S', 'I', 'R', 'Location', 'N')
producing:
.
  2 件のコメント
Deva Narayanan
Deva Narayanan 2020 年 9 月 11 日
@ Star Strider; Thank u so much
Star Strider
Star Strider 2020 年 9 月 11 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by