Curve fit such as f(x) = A + B/x^2

5 views (last 30 days)
Peter Burda
Peter Burda on 10 Oct 2020
Commented: Ameer Hamza on 10 Oct 2020
Hi, my problem is that I need fit curve such as f(x) = A + B/x^2 in matlab any suggestions?
thanks

Answers (3)

Ameer Hamza
Ameer Hamza on 10 Oct 2020
Edited: Ameer Hamza on 10 Oct 2020
If you have optimization toolbox then you can use lsqcurvefit()
x; % x-values
y; % f(x) values
mdl = @(p, x) p(1) + p(2)./x.^2;
sol = lsqcurvefit(mdl, rand(1,2), x, y);
A = sol(1);
B = sol(2);
You can also check fit() from Curve fitting toolbox.
  2 Comments
Peter Burda
Peter Burda on 10 Oct 2020
Edited: Peter Burda on 10 Oct 2020
thanks mate but what does mean rand(1,2)?
Ameer Hamza
Ameer Hamza on 10 Oct 2020
It is just a starting point for the algorithm.

Sign in to comment.


Matt J
Matt J on 10 Oct 2020
Edited: Matt J on 10 Oct 2020
p=polyfit(1./x.^2,y,1);
B=p(1);
A=p(2);

Matt J
Matt J on 10 Oct 2020
Edited: Matt J on 10 Oct 2020
p=((1./x(:)).^[0,2]) \ y(:);
A=p(1);
B=p(2);

Categories

Find more on Linear and Nonlinear Regression 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!