Effect of Significant Digits on Derivative of a Function

Ana Catalina Torres, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu

Introduction

This worksheet demonstrates the use of Maple to illustrate the effect of significant digits on the numerical calculation of the Forward Difference Approximation of the first derivative of continuous functions. 
Forward Difference Approximation of the first derivative uses a point h ahead of the given value of x at which the derivate of f(x) is to be found.
					f'(x)≅((f(x+h)-f(x))/h)

Section 1: Input

The following simulation approximates the first derivative of a function using Forward Difference Approximation with fixed number of significant digits used in the calculation. The user inputs are
      a) function, f(x)
      b) point at which the derivative is to be found, xv 
      c) step size, h 
      d) The lowest and highest number of significant digits user wants to use in the calcluation.  The user should choose the lowest number to be at least 2.

The outputs include
      a) exact value
      c) true error and absolute relative true error as a function of the number of significant digits.

Function f(x):

f[x_]:=x*Exp[2*x];

Value of x at which f '(x) is desired, xv

xv=4;

Step size, h

h=0.5;

Lowest number of Significant Digits and Highest Number of Significant Digits

nlow:=2;
nhigh:=10;

This is the end of the user section. All the information must be entered before proceeding to the next section.

Section 2: Significant Digit Operators

The following functions modify standard arithmetic operators allowing computation with the appropriate number of significant digits. These redefined operators are then used in the Forward Difference Approximation method to generate a solution that was computed with the number of significant digits specified.

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

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

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

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

Section 3: Procedure

The following procedure estimates the solution of first derivate of an equation at a point xv.
f (x) = function
 xv   = value at which the solution is desired
 h    = step size value
sd   = number of significant digits used in the calculation

FDD[sd_,f_,xv_,h_]:=Module[{deriv},
deriv=sddiv[sd,sdsub[sd,f[sdadd[sd,xv,h]],f[xv]],h];
deriv]

Section 3: Calculation

The exact value Ev of the first derivative of the equation:
First, using the diff command the solution is found. In a second step, the exact value of the derivative is shown.

f[x_]

f'[x_]

Ev=N[f'[xv],10]

The next loop calculates the following:
The next loop calculates the following:
Av: Approximate value of the first derivative using Forward Difference Approximation by calling the procedure "FDD"
Et: True error
et: Absolute relative true percentage error
Ea: Approximate error
ea: Absolute relative approximate percentage error

Do[
Digits[i]=i;
AV[i]=N[FDD[i,f,xv,h],i];
Et[i]=Ev-AV[i];
et[i]=Abs[(Et[i]/Ev)]*100;
,{i,nlow,nhigh,1}]

The loop calculates the approximate value of the first derivative, the corresponding true error and relative true error as  a function of the number of significant digits used in the calculations.

Section 4: Spreadsheet

The next table shows the approximate value, true error, and the absolute relative true percentage error as a function of the number of significant digits used in the calculations.

Print["  ","Dig","     ","AV","           ","Et","          ","et","       "];
Print[" "]
Grid[Table[{Digits[i],AV[i],Et[i],et[i]},{i,nlow,nhigh}]]

Section 5: Graphs

The following graphs show the approximate solution, true error and absolute relative true error as a function of the number of significant digits used.

data=Table[{i,AV[i]},{i,nlow,nhigh}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.004],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->Style["Approximate Solution of the First Derivative using Forward\nDifference Approximation as a Function of Number of Significant Digits",12],AxesLabel->Style["Approximate Value",12]]
data=Table[{i,Et[i]},{i,nlow,nhigh}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.004],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->Style["True Error in the First Derivative using Forward \nDifference Approximation as a Function of Number of Significant Digits",12],AxesLabel->Style["True error",12]]
data=Table[{i,et[i]},{i,nlow,nhigh}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.004],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->Style["Relative True Error using Forward Difference\nApproximation as a Function of Number of Significant Digits",12],AxesLabel->Style["Relative True error",12]]

References

Numerical Differentiation of Continuous Functions.  http://numericalmethods.eng.usf.edu/mws/gen/02dif

Questions

1. The velocity of a rocket is given by
				v(t)=2000ln(140000/(140000-2100t))-9.8t
Use Forward Divided Difference method with a step size of 25/100 to find the acceleration at t=5s using different number of significant digits.

Conclusions

The effect of significant digits on the calculation of the first derivative using Forward Difference approximation is studied.

Legal Notice: The copyright for this application is owned by the author(s). Neither Maplesoft nor the author 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 for-profit activities.