Which is the most efficient way to solve a ODE that has a parameter that changes in every period?
1 次查看(过去 30 天)
显示 更早的评论
The problem I have to solve is to choose optimally teh parameters of a system of differential equations. The problem is that one of the parameters depends on a function (which are the parameters I need to choose) and the value changes in every period. So, in order to choose the parameters I minimize the square error of the fitted model with some actual data. The problem is that the algorithm is really slow and I need to optimize it. In every step I need to redefine the ODE model in the following way:
syms S(t) I(t) R(t) D(t)
odeS = diff(S) == -beta*I*S/pop;
odeI = diff(I) == beta*I*S/pop - gamma*I-mu*I;
odeR = diff(R) == gamma*I;
odeD = diff(D) == mu*I;
% Transform the equations for the numerical solver
odes = [odeS; odeI; odeR; odeD];
odes2 = odeToVectorField(odes);
eq_mat = matlabFunction(odes2, 'Vars', {'t', 'Y'});
ic = [s0, i0, r0, d0];
tspan = [0, 100];
[t, y]= ode45(eq_mat, tspan, ic);
The problem is that in every period the value of beta changes and i need to run all the latter lines again and that takes time. I have tried other things but are even slower.
0 个评论
采纳的回答
Stephan
2021-4-22
编辑:Stephan
2021-4-22
Are gamma and beta parameters that result from the gamma / beta functions? Or just scalar parameters? I suggest using other names if they are scalars, because Matlab inbuilt functions are called like that, which will produce errors.
Run this part only once - ideally in a seperate script.Save or copy the result into another script and optimize then without the symbolic calculation. Therefore use beta (and maybe the others) as an additional input.
syms S(t) I(t) R(t) D(t) Beta pop Gamma mu
odeS = diff(S) == -Beta*I*S/pop;
odeI = diff(I) == Beta*I*S/pop - Gamma*I-mu*I;
odeR = diff(R) == Gamma*I;
odeD = diff(D) == mu*I;
% Transform the equations for the numerical solver
odes = [odeS; odeI; odeR; odeD];
odes2 = odeToVectorField(odes);
eq_mat = matlabFunction(odes2, 'Vars', {'t', 'Y', 'Beta', 'pop', 'Gamma', 'mu'})
The numerical solution process is fast, but symbolic calculations are not. So this part should be the only one that runs repeated durng the optimization:
eq_mat = @(t,Y,Beta,pop,Gamma,mu)[-Gamma.*Y(1)-mu.*Y(1)+(Beta.*Y(1).*Y(2))./pop;-(Beta.*Y(1).*Y(2))./pop;Gamma.*Y(1);mu.*Y(1)]
s0 = 1;
i0 = -1;
r0 = 1;
d0 = -1;
ic = [s0, i0, r0, d0];
Gamma = 1;
pop = 2;
mu = -1;
Beta = 0.5;
tspan = [0, 100];
[t, y]= ode45(@(t,Y)eq_mat(t,Y,Beta,pop,Gamma,mu), tspan, ic);
plot(t,y)
更多回答(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!