How to solve multiple ODEs to fit empirical observations by optimizing multiple constants?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Teja Muppirala
am 21 Aug. 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 Kommentar
Weitere Antworten (3)
Ryan G
am 31 Jul. 2012
I'm not sure how you would do this with MATLAB only but simulink design optimization would probably handle this fairly easy.
0 Kommentare
Bjorn Gustavsson
am 31 Jul. 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 Kommentare
Star Strider
am 31 Jul. 2012
Bearbeitet: Star Strider
am 31 Jul. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Mathematics and Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!