Fitting in MatLAB

1 view (last 30 days)
Niles Martinsen
Niles Martinsen on 9 May 2012
Hi
I have a dataset, which I am trying to fit to a function having the form
A*cos(a*x) + B*sin(b*x) + C/x + exp(-D*x).
Fitting in MatLAB has always been difficult for me. Generally I have been able to fit functions "manually" like this:
**************************************************************************************************
t = 0:7 ; rel = [629 537 460 375 334 286 249 227];
fh = @(x,p) p(1) + p(2)*exp(-x./p(3))
errfh = @(p,x,y) sum((y(:)-fh(x(:),p)).^2)
p0 = [mean(rel) (max(rel)-min(rel)) (max(t) - min(t))/2];
P = fminsearch(errfh,p0,[],t,rel)
plot(t,rel,'bo',t,fh(t,P),'r-')
**************************************************************************************************
But I have never been able to get errorbars out with this method + I have run into problems regarding the number of iterations needed for convergence. Can I get a hint to how I would go about fitting my function above to my data set?
Best, Niles.
  2 Comments
Oleg Komarov
Oleg Komarov on 9 May 2012
This is a duplicate of the active thread (2 answers): http://www.mathworks.com/matlabcentral/answers/37920-help-with-fminsearch
Please edit your original question, do not duplicate posts.
Sargondjani
Sargondjani on 9 May 2012
this is actually quite a different question....

Sign in to comment.

Answers (1)

Sargondjani
Sargondjani on 9 May 2012
the problem is that fminsearch is not well suited to optimize for more than a couple of parameters, BUT there are special tools for curve fitting:
lsqcurvefit
lsqnonlin
or if you want something else than least squares, you would need some other algorithm to optimize for more than a couple of variables: if you have the optimization toolbar, you can use fminunc
otherwise im afraid you will have to program an optimizer yourself, like Newton Raphson algorithm
  2 Comments
Niles Martinsen
Niles Martinsen on 9 May 2012
Thanks. In that case, do you know how I would get the uncertainties on the fitted parameters out with the above method?
Sargondjani
Sargondjani on 9 May 2012
i editted my answer: lsqcurvefit and lsqnonlin are sepcial tools. they should be able to do the trick... sorry for confusion (i dont do much curvefitting myself, so i should actually shut up)

Sign in to comment.

Categories

Find more on Interpolation 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!