
how to change the coefficient of system of ODE with respect to time ?
4 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!