How to constrain the lower and upper bounds in lsqcurvefit?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Arun Kumar Bar
am 29 Dez. 2018
Kommentiert: Arun Kumar Bar
am 3 Jan. 2019
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
7 Kommentare
Matt J
am 2 Jan. 2019
Bearbeitet: Matt J
am 2 Jan. 2019
I don't get negative values when I run this code. I get complex ones,
output =
0.3528 + 0.0001i -3.5428 - 1.5825i 0.2693 + 0.0047i
0.3698 + 0.0000i -0.0523 - 0.0029i 0.2063 + 0.0017i
Your function is not real-valued at certain m in the search space.
Akzeptierte Antwort
Matt J
am 2 Jan. 2019
Bearbeitet: Matt J
am 2 Jan. 2019
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

2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!