MATLAB - Curve Fitting 1/x Function
Solution 1
Sure, try this.
You can rewrite this equation as: y = c0 + c1*z
where c0 and c1 are the constants you want to solve for and z = 1/x
.
If you have n points, you can write one equation for each pair:
y1 = c0 + c1*z1
y2 = c0 + c1*z2
...
yn = c0 + c1*zn
You've got an (n x 1) vector of known y values on the left hand side. There's an (n x 2) matrix where the first column is all ones and the second column is the known vector of x values that multiplies a (2 x 1) vector of unknown coefficients c0 and c1.
Premultiply both sides by the (2 x n) transpose of the matrix and you'll have two equations for two unknown coefficients that you can solve easily.
Read this for details.
Solution 2
Do you have the optimization toolbox? If so, use the lsqcurvefit function.
a=lsqcurvefit(@(a,x) a(1)./x,1,x,y);
hold on
plot(x,y,'o') %plot data
plot(x,a./x) %fit
nick_name
Updated on June 04, 2022Comments
-
nick_name almost 2 years
I'd like to find a way to fit a curve to a specific functional form, namely:
y=constant/x
Is there a good way to do this? My data is just a set of (x,y) pairs.