Fitting in MatLAB

1 回表示 (過去 30 日間)
Niles Martinsen
Niles Martinsen 2012 年 5 月 9 日
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 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 5 月 9 日
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 2012 年 5 月 9 日
this is actually quite a different question....

サインインしてコメントする。

回答 (1 件)

Sargondjani
Sargondjani 2012 年 5 月 9 日
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 件のコメント
Niles Martinsen
Niles Martinsen 2012 年 5 月 9 日
Thanks. In that case, do you know how I would get the uncertainties on the fitted parameters out with the above method?
Sargondjani
Sargondjani 2012 年 5 月 9 日
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)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by