In this paper, the theory and proof of least square method and the calculation process are introduced, and the program code of two least squares is given.
Octave Code
Clear All;close all;% fitted Data set x = [2;6;9;13]; y = [4;8;12;21];% Data lengthN = length (x);% 3Percent Count x mean m_x = SUM (x)/N%%% calculates the average of t m_t = SUM (y)/N%%% calculates the average of t*x m_xt = SUM (y.*x)/N%%% calculates the average of x squared m_xx = SUM (x.*x)/NPercent percent calculates the value of M in F (x;m,c) =mx+c according to the formula W_1 = (m_xt-m_x*m_t)/(m_xx-m_x^2);The value of C in F (x;m,c) =mx+c is calculated as W_0 = m_t-w_1*m_x;Percent-percent plots the point figure of the DataSet on the artboard (1); Hold off plot (x, Y,' Bo ', ' markersize ', 5, ' linewidth ', 2) Set (Gca, ' XTick ', 0: 1:25) % canvas size 25*25 xplot = [0 25]; Yplot = [0 25]; Xlim (xplot) Ylim (Yplot) Hold on % prints out the fitted segment plot (Xplot,w_0+w_1*xplot,2) Set (Gca, ' Ytick ', 0:< Span class= "Hljs-number" >1:150) xlabel ( ' x '); Ylabel (
Run results
Article starting address: Solinx
http://www.solinx.co/archives/717
Linear regression--Example of least squares _ (I.)