Use the least squares method to fit the power function y=a*x^b and the exponential function y=b*exp(a)

WBOY
Release: 2024-01-16 16:51:11
forward
1127 people have browsed it

运用最小二乘法分别进行乘幂函数 y a x^b指数函数y b expa

Use the least squares method to multiply the power functions y a x^b exponential function y b expa xqu

x=[0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43];

y=[0.211 0.313 0.466 0.692 1.03 1.532 2.190 3.250 4.823 7.158];

Fit the first one:

function f = first(c, x, y)

f = y - c(1) .* x .^ c(2);

Save as first.m file.

Run from the command line:

c = lsqnonlin('first', [0 0], [], [], [], x, y);

a = c(1)

b = c(2)

Fitting the second one:

function f = first2(c, x, y)

f = y - c(2) .* exp(c(1) .* x);

Save as first2.m file.

Run from the command line:

c2 = lsqnonlin('first2', [0 0], [], [], [], x, y);

a2 = c2(1)

b2 = c2(2)

How to use matlab for least squares fitting

Use the polyfit function (for polynomial fitting, the least squares method is used)

for example

x=[90 91 92 93 94 95 96];

z=[70 122 144 152 174 196 202];

a=polyfit(x,z,1)

result:

a =

1.0e 03 *

0.0205 -1.7551

1 represents a polynomial of degree 1 (it is a straight line when it is degree 1, applicable to your situation)

a is the coefficient vector of the polynomial, which is arranged from high-order terms to low-order terms,

If you want to use the result, for example, you want to know what z is equal to when x=97

Then there are two methods,

Use the coefficient directly

>>a(1)*97 a(2)

ans =

233.4286

Or use polyval function

>>polyval(a,97)

ans =

233.4286

Application of least squares method? ?

The least squares method is a mathematical optimization technique that finds the best functional match for a set of data by minimizing the sum of squared errors.

The least squares method is to use the simplest method to obtain some absolutely unknowable true values, while minimizing the sum of squared errors.

The least squares method is commonly used for curve fitting. Many other optimization problems can also be expressed in least squares form by minimizing energy or maximizing entropy.

For example, let’s start with the simplest linear function y=kx b

It is known that there are some points (1.1, 2.0), (2.1, 3.2), (3, 4.0), (4, 6), (5.1, 6.0) on the coordinate axis. The linear function relationship of the image passing through these points Mode.

Of course, this straight line cannot pass through every point. We only need to minimize the sum of the squares of the distances from the five points to this straight line. This requires the use of the idea of the least squares method. Then use linear Fitting. It’s a lot to talk about. Since you only asked about the least squares method, I will talk about this much.

This is something only learned in college, and is generally used for modeling.

The above is the detailed content of Use the least squares method to fit the power function y=a*x^b and the exponential function y=b*exp(a). For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!