Solving an ODE with best-fit adjustment to empirical observations
12 views (last 30 days)
Show older comments
Hi everybody, I want to numerically solve an ODE but I want to fit the constants to experimental data following that ODE. Any simple example out there?
0 Comments
Accepted Answer
Teja Muppirala
on 8 Apr 2011
If I understood correctly, you want to find the parameters for an ODE such that the ODE fits some experimental data. If you have the Optimization Toolbox this is easy to program. You basically put the ODE solver inside the cost function for your optimization
Say your ODE is :
y' = A*y*(B-y)
And you want to find A, B, and y(0). This is some sample code that shows how it's done. Save it and run it.
function xout = do_optimization
% The true parameters
y0_true = 1;
A_true = 2;
B_true = 3;
T = (0:.01:1)';
ytrue =3./(1 + 2*exp(-6*T)) + 0.1*randn(size(T));
hold on;
plot(T,ytrue);
h = plot(T,nan*ytrue,'r');
set(h,'tag','solution');
x0 = [0.4 3.9 1.2]; % Just some Initial Condition
ub = [5 5 5]; % Upper bounds
lb = [0 0 0]; % Lower bounds
F = @(x) COST(x,T,ytrue);
xout = fmincon(F,x0,[],[],[],[],lb,ub); %<-- FMINCON is the optimizer
legend({'Experimental Data','Fitted Data'});
function COST = COST(x,T,ytrue)
y0 = x(1);
A = x(2);
B = x(3);
% The cost function calls the ODE solver.
[tout,yout] = ode45(@dydt,T,y0,[],A,B);
COST = sum((yout - ytrue).^2);
h = findobj('tag','solution');
set(h,'ydata',yout);
title(['y0 = ' num2str(y0) ' ' 'A = ' num2str(A) ' B = ' num2str(B)]);
drawnow;
function yprime = dydt(t,y,A,B)
yprime = A*y*(B-y);
2 Comments
Pooh
on 2 Jul 2011
after I try
T = (0:.1:1)'; ytrue =[1 1.43 1.87 2.25 2.54 2.73 2.84 2.91 2.95 2.97 2.99];
instead of the random ytrue, some how the fitted line dose not show and no answer for t0, A, or B What happen sir?
and ading to that if I have 3 ODEs how do I with 3 unknowns, how do I adjust the code? thank you sir.
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!