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)
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
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!