How to solve multiple ODEs to fit empirical observations by optimizing multiple constants?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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..
0 commentaires
Réponse acceptée
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
Plus de réponses (3)
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.
0 commentaires
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.
0 commentaires
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
Voir également
Catégories
En savoir plus sur Mathematics and 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!