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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!