Use C language to implement the least square algorithm and least square algorithm
Use C language to implement the least square algorithm
For more information, see http://blog.csdn.net/jdh99, :jdh.
Environment:
HOST: WIN8
Development Environment: MINGW
Note:
Refer to Wikipedia's least square method:
Example of a warship in the test text: predicting the width with the length of the warship
Example of simple linear model y = b0 + b1t [edit]
Randomly select 10 warships and analyze their length and width to find the relationship between their length and width. The following figure shows that the length (t) of a warship is linearly related to the width (y. The scatter chart is as follows:
The table lists the data of each warship. The subsequent step is to use the least square method to determine the linear relationship between the two variables.
No. |
Length (m) |
Width (m) |
Ti-t |
Yi-y |
|
|
|
I |
Ti |
Yi |
Ti * |
Yi * |
Ti * yi * |
Ti * |
Yi * |
1 |
208 |
21.6 |
40.2 |
3.19 |
128.238 |
1616.04 |
10.1761 |
2 |
152 |
15.5 |
-15.8 |
-2.91 |
45.978 |
249.64 |
8.4681 |
3 |
113 |
10.4 |
-54.8 |
-8.01 |
438.948 |
3003.04 |
64.1601 |
4 |
227 |
31.0 |
59.2 |
12.59 |
745.328 |
3504.64 |
158.5081 |
5 |
137 |
13.0 |
-30.8 |
-5.41 |
166.628 |
948.64 |
29.2681 |
6 |
238 |
32.4 |
70.2 |
13.99 |
982.098 |
4928.04 |
195.7201 |
7 |
178 |
19.0 |
10.2 |
0.59 |
6.018 |
104.04 |
0.3481 |
8 |
104 |
10.4 |
-63.8 |
-8.01 |
511.038 |
4070.44 |
64.1601 |
9 |
191 |
19.0 |
23.2 |
0.59 |
13.688 |
538.24 |
0.3481 |
10 |
130 |
11.8 |
-37.8 |
-6.61 |
249.858 |
1428.84 |
43.6921 |
Sum (Σ) |
1678 |
184.1 |
0.0 |
0.00 |
3287.820 |
20391.60 |
574.8490 |
Follow the example above.
And get the corresponding.
Then confirm b1
It can be seen that every 1 m change in the warship length, the corresponding width will change 16 cm. And obtain the constant item b0 from the following formula:
The random theory is not described here. We can see that the point fitting is very good, and the correlation between length and width is about 96.03%. Use Matlab to obtain fitting Straight
Line:
Algorithm result:
Mean_x = 167.800003, mean_y = 18.410000
A =-8.645075, B = 0.161234
The predicted width of a 39.725140 M warship is
Source code:
# Include <stdio. h> /************************************* ****** **************************************** ************************//************** **************************************** ********************* **************************************** * ********/# define LEN100 /************************** **************************************** ********************************** ************************************//** **************************************** ********** **************************************** * *****************/struct _ Data_Unit {float x; float y ;}; /*************************************** * ****************************** data fifo ******* **************************************** * ********************/struct _ Data {struct _ Data_Unit data [LEN]; int len ;}; /*************************************** * ****************************** global variables ******* **************************************** ***********************//*************** **************************************** *********************** **************************************** * *****/struct _ Data = {. len = 0 }; /*************************************** ******** **************************************** **********************//**************** **************************************** * ************** press data * parameter: x: measurement data x * y: measurement Data y ************************************* * *******************************/void push (float x, float y) {int I = 0; if (Data. len <LEN) {Data. data [Data. len]. x = x; Data. data [Data. len ++]. y = y; return;} // move Data, remove the last Data for (I = 0; I <LEN-1; I ++) {Data. data [I]. x = Data. data [I + 1]. x; Data. data [I]. y = Data. data [I + 1]. y;} Data. data [LEN]. x = x; Data. data [LEN]. y = y; Data. len = LEN ;} /*************************************** * ****************************** calculate the valuation * fit curve y = * x + B * parameters: x: x value of the number to be evaluated * return: valuation y ************************************** * ******************************/float calc (float x) {int I = 0; float mean_x = 0; float mean_y = 0; float num1 = 0; float num2 = 0; float a = 0; float B = 0; // calculate the mean of t and y for (I = 0; I <Data. len; I ++) {mean_x + = Data. data [I]. x; mean_y + = Data. data [I]. y;} mean_x/= Data. len; mean_y/= Data. len; printf ("mean_x = % f, mean_y = % f \ n", mean_x, mean_y); for (I = 0; I <Data. len; I ++) {num1 + = (Data. data [I]. x-mean_x) * (Data. data [I]. y-mean_y); num2 + = (Data. data [I]. x-mean_x) * (Data. data [I]. x-mean_x);} B = num1/num2; a = mean_y-B * mean_x; printf ("a = % f, B = % f \ n",, b); return (a + B * x);} int main () {float length [10] = {208,152,113,227,137,238,178,104,191,130}; float width [10] = {21.6, 15.5, 10.4, 31.0, 13.0, 32.4, 19.0, 10.4, 19.0}; int I = 0; for (I = 0; I <10; I ++) {push (length [I], width [I]);} printf ("the predicted width of a 300 m warship is % f m \ n", calc )); getchar (); return 0 ;}