Linear regression of multiple points in the picture using the least squares method

zhaozj2021-02-16  100

In daily life and scientific experiments, there is often a set of linear relationships between variable Y and the argument x to set a set of data:

The relationship between Y and X can be represented by a linear equation:

Press the least squares method:

The degree of linear relationship can be represented by the correlation coefficient R

So, if you want to discode the nearest linear line from the known plurality of linear relations in the image box, the code should be utilized, the code is as follows:

Private Sub Command1_Click () Picture1.scale (0, 20) - (12, 0) Set Coordinate Range DIM P (4, 1) As Double, I as Integer I = 0 to 4P (i, 0) = Choose (i 1, 1.2, 3.7, 4.1, 5.1, 8.3) p (i, 1) = choose (i 1, 2.2, 6.4, 7.8, 10.1, 15.8) Next 'Defines five points Drawline Picture1, P' draws Five points of straight line End Subsub Drawline (Byval Pic As Picturebox, Byref P () As Double) DIM Sigmax As Double, Sigmay As Double, Sigmaxx As Double, Sigmaxy AS Double, N AS Integerdim i As Longdim A As Double, B Double 'Intercept Slope DIM X0 As Double, Y0 As Double, X1 As Double, Y1 AS Double' Defines Both End Point N = Ubound (P) - LBound (P) 1 'Point for i = lbound (P) To Ubound (P) Picture1.circle (p (i, 0), p (i, 1)), Picture1.ScaleWidth / 200, VBRED 'Point Picture1.currentx = P (i, 0) Picture1.currenty = P (i , 1) Picture1.ForeColor = VBBLuePicture1.print "(" & P (i, 0) & ","; p (i, 1) & ")" data logo sigmax = sigmax p (i, 0) 'σXSIGMAY = sigmay p (i, 1) 'σysigmaxx = sigmaxx p (i, 0) ^ 2' σx ^ 2Sigmaxy = Sigmaxy P (i, 0) * p (i, 1) 'σX * YNEXT

A = (sigmaxx * sigmay - sigmax * sigmaxy) / (n * sigmaxx - sigmax ^ 2) 'Intercept B = (N * Sigmaxy - Sigmax * Sigmay) / (n * sigmaxx - sigmax ^ 2)' slope X0 = Picture1 .Scalelefty0 = a b * x0 'left end point x1 = Picture1.scaleleft Picture1.scaleWidthy1 = a b * x1' right endpoint Picture1.Line (x0, y0) - (x1, y1), vbgreen 'regular line END SUB

The result is as shown below:

转载请注明原文地址:https://www.9cbs.com/read-11532.html

New Post(0)