How to constrain the lower and upper bounds in lsqcurvefit?
    7 views (last 30 days)
  
       Show older comments
    
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 Comments
Accepted Answer
  Matt J
      
      
 on 2 Jan 2019
        
      Edited: Matt J
      
      
 on 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

More Answers (0)
See Also
Categories
				Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!