FMINCON Curve fitting

I am trying to use FMINCON to solve for the parameters in a model with two inputs and four unknown parameters. I would like FMINCON to find the set of parameters that minimize the squared residuals of the model predictions to what I have measured. This is analogous to using EXCEL SOLVER to do curve fitting. The model is nonlinear with unknown parameters P and of the form:
F(X,Y) = P1*P2^(X/10)-(P3*P4*Y/(P4+P3*Y)
Does anyone know how to do this? I am lost.

Answers (1)

Sean de Wolski
Sean de Wolski on 13 Oct 2014
Edited: Sean de Wolski on 14 Oct 2014
fmincon is overkill. Use lsqcurvefit which already has the objective function framed for you.
doc lsqcurvefit % for more info
MORE
Here's a full example:
% Something to simulat data
P1act = pi;
P2act = 1;
P3act = exp(1);
P4act = 0.06;
X = rand(100,2); % Simulate X, first column is your "X", second is your "Y"
zz = P1act*P2act.^(X(:,1)/10)-(P3act*P4act.*X(:,2)./(P4act+P3act.*X(:,2)));
% Use the solver
fun = @(P,x)P(1).*P(2).^(x(:,1)/10)-(P(3)*P(4).*x(:,2)./(P(4)+P(3).*x(:,2)));
x0 = [2 1 2 0.1];
p = lsqcurvefit(fun,x0,X,zz)

5 Comments

Matt J
Matt J on 13 Oct 2014
... unless you have complicated constraints that you haven't mentioned...
Adrian
Adrian on 14 Oct 2014
So how would you do it with lsqcurvefit when it only accepts one variable
lsqcurvefit(@func, ydata, xdata)
if i try to put lsqcurvefit(@func, ydata, xdata1, xdata2,) it doesn't work?
See MORE for a full example
xdata is an mxd matrix where d is the number of dimensions, in this case two. So xdata should be your [x, y]
xdata = [X Y]
Ydata is the independent variable (i.e. Z).
Above, I simulated some xdata and ydata and rewrote the function the way the solver expects it. The first input are the coefficients that are being searched for, the second is the xdata.
I also used fmincon for curve fitting problem because I have several linear constraints between the parameters. For examples, x(1) < x(2) < x(3). Is there any way to add such constraints in lsqcurvefit? I saw there is no way to use upper and lower bounds for these constraints. Thanks!
Torsten
Torsten on 28 Nov 2018
Instead of using x(1),x(2),x(3),... use y(1)=x(1), y(2)=x(2)-x(1), y(3)=x(3)-x(2),... as solution parameters and put constraints y(2) >=0, y(3) >= 0,...
Best wishes
Torsten.

Sign in to comment.

Categories

Asked:

on 13 Oct 2014

Commented:

on 28 Nov 2018

Community Treasure Hunt

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

Start Hunting!