Linear Fitting: for the form of Y = A * x + B
A = (N * Σ Xi * Yi-Σ Xi * Σ Yi)/(n * Σ Xi * Xi-(Σ xi) 2)
B = (Σ Xi * xi) * (Σ Yi)-(Σ xi) * (Σ Xi * Yi)/(n * Σ Xi * Xi-(Σ XI) 2)
MATLAB built-in functions can be used to achieve:
Fitting Function: Pn = polyfit (X, Y, n) returns the PN coefficient vector, descending order, and N is the order.
Function: yy = polyval (Pn, x) PN is a polynomial coefficient in descending order, X is a vector or matrix, and returns YY: vector or matrix calculated after bringing X into Pn.
% Initial data x = [1 2 3 4]; y = [1.1 2.2 2.7 3.8]; % obtain linear fitting coefficient Pn = polyfit (X, Y, 1 ); YY = polyval (Pn, x); subplot (1, 2); plot (x, y); subplot (1, 2); plot (x, YY );
Result: Pn = 0.86, 0.3
This article from the "useless uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1616957
Linear regression using Matlab (linear fitting)