' comparing errors for exponential, linear, and quadratic least squares fits ' Dim x(9), y(9) ' s = 17108069 m7 = s Mod 7 ' m7 = 6 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 a = 0.785714 b = -12.4429 c = 263.971 'm = 5 ' ------ a = -1.03571 b = -6.25 c = 261.786 ' m = 6 ' ----- a = -0.9762 b = -6.5 c = 261.905 m = 7 ' ----- a = -2.25 b = -5.131 c = 261.083 m = 8 ' ----- a = -1.1905 b = -5.476 c = 261.33 m = 9 error2quadratic = 0 For j = 1 To m error2quadratic = error2quadratic + (y(j) - a * x(j) ^ 2 - b * x(j) - c) ^ 2 Next j MsgBox error2quadratic