Clear Filters
Clear Filters

Fitting implicit function using lsqnonlin

2 views (last 30 days)
Ali
Ali on 21 Dec 2011
Hi,
I am trying to fit an implicit function to data. As an example I tried to fit:
x = ((D - B*log(y/D) - y)/A)+C.
I get it to fit but the coeffs do not converge to the values I generate the data with, even when I set the initial values very close to the ones I used initially. Is there are better way or is this a problem with the large number of local minima that the routine can run into.
Thanks!
%generate random 20 points between 0.5 and 5.5 (b + (a-b)*rand(1,20)) coeff = [] n = 20; y = 0.5+5*rand(1,n);
%set the parameters we want and generate corresponding x values % we now have data to fit to [x,y]
A = 1.5 B = 2.0 C = 3.5 D = 4.5 x = ((D - B*log(y/D) - y)/A)+C
%put some random error on the y values
err = randn(1,n)/100; ye = y + err;
%plot generated data figure; plot(x, ye, '*r'); hold on;
%set up the function
fun = @(p) (((p(4) - p(2)*log(ye/p(4)) - ye)/p(1))+p(3)) - x
% and the options for the least squares non-linear fit
options = optimset('lsqnonlin'); options.Display = 'iter'; options.MaxFunEvals = 10000;
%inital guesses for parameters abstart = [1.5 2.0 3.4 4.5]; coeff = lsqnonlin(fun,abstart,[],[],options)
%coeff = [1.5 2.0 3.5 4.5]; x_fit = ((coeff(4) - coeff(2)*log(ye./coeff(4)) - ye)/coeff(1))+coeff(3); [x_fit I_fit] = sort(x_fit);
ye_fit = ye(I_fit); plot(x_fit, ye_fit,'-ob');

Answers (0)

Categories

Find more on Linear and Nonlinear Regression 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!