Fitting in MatLAB
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Oleg Komarov
le 9 Mai 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.
Réponses (1)
Sargondjani
le 9 Mai 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 commentaires
Sargondjani
le 9 Mai 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)
Voir également
Catégories
En savoir plus sur Nonlinear Optimization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!