Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
close all
clear
* to get Y=aX +b
Xavg = 0 && mean of X values
Yavg = 0 && mean of Y values
XYprod = 0 && mean of products of X and Y
Xsqr = 0 && mean of X values squared
Ysqr = 0 && mean of Y values squared
mcount = 0 && number of values
aValue = 0 && will be the 'a' part of the equation
bValue = 0 && will be the 'b' part of the equation
use linear
go top
* process the table (could be an array)
do while .not. eof()
* inc the counter
mcount = mcount +1
** sum in the values required
Xavg = Xavg + X
Yavg = Yavg + Y
XYprod = XYprod + (X*Y)
Xsqr = Xsqr + (X*X)
Ysqr = Ysqr + (Y*Y)
* get the next record
skip
enddo
*assuming there is some data to work with
if mcount <> 0
** finish calculating the means by dividing by the number of entries processed
Xavg = Xavg / mcount
Yavg = Yavg / mcount
XYprod = XYprod / mcount
Xsqr = Xsqr / mcount
Ysqr = Ysqr / mcount
** apply the 'least squares' approach to get the two values
aValue = (XYprod - (Xavg * Yavg)) / (Xsqr - (Xavg * Xavg))
bValue = (Yavg - (aValue * Xavg))
endif
? "A = ", aValue
? "B = ", bValue
DECLARE XCoords[3],YCoords[3]
XCoords[1] = 1000000
XCoords[2] = 2
XCoords[3] = 3
YCoords[1] = 1000000
YCoords[2] = 1600000
YCoords[3] = 1750000
? ExtraPol(XCoords,YCoords,4)
FUNCTION Extrapol
PARAMETERS arryX,arryY,NewXValue
PRIVATE i,NoElem,RetValue
PRIVATE Xavg && mean of X values
PRIVATE Yavg && mean of Y values
PRIVATE XYprod && mean of products of X and Y
PRIVATE Xsqr && mean of X values squared
PRIVATE Ysqr && mean of Y values squared
PRIVATE mcount && number of values
PRIVATE aValue && will be the 'a' part of the equation
PRIVATE bValue && will be the 'b' part of the equation
* to get Y=aX +b
Xavg = 0 && mean of X values
Yavg = 0 && mean of Y values
XYprod = 0 && mean of products of X and Y
Xsqr = 0 && mean of X values squared
Ysqr = 0 && mean of Y values squared
aValue = 0 && will be the 'a' part of the equation
bValue = 0 && will be the 'b' part of the equation
retValue = 0
noElem = LEN(arryX)
FOR i = 1 TO noElem
** sum in the values required
Xavg = Xavg + arryX[i]
Yavg = Yavg + arryY[i]
XYprod = XYprod + (arryX[i]*arryY[i])
Xsqr = Xsqr + (arryX[i]*arryX[i])
Ysqr = Ysqr + (arryY[i]*arryY[i])
NEXT
*assuming there is some data to work with
IF noElem <> 0
** finish calculating the means by dividing by the number of entries processed
Xavg = Xavg / noElem
Yavg = Yavg / noElem
XYprod = XYprod / noElem
Xsqr = Xsqr / noElem
Ysqr = Ysqr / noElem
** apply the 'least squares' approach to get the two values
aValue = (XYprod - (Xavg * Yavg)) / (Xsqr - (Xavg * Xavg))
bValue = (Yavg - (aValue * Xavg))
** now calculate the y value for the new X
retValue = (aValue * NewXValue) + bValue
ENDIF
RETURN(retValue)