Fitting in MatLAB
1 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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 个评论
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 Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!