function xout = do_optimization
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];
ub = [5 5 5];
lb = [0 0 0];
F = @(x) COST(x,T,ytrue);
xout = fmincon(F,x0,[],[],[],[],lb,ub);
legend({'Experimental Data','Fitted Data'});
function COST = COST(x,T,ytrue)
y0 = x(1);
A = x(2);
B = x(3);
[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);
Best Answer