Which Function Gives a Better Fit?
Let's begin by plotting the two fitting curves and the data on the same set of axes.
>
p0:=plot(data1,style=point, color=green):
p1:=plot(polyFunc(x),x=1790..1850, color=red):
p2:=plot(expFunc(x),x=1790..1850, color=blue):
with(plots):
display([p0,p1,p2]);
How do we decide which fit is better? We are going to use the sum of the squares of the errors as a measure of the goodness of fit over the time period 1790-1850. That is, suppose we start with data points (
), (
), ..., (
) and we fit the data with the function
. Then we will define the
total squared error
over the period to be the sum of the individual squared errors, namely,
. Moreover, one fitting function will be better than another if its total squared error is smaller. Squaring the errors does not allow the signed-errors to cancel one another in the summation; sqaring also has the effect of giving more weight to bigger errors than smaller ones.
Using the sum of squared errors as the criterion, let's display the errors in tabular form and compare them.
>
polyerror:=[seq([data1[i][1],abs(polyFunc(1790+10*(i-1))-data1[i][2]),abs(polyFunc(1790+10*(i-1))-data1[i][2])/data1[i][2]*100],i=1..7)]:
toterrorpoly:=add((polyFunc(1790+10*(i-1))-data1[i][2])^2,i=1..7):
with(math3):
headers:=["year","absolute error","% error"]:
printtable(polyerror,"Cubic Polynomial Fit", headers);
TotalSqrError:=toterrorpoly;
Cubic Polynomial Fit
year absolute error % error
-----------------------------------------------
1790 46.300000 1.178417
1800 107.400000 2.027563
1810 26.100000 .361296
1820 49.200000 .511541
1830 77.700000 .602279
1840 146.900000 .858061
1850 55.300000 .237737
>
experror:=[seq([data1[i][1],abs(expFunc(1790+10*(i-1))-data1[i][2]),abs(expFunc(1790+10*(i-1))-data1[i][2])/data1[i][2]*100],i=1..7)]:
toterrorexp:=add((expFunc(1790+10*(i-1))-data1[i][2])^2,i=1..7):
with(math3):
headers:=["year","absolute error","% error"]:
printtable(experror,"Exponential Fit", headers);
TotalSqrError:=toterrorexp;
Exponential Fit
year absolute error % error
---------------------------------------------------
1790 26.037493 .662700
1800 15.360584 .289987
1810 88.498676 1.225065
1820 33.677210 .350148
1830 27.448430 .212762
1840 171.605470 1.002368
1850 35.115430 .150963
Which fitting function is better? Discuss.
Go back