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.
답변 (1개)
Sean de Wolski
2014년 10월 13일
편집: Sean de Wolski
2014년 10월 14일
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
Matt J
2014년 10월 13일
... unless you have complicated constraints that you haven't mentioned...
Adrian
2014년 10월 14일
Sean de Wolski
2014년 10월 14일
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.
Hany Ferdinando
2018년 11월 28일
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
2018년 11월 28일
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.
카테고리
도움말 센터 및 File Exchange에서 Choose a Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!