Switching from fminsearch to lsqcurvefit

12 views (last 30 days)
Sam
Sam on 7 Dec 2014
Answered: Star Strider on 7 Dec 2014
Okay, I have made the following code that estimates unknown parameters using ode15s and fminsearch. It then plots the experimental data given and the best fit obtained from the model. The model is made of three .m files (the main model file, the objective function file, and the ODE file):
clear all
close all
% Data from problem
tspan = 0:20:360;
data=[0 1
3.05 0.28
2.04 0.18
0.34 0.32
0.63 0.38
0.95 0.46
1.60 0.35
2.27 0.22
1.60 0.20
0.44 0.32
0.75 0.40
1.13 0.39
1.82 0.31
2.20 0.22
0.56 0.22
0.54 0.33
0.86 0.40
1.32 0.37
2.18 0.27];
xdata = data(:,1);
ydata = data(:,2);
% Initial values for state variables
x0=[0 1];
% Parameters
p0=[1.5 4 5 0.02 2.5];
% Minimization algorithm
psolve = fminsearch(@(p) myobjective(tspan,data,p,x0), p0);
% Call myodefcn and minimize according to fminsearch
f = @(t,x) myodefcn(psolve,t,x);
[ts,xs] = ode15s(f, tspan, x0);
% Plot x vs. x-fit
subplot(1,2,1)
plot(tspan,data(:,1),'-', tspan,xs(:,1),'-')
legend('x','xfit')
title('Biochemical Oscillator Model')
xlabel('Time');
ylabel('Protein Concentrates');
% Plot y vs. y-fit
subplot(1,2,2)
plot(tspan,data(:,2),'-.', tspan,xs(:,2),'-.')
legend('y','yfit')
title('Biochemical Oscillator Model')
xlabel('Time');
ylabel('Protein Concentrates')
function SSE = myobjective(td,xd, p, x0)
f = @(t,x) myodefcn(p,t,x);
[ts,xs] = ode15s(f, td, x0);
err = xd - xs;
SSE = sum(sum(err.^2));
function dx= myodefcn(p, t, x)
dx=zeros(2,1);
dx(1) = x(2)-p(1)*x(1)+(p(2)*x(1)^2)/(p(3)+x(1)^2);
dx(2) = p(4)*(1-p(5)*x(1)*x(2));
Now, I want to change the method of minimization by using lsqcurvefit instead of fminsearch. Based on internet browsing, it seems that I will have to change the main.m and objective.m files, but the ode.m should be fine. The thing is, I'm still not sure what the inputs for lsqcurvefit are. What do I need to change for lsqcurvefit to work?

Answers (1)

Star Strider
Star Strider on 7 Dec 2014
I’m referring you to Monod kinetics and curve fitting that does exactly what you want (but with a different ODE, so change it and the solver). It outlines what you need to do.

Community Treasure Hunt

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

Start Hunting!