Solving an ODE with best-fit adjustment to empirical observations

4 次查看(过去 30 天)
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?

采纳的回答

Teja Muppirala
Teja Muppirala 2011-4-8
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 个评论
Pooh
Pooh 2011-7-2
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by