MATLAB Answers

# Which is the most efficient way to solve a ODE that has a parameter that changes in every period?

1 view (last 30 days)
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 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Stephan on 22 Apr 2021
Edited: Stephan on 22 Apr 2021
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'})
eq_mat = function_handle with value:
@(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)]
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)]
eq_mat = function_handle with value:
@(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) ##### 1 CommentShowHide None
Juan Manuel Escolar on 24 Apr 2021
Thank you very much! The beta is a vector of scalars and gamma is a scalar too. I will try your advice, i also wrote the ODE in discrete terms and that improved drastically the computation time.

Sign in to comment.

R2020b

### Community Treasure Hunt

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

Start Hunting!