curvefitting using Fmincon is not accurate please help
1 view (last 30 days)
Show older comments
I have used the fmincon to do curve fitting and here is the below code :
'y ranges between 0.02 please see figure'
xdata =t;% t renges from 0 to 3000
pot_diff = abs(max(y) - min(y));
F = @(x) sum((x(1)*(exp(-xdata/x(2)))+(pot_diff-x(1))*(exp(-xdata/x(3)))-y).^2);
% F = @(x) sqrt(mean(((x(1)*(exp(-xdata/x(2)))+(pot_diff-x(1))*(exp(-xdata/x(3)))-y).^2)));
x0 = [0.01 2000 1000];
A = [];
b = [];
Aeq = [];
beq = [];
% lb = [0 0 0 ];
% ub =[0.6*pot_diff inf inf ];
lb = [0.2*pot_diff 4000 1000];
ub =[0.6*pot_diff 10000 7000];
options = optimoptions('fmincon');
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'MaxFunEvals', 10000);
options = optimoptions(options,'MaxIter', 30000);
options = optimoptions(options,'TolFun', 1e-10);
options = optimoptions(options,'TolX', 1e-6);
options = optimoptions(options,'TolCon', 1e-6);
options = optimoptions(options,'Algorithm', 'interior-point');
options = optimoptions(options,'FinDiffType', 'central');
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(F,x0,A,b,Aeq,beq,lb,ub,@my_nlincon,options);
D = @(x,xdat) x(1)*(exp(-xdata/x(2)))+(pot_diff-x(1))*(exp(-xdata/x(3)));
xdat = matname(Tstart:endT);
xdat = (xdat-xdat(1))';
y_p = D(x,xdat);
%%%%%%%%%%%%%%%
function [C,Ceq] = my_nlincon(x)
pot_diff =evalin('base','pot_diff');
% C(1) = x(1)-x(3);
C = x(3)-x(2);
% Ceq =x(3)+x(1);
Ceq =[];
return
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166450/image.png)
The problem now is the estimated parameters fall in expected boundary but the curve fit is not so good. Please help me if any modification to my logic is required. I already tried lsqcurvefit but it just did not work because of boundary constraints.
3 Comments
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!