lsqcurvefit vs lsqnonlin - lsqnonlin wouldn't work...

Hello,
I have a nonlinear fitting question. I can use lsqcurvefit or lsqnonlin in the two methods described below, where lsqcurvefit is a LOT slower but works, yet lsqnonlin deviates at small xdata. Could anyone please take a look and see if there's anything wrong with my lsqnonlin method?
Any suggestion on improving the speed of computation is greatly appreciated too! Thanks!
XR
Goal: Fit [xdata,ydata] to function Y = Fun(X,Y) and obtain 5 parameters -> size(params) = 5,1
Method 1-lsqcurvefit (works):
[params,resnorm] = lsqcurvefit(@Fun1,params0,xdata,ydata,lb,ub,options);
where Fun1 is a function that uses fzero to find the root Y of the expression Y-Fun(X,Y) at all given X and params i.e.,
function Y = Fun1(params,xdata)
f = inline('Y-Fun(X,Y)','','','',...);
for i = 1:length(xdata)
Y(i) = fzero(f,y0,params,xdata(i));
end
end
Method 2-lsqnonlin (doesn't work...):
Redefine a new function Fun2 = Y-Fun(X,Y);
Use data = [xdata ydata] together as input
fit_fun = @(params)Fun2(params,data)
[params,resnorm] = lsqnonlin(fit_fun,params0,lb,ub,options);

1 Comment

maybe a more detailed example is in order, I don't understand your goal.
Depending on the length of xdata in your first method, you might try preallocating Y to speed things up

Sign in to comment.

Answers (0)

Asked:

on 26 Mar 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!