How to constrain the lower and upper bounds in lsqcurvefit?
8 次查看(过去 30 天)
显示 更早的评论
Hi,
For a data set (x, y), I am trying to fit a function f(m, x) using lsqcurvefit. I write as follow:
x=xdata;
y=ydata;
fun=f(m,x);
m0=[m01; m02; m03]; %these are real numbers
lb=[0; 0; 0];
ub=[ub1; ub2; ub3]; %these are real numbers
m=lsqcurvefit(fun, m0, xdata, ydata, lb, ub);
However, sometimes it obeys the bounds and sometimes it does not while fitting. Could anyone please help me if there is any way of constraining the bounds?
Thank you very much in advance!
Kind regards,
Arun
采纳的回答
Matt J
2019-1-2
编辑:Matt J
2019-1-2
This seems to work okay,
for i=1:2
[xdata,is]=sort(realpart(:, i));
ydata=imaginarypart(is, i);
fun=@(m,xdata) ((((m(2)-m(1)).*tan(pi.*m(3)/2))/2)+((sqrt((((m(2)-m(1)).*tan(pi.*m(3)/2)).^2)-(4.*((xdata.*xdata)-(xdata.*(m(1)+m(2)))+(((m(1)+m(2)).^2)/4)-(((m(2)-m(1)).^2)/(4.*((sin(pi.*(m(3)-1)/2)).^2)))+(((m(2)-m(1)).^2).*((tan(pi.*m(3)/2)).^2)/4)))))/2));
m0=[0.45; 0.1815; 0.0735];
lb=[0; 0; 0];
ub=[2, 0.75, 0.25];
[m,resnorm,residual,exitflag,stats]=lsqcurvefit(@(m,xd) abs(fun(m,xd)), m0, xdata, ydata, lb, ub);
plot(xdata, ydata, 'o', 'linewidth', 1.5); hold on;
plot(xdata, fun(m, xdata), '-', 'linewidth', 1.5);
output(i, 1:3)=m(1:3, 1);
end
output =
0.3522 0.0000 0.1771
0.3676 0.0239 0.1444

更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!