简体   繁体   中英

MATLAB - Curve Fitting 1/x Function

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.

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 (nx 1) vector of known y values on the left hand side. There's an (nx 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 xn) transpose of the matrix and you'll have two equations for two unknown coefficients that you can solve easily.

Read this for details.

No reason to use anything more sophisticated than backslash, although the code inside backslash is quite sophisticated.

constant = (1./x(:))\y(:);

This does the linear regression for a model of the form y=constant/x. See that I inverted the elements of x using ./ and then applied backslash for the regression.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM