Differentiation of Continuous Functions
Forward Difference Approximation of the First Derivative

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

Introduction

This worksheet demonstrates the use of Mathematica to illustrate 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 derivative 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. The user inputs are
      a) function, f(x)
      b) point at which the derivative is to be found, xv 
      c) starting step size, h
      d) number of times user wants to halve the starting step size, n

The outputs include
      a) approximate value of the derivative at the point and given initial step size
      b) exact value
      c) true error, absolute relative true error, approximate error and absolute relative approximate error, number of at least correct significant digits in the solution as a functio
n of step size.

Function f(x):

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

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

xv=4.0;

Starting step size, h

h=0.2;

Number of times step size is halved

n=12.0;

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

Section 2: 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
 n    = number of times step size is halved

FDD[f_,xv_,h_]:=Module[{deriv},
deriv=(f[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]]

The next loop calculates the following:
AV: Approximate value of the first derivative using Forward Difference Approximation by calling the procedure "FDD"
Ev: Exact value of the first derivative
Et: True error
et: Absolute relative true percentage error
Ea: Approximate error
ea: Absolute relative approximate percentage error
Sig: Least number of correct significant digits in an approximation

Do[
Nn[i]=2^i;
H[i]=h/Nn[i];
AV[i]=FDD[f,xv,H[i]];
Et[i]=Ev-AV[i];
et[i]=Abs[(Et[i]/Ev)]*100.0;
If[i>0,
Ea[i]=AV[i]-AV[i-1];
ea[i]=Abs[Ea[i]/AV[i]]*100.0;
Sig[i]=Floor[(2-Log[10,ea[i]/0.5])];
If[Sig[i]<0,Sig[i]=0];
]
,{i,0,n-1,1}]

The loop halves the value of the step size n times. Each time, the approximate value of the derivative is calculated and saved in a vector. The approximate error is calculated after at least two approximate values of the derivative have been saved. The number of significant digits is calculated and written as the lowest real number. If the number of significant digits calculated is less than zero, then is shown as zero.

Section 4: Spreadsheet

The next table shows the step size value, approximate value, true error, the absolute relative true percentage error, the approximate error, the absolute relative approximate percentage error and the least number of correct significant digits in an approximation as a function of the step size value.

Print["      ","h","         ","Av","       ","Et","        ","et","        ","Ea","         ","ea","       ","Sig"];
Print[" "]
Grid[Table[{H[i],AV[i],Et[i],et[i],Ea[i],ea[i],Sig[i]},{i,0,n-1}]]

Section 5: Graphs

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

data=Table[{H[i],AV[i]},{i,0,n-1}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.002],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->"Approximate Solution of the First Derivative using\nForward Difference Approximation as a Function of Step Size",AxesLabel->{"Step Size","Approximate Value"}]
data=Table[{H[i],et[i]},{i,0,n-1}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.002],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->"Absolute Relative True Percentage\nError as a Function of Step Size",AxesLabel->{"Step Size","Absolute Relative 
True Error"}]
data=Table[{H[i],ea[i]},{i,0,n-1}];
plot=ListPlot[data,
PlotJoined->True,
PlotStyle->{Thickness[0.002],RGBColor[0,1,0]},
DisplayFunction->Identity,
PlotRange->Full];
Show[plot,PlotLabel->"Absolute Relative Approximate Percentage\n Error as a Function of Step Size",AxesLabel->{"Step Size","Absolute Relative 
Approximate Error"}]
sigdigplot=Table[{H[i],Sig[i]},{i,0,n-1}];
BarChart[sigdigplot,BarStyle->Green,PlotLabel->"Least Significant Digits Correct\nas a Function of Step Size"]

References

Numerical Differentiation of Continuous Functions. 
See http://numericalmethods.eng.usf.edu/mws/gen/02def/mws_gen_dif_txt_continuous.pdf

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 0.25 to find the acceleration at t=5s. Compare with the exact answer and study the effect of the step size.
2. Look at the true error vs. step size data for problem # 1. Do you see a relationship between the value of the true error and step size ? Is this concidential?

Conclusions

To obtain more accurate values of the first derivative using Forward Difference Approximation, the step size needs to be small. As the spreadsheet shows, the smaller the step size value is, the approximation is closest to the exact value.

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.