Dim x(9), y(9) ' s = 17108069 m7 = s Mod 7 ' m = m7 + 3 ' x(1) = 0 x(2) = 1 x(3) = 2 x(4) = 3 x(5) = 4 x(6) = 5 x(7) = 6 x(8) = 7 x(9) = 8 ' y(1) = 263 y(2) = 254 y(3) = 243 y(4) = 231 y(5) = 228 y(6) = 201 y(7) = 188 y(8) = 162 y(9) = 142 ' 'y(1) = Exp(x(1)) 'y(2) = Exp(x(2)) 'y(3) = Exp(x(3)) 'y(4) = Exp(x(4)) 'y(5) = Exp(x(5)) 'y(6) = Exp(x(6)) 'y(7) = Exp(x(7)) 'y(8) = Exp(x(8)) 'y(9) = Exp(x(9)) ' ' Exponential case: ' xsum = 0 For j = 1 To m xsum = xsum + x(j) Next j ' x2sum = 0 For j = 1 To m x2sum = x2sum + (x(j)) ^ 2 Next j ' xlogysum = 0 For j = 1 To m xlogysum = xlogysum + x(j) * Log(y(j)) Next j ' ylogsum = 0 For j = 1 To m ylogsum = ylogsum + Log(y(j)) Next j ' a = (ylogsum * x2sum - xsum * xlogysum) / (m * x2sum - xsum ^ 2) b = (m * xlogysum - xsum * ylogsum) / (m * x2sum - xsum ^ 2) ' 'MsgBox a ABIG = Exp(a) 'MsgBox ABIG 'MsgBox b error2 = 0 For j = 1 To m error2 = error2 + (y(j) - ABIG * Exp(b * x(j))) ^ 2 Next j 'MsgBox error2 p2200 = ABIG * Exp(b * 19) 'MsgBox p2200 ' http://mathworld.wolfram.com/LeastSquaresFittingExponential.html ' https://en.wikipedia.org/wiki/List_of_countries_by_future_population_(United_Nations,_low_fertility_variant) ' Linear case: sx = 0 For j = 1 To m sx = sx + x(j) Next j sy = 0 For j = 1 To m sy = sy + y(j) Next j sxy = 0 For j = 1 To m sxy = sxy + x(j) * y(j) Next j sx2 = 0 For j = 1 To m sx2 = sx2 + x(j) ^ 2 Next j g = (m * sxy - sx * sy) / (m * sx2 - sx ^ 2) i = (sy - g * sx) / m 'MsgBox g 'MsgBox i error2linear = 0 For j = 1 To m error2linear = error2linear + (y(j) - g * x(j) - i) ^ 2 Next j di = Abs(error2linear - error2) 'MsgBox di 'MsgBox error2linear ' Quadratic case: ' https://www.azdhs.gov/documents/preparedness/state-laboratory/lab-licensure-certification/technical-resources/calibration-training/12-quadratic-least-squares-regression-calib.pdf m = 3 y(1) = 0 y(2) = 1 y(3) = 4 sx = 0 For j = 1 To m sx = sx + x(j) Next j sx2 = 0 For j = 1 To m sx2 = sx2 + x(j) ^ 2 Next j xx = sx2 - sx ^ 2 / m sy = 0 For j = 1 To m sy = sy + y(j) Next j sxy = 0 For j = 1 To m sxy = sxy + x(j) * y(j) Next j xy = sxy - sx * sy / m sx3 = 0 For j = 1 To m sx3 = sx3 + x(j) ^ 3 Next j xx2 = sx3 - sx * x2 / m sx2y = 0 For j = 1 To m sx2y = sx2y + x(j) ^ 2 * y(j) Next j x2y = sx2y - sx2 * sy / m sx4 = 0 For j = 1 To m sx4 = sx4 + x(j) ^ 4 Next j x2x2 = sx4 - sx2 ^ 2 / m a = (x2y * xx - xy * xx2) / (xx * x2x2 - xx2 ^ 2) MsgBox a b = (xy * x2x2 - x2y * xx2) / (xx * x2x2 - xx2 ^ 2) MsgBox b c = sy / m - b * sx / m - a * sx2 / m MsgBox c