Concepts of True Error: True Error, Absolute True Error, Relative True Error, and Absolute Relative True Error

Sean Rodby, Luke Snyder, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu
Website: http://numericalmethods.eng.usf.edu
Version: Mathematica 6; Last Revised: August 28, 2008

Initialization

Clearing the definitions of all symbols in the current context:

ClearAll[Evaluate[Context[]<>"*"]]

Introduction

The following worksheet demonstrates how to calculate different definitions related to true error, such as true error, absolute true error, relative true error, and absolute relative true error. The concept is demonstrated using an example of a Maclaurin series. The user will choose which function to perform the calculation for in the Input section of the program. The choices are given as 1 for (e^x), 2 for sin(x) , and 3 for cos(x). The true value of these functions will be assumed as given by the Mathematica commands for these functions.

Section 1: Input Data

This is the only section where the user interacts with the program.

Pick the function of your desire by choosing an integer: 1 for (e^x); 2 for sin(x); 3 for cos(x)

funcchoice=1;

Maximum number of terms to use in the Maclaurin series

n=15;

Value of x at which the function is calculated

xv=1.6;

This is the end of the user section. All information must be entered before proceeding to the next
section. RE-EVALUATE THE NOTEBOOK.

Section 2: Procedure

First, determine which function will be used in the calculations, based on the user input. Once the function is determined, the value is calculated using a Maclaurin series in a repetitive loop.

sumprevious=0;
If[funcchoice==1,
Do[(Subscript[sumpresent, i])=sumprevious+((xv^(i-1))/((i-1)!));
sumprevious=(Subscript[sumpresent, i]),
{i,1,n}]; 
f[x_]:=N[E^x]];
If[funcchoice==2,
Do[(Subscript[sumpresent, i])=sumprevious+(((((-1))^(i-1))*(xv^(2*i-1)))/((2*i-1)!));
sumprevious=(Subscript[sumpresent, i]),
{i,1,n}]; 
f[x_]:=N[Sin[x]]];
If[funcchoice==3,
Do[(Subscript[sumpresent, i])=sumprevious+(((((-1))^(i+1))*(xv^(2*i-2)))/((2*i-2)!));
sumprevious=(Subscript[sumpresent, i]),
{i,1,n}]; 
f[x_]:=N[Cos[x]]];

Using Mathematica to calculate true error, absolute true error, relative true error, and absolute relative true error for each term. Once these error values are calculated, determining if sufficient iterations have been performed by comparing the prespecified tolerance to the absolute relative true error.

Do[
(Subscript[TrueError, i])=f[xv]-(Subscript[sumpresent, i]);
(Subscript[AbsTrueError, i])=Abs[f[xv]-(Subscript[sumpresent, i])];
(Subscript[RelTrueError, i])=(((f[xv]-(Subscript[sumpresent, i]))*100)/(f[xv]));
(Subscript[AbsRelTrueError, i])=Abs[(((f[xv]-(Subscript[sumpresent, i]))*100)/(f[xv]))];,
{i,1,n}]

Section 3: Table of Values

This table shows the true value, true error, absolute true error, relative true error, absolute relative true error, and if the prespecified tolerance has been met, all as a function of the number of the number of terms used.

TableForm[Table[{i,f[xv],(Subscript[TrueError, i]),(Subscript[AbsTrueError, i]),(Subscript[RelTrueError, i]),(Subscript[AbsRelTrueError, i])},{i,1,n}],TableHeadings->{None,{"Terms Used","True Value", "True Error", "Abs True Error", "Rel True Error", "Abs Rel True Error"}},
TableSpacing->{2,2}]

Section 4: Graphs

The following graphs show the true value, calculated value, true error, absolute true error, relative true error, and absolute relative true error, all as a function of the number of terms.

Plot1=Plot[f[xv],{x,0,n},PlotStyle->{Thickness[0.008`],RGBColor[0,1,0]}];
data=Table[(Subscript[sumpresent, i]),{i,1,n}];
Plot2=ListPlot[data,Joined->True,PlotStyle->{Thickness[0.008`],RGBColor[1,0,0]}];
Needs["PlotLegends`"];
l1=Graphics[{Green,Line[{{0,0},{1,0}}]}];
l2=Graphics[{Red,Line[{{0,0},{1,0}}]}];
Legend[{l1 ,"Exact Value"},{l2,"Approximate Value"}]
Show[Plot1,Plot2,PlotLabel->"Calculated Value of f(x) Using Maclaurin Series vs. Number of Terms",Frame->True,FrameLabel->{"Number of Terms Used","Approximate Value"},PlotRange->Automatic]

data=Table[(Subscript[TrueError, i]),{i,1,n}];
Plot2=ListPlot[data,Joined->True,PlotStyle->{Thickness[0.008`],RGBColor[0,0,1]},PlotLabel->"True Error vs. Number of Terms",Frame->True,FrameLabel->{"Number of Terms Used","True Error"},PlotRange->Full]

data=Table[(Subscript[AbsTrueError, i]),{i,1,n}];
Plot2=ListPlot[data,Joined->True,PlotStyle->{Thickness[0.008`],RGBColor[0,0,1]},PlotLabel->"Absolute True Error vs. Number of Terms",Frame->True,FrameLabel->{"Number of Terms Used","Absolute True Error"},PlotRange->Full]

data=Table[(Subscript[RelTrueError, i]),{i,1,n}];
Plot2=ListPlot[data,Joined->True,PlotStyle->{Thickness[0.008`],RGBColor[0,0,1]},PlotLabel->"Relative True Error vs. Number of Terms",Frame->True,FrameLabel->{"Number of Terms Used","Relative True Error"},PlotRange->Full]

data=Table[(Subscript[AbsRelTrueError, i]),{i,1,n}];
Plot2=ListPlot[data,Joined->True,PlotStyle->{Thickness[0.008`],RGBColor[0,0,1]},PlotLabel->"Absolute Relative True Error vs. Number of Terms",Frame->True,FrameLabel->{"Number of Terms Used","Absolute Relative True Error"},PlotRange->Full]

Conclusion

This worksheet shows how the number of terms taken in a Maclaurin series affects the accuracy of the calculated answer through the analysis of error. Note that though true error shows the magnitude of the error, it does not indicate how bad the error really is. Hence, relative true error is used here to give a more complete picture of the state of error.

References

Measuring Errors.
See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_measuringerror.pdf

Legal Notice: The copyright for this application is owned by the author(s). Neither Wolfram Research nor the author(s) are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.