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!