Linear fitting with least squares method and its MATLAB implementation

Source: Internet
Author: User
  least squares, usually used in our known mathematical model, but do not know the model parameters of the case, through the measured data to calculate the mathematical model, for example, in the problem, the mathematical model is the linear equation y=ax+b, but do not know the linear equation of a and B.
    Originally, we only need two groups (Xi,yi), we can solve A and B, but because of the measured data there are errors, so it is easy to think of a way, we test a lot of data to make my A and B more accurate.
    "We measure a lot of data to make my A and B more accurate," then how do I express this from a mathematical perspective?     For example, in this example, the known mathematical model y=ax+b      We have a lot of data, then we need to find a straight line, so that each data we measured, to the line of the sum of the deviation of the minimum.
	(This sentence is a bit of a mouthful, slowly understand)     So how to use mathematics to describe the "sum of the minimum amount of deviation" concept. The mathematician applied the variance. 
    Mathematical model y=ax+b     set f=ax+b-y     Then for the point on the model (note the point on the model, which is the theoretical value), f=ax+b-y=0     but For the actual value, the f=axi+b-yi must not be equal to 0.
Then we will find a pair of a and B, so that f as close as possible to 0.
    That is, the concept of "least amount of deviation" is mathematically required to minimize the variance of F.     i.e. σf^2→0 (f squared and approaching 0)     Σ (axi+b-yi) ^2→0     So we get an equation F (A, B) =σ (Axi+b-yi) ^2, we're going to find the right
	A, B makes F (A, b) the smallest. In other words, what we are going to find is actually the minimum point of F (A, B). (because the variance cannot be less than 0) so we need to find the extreme point of F (A, B).
	We use the mathematical tools to bias the guide.
	If there is a group, a, B makes ∂f (A, b)/∂a=0    ∂f (A, b)/∂b=0     then f (A, B) is the extremum point, and if A and B have only one pair, then it is the minimum point.  i.e. ∂ (σ (axi+b-yi) ^2)/∂a=0          ∂ (σ (axi+b-yi) ^2)/∂b=0 simplification get             a*σxi^2 + b*σxi =σ (xi*yi)             A*ΣXI + b*n =σyi     where N is the number of (Xi,yi). That is, we have measured the number of data       solution above the two-dollar equation, we can get the only group of A, B, which is what we need a and a o~       O (∩_∩) is not quite simple.
The most basic program of MATLAB is as follows:
% raw Data
x=[163     123      123     141];
y=[186     126     172     148];
n=5;                % of altogether 5 variables

x2=sum (x.^2);       % for σ (xi^2)
x1=sum (X);          % for Σ (xi)
x1y1=sum (x.*y);     % for σ (xi*yi)
y1=sum (Y);          % seeking σ (Yi)

a= (n*x1y1-x1*y1)/(N*X2-X1*X1);      % solved straight line slope b= (y1-a*x1)/n
b= (y1-a*x1)/n;                      % out of line intercept
%
plot% first the original data points are plotted with a blue Cross
(x, y, ' + ');      
Hold
on% draw the fitted straight line
Px=linspace (120,165,45) in red;% here the line interval according to own actual demand rewrites
py=a*px+b;
Plot (px,py, ' R ');
Results a=1.5555  b=-66.365

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.