Effacer les filtres
Effacer les filtres

How to solve multiple ODEs to fit empirical observations by optimizing multiple constants?

2 vues (au cours des 30 derniers jours)
I have 3 ODEs and 2 parameters to be optimized to fit the ODE's to given data..
eg dA/dt = -(K1+K2)*A;
dB/dt = K1*A;
dC/dt = K2*A
where t= time and K1,K2 are constants
I have been given A,B and C vs time data..I must manipulate K1 and K2 to match the data. How do I go about doing this using optimization toolbox preferably fmincon? Please suggest a sample code if possible..

Réponse acceptée

Teja Muppirala
Teja Muppirala le 21 Août 2012
Below is an example that does exactly what you are describing. Save it into a file and run it. First I just made some sample data, and then I fit your equations to it, getting both K1 and K2, as well as initial conditions on the data.
function fitdata
% True Values
[K1,K2,A0,B0,C0] = deal(3.5,4.2,1,2,3);
[T,Y0] = ode45(@(t,y)[-(K1+K2)*y(1); K1*y(1); K2*y(1)],[0:0.005:1],[A0;B0;C0]);
Ymeas = Y0 + 0.1*randn(size(Y0));
figure;
plot(T,Ymeas);
hold on;
h = plot(T,nan*Ymeas,'k','linewidth',2);
minERR = Inf;
opts = optimset('fminunc');
opts.LargeScale = 'off';
Xest = fminunc(@(X)objfun(X),[0;0;0;0;0],opts);
Xest = num2cell(Xest);
[K1,K2,A0,B0,C0] = deal(Xest{:}),
legend({'A','B','C'});
function ERR = objfun(X);
X = num2cell(X);
[K1,K2,A0,B0,C0] = deal(X{:});
[T,Yest] = ode45(@(t,y)[-(K1+K2)*y(1); K1*y(1); K2*y(1)],[0:0.005:1],[A0;B0;C0]);
ERR = sum((Ymeas(:) - Yest(:)).^2);
if ERR < minERR
minERR = ERR;
for n = 1:3; set(h(n),'Ydata',Yest(:,n)); end
drawnow;
end;
end;
end
  1 commentaire
Nitin Samuel
Nitin Samuel le 21 Août 2012
Modifié(e) : Nitin Samuel le 21 Août 2012
thank u so much!!it works so perfectly! I have to do this so for many more parameters and many more odes..can I use the same logic and generalize??thank you so much again..

Connectez-vous pour commenter.

Plus de réponses (3)

Ryan G
Ryan G le 31 Juil 2012
I'm not sure how you would do this with MATLAB only but simulink design optimization would probably handle this fairly easy.
This demo shows how it can be utilized on a simple model to match a data input.

Bjorn Gustavsson
Bjorn Gustavsson le 31 Juil 2012
If the ODEs are that simple it should just be to integrate them analytically, then you'd simply end up with a well overdetermined least square fitting problem for K1 and K2 (perhaps you'd get A(0), B(0) and C(0) in there as unknowns too).
If the ODEs are a bit more complicated you could try a finite difference aproximation.

Star Strider
Star Strider le 31 Juil 2012
Modifié(e) : Star Strider le 31 Juil 2012
If you are looking for a way to use an ODE solver with an objective function, I have used this strategy:
function Y = objfun(B, t) % Objective function
[T,Ymtx] = ode45(@DifEq, t, x0); % Do the ODE integration
function dY = DifEq(t, x) % Function ode45 integrates DifEq
ydot(1) = . . .;
. . .
ydot(n) = . . .;
dY = ydot
end
Y = Ymtx(:,2); % If Ymtx has more than one column, return the one you want here
end
Note that you do not have to pass the parameter vector B specifically to DifEq, since DifEq can access the B vector since it is part of objfcn.
  2 commentaires
Nitin Samuel
Nitin Samuel le 1 Août 2012
Can you please elaborate?Or can you write a sample code using same variables and parameters mentioned by me above? Im new to matlab and this will be of great help to me.Thanks
Nitin Samuel
Nitin Samuel le 1 Août 2012
Also, In the objective function I have to optimize K1 and K2 using 3 given data sets i.e A,B and C vs Time..I think your code can optimize it matching only 1 variable..

Connectez-vous pour commenter.

Catégories

En savoir plus sur Nonlinear Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by