The Quadratic Formula as a Way to Show the Subtraction of Small Numbers

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 illustrates the use of a quadratic equation solution for showing the effect of significant digits on round-off errors The user will enter the a, b and c values as given by the equation for the standard form of a quadratic equation: (ax^2) + bx + c = 0, as well as the number of significant digits to be displayed in a table that will be created at the end of the program. Two variations of the quadratic equation solution will be used :

(A)     x1 = ((-b + (Sqrt[(b^2) - 4 a c]))/(2 a))
           x2 = ((-b - (Sqrt[(b^2) - 4 a c]))/(2 a))
(B)     x1 = ((2 c)/(-b - (Sqrt[(b^2) - 4 a c])))
           x2 = ((2 c)/(-b + (Sqrt[(b^2) - 4 a c])))

Section 1: Input Data

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

The quadratic formula is derived from the standard form of a quadratic equation: (ax^2) + bx + c = 0.

Enter coefficient a

a=0.001;

Enter coefficient b

b=-4.94627;

Enter coefficient c

c=0.002;

Enter range of significant digits to be used.

siglow=7;
sighigh=10;

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: Significant Digit Arithmetic Functions

The following functions modify standard arithmetic operators allowing computation with the appropriate number of significant digits.

sdscale[sd_,x_]:=Module[{},
If[x==0,m=sd,m=sd-(Floor[Log[10,Abs[x]]]+1)] ;
q=N[x*10^m];
q=N[Floor[q]*10^(-m)]]

add[a_,b_]:=N[a+b]
sub[a_,b_]:=N[a-b]
div[a_,b_]:=N[a/b]
mul[a_,b_]:=N[a*b]

SdDyadic[op_,sd_,x_,y_]:=Module[{},
z=op[sdscale[sd,x],sdscale[sd,y]];
sdscale[sd,z]]

sdadd[sd_,x_,y_]:=SdDyadic[add,sd,x,y]
sdsub[sd_,x_,y_]:=SdDyadic[sub,sd,x,y]
sdmul[sd_,x_,y_]:=SdDyadic[mul,sd,x,y]
sddiv[sd_,x_,y_]:=SdDyadic[div,sd,x,y]

Section 3: Calculation

The following calculations will be performed inside a loop so that the number of significant digits used can be varied as specified by the user.
Variation 1: 
x1a = ((-b + (Sqrt[(b^2) - 4 a c]))/(2 a))
x2a = ((-b - (Sqrt[(b^2) - 4 a c]))/(2 a))

root1a=sdsub[dig,sdmul[dig,b,b],sdmul[dig,4 a,c]];
top1a=sdadd[dig,-b,(Sqrt[root1a])];
x1a[dig_]=sddiv[dig,top1a,2 a];

root2a=sdsub[dig,sdmul[dig,b,b],sdmul[dig,4 a,c]];
top2a=sdsub[dig,-b,(Sqrt[root2a])];
x2a[dig_]=sddiv[dig,top2a,2 a];

Variation 2: 
x1b = ((2c)/(-b - (Sqrt[(b^2) - 4 a c])))
x2b = ((2c)/(-b + (Sqrt[(b^2) - 4 a c])))

root1b=sdsub[dig,sdmul[dig,b,b],sdmul[dig,4 a,c]];
bott1b=sdsub[dig,-b,(Sqrt[root1b])];
final1b=sddiv[dig,2 c,bott1b];
x1b[dig_]=final1b;

root2b=sdsub[dig,sdmul[dig,b,b],sdmul[dig,4 a,c]];
bott2b=sdadd[dig,-b,(Sqrt[root2b])];
final2b=sddiv[dig,2 c,bott2b];
x2b[dig_]=final2b;

Section 4: Table of Values

This table shows the values of x1a, x2a, x1b, and x2b and the number of significant digits used in their calculation.

TableForm[Table[{i,x1a[i],x1b[i],x2a[i], x2b[i]},{i,siglow,sighigh}],TableHeadings->{None,{"Digits","x1a", "x1b", "x2a", "x2b"}},
TableSpacing->{2,2}]

Section 5: Graphs

These bar graphs show the values of x1 and x2 for both variations of the quadratic function.

Needs["BarCharts`"];
data1=Table[x1a[i], {i,siglow,sighigh}];
data2=Table[x1b[i], {i,siglow,sighigh}];
BarChart[{data1,data2},PlotLabel->"Value of First Root as a Function of Significant Digits",Frame->True, FrameLabel->{"Number of Significant Digits","Value of Quadratic Root"},BarLabels->Range[siglow,sighigh]]

data1=Table[x2a[i], {i,siglow,sighigh}];
data2=Table[x2b[i], {i,siglow,sighigh}];
BarChart[{data1,data2},PlotLabel->"Value of Second Root as a Function of Significant Digits",Frame->True, FrameLabel->{"Number of Significant Digits","Value of Quadratic Root"},BarLabels->Range[siglow,sighigh]]

Conclusion

Subtraction of numbers that are nearly equal can result in unwanted inaccuracies. The number of significant digits used in calculations plays a large role in the creation of these inaccuracies and the magnitude of the round-off errors. Hence, when the accuracy of calculations is critical, it is necessary to understand possible sources of error and how they are best avoided.

References

Sources of Error. See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_sourcesoferror.pdf
Propagation of Errors. See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_propagationoferrors.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.