Mathematical Principles for Scientific Computing and Visualization  
http://www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

This notebook demonstrates cancellation errors with the function (1-cosx)/x^2.
This function is plotted over [-10,10] and over a small interval around x=0.


See Chapter 2: Computational Basics for details.

Updated May 2008

(* Set output folder to folder this file lives in *)

(SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];) 

Clear[x];
ClearAll;

(* Define our function *)
f[x_]:=N[(1.0-Cos[x])/x^2,300];

(* Plot the function over [-10,10] *)

fplot1=Plot[f[x],{x,-10,10},PlotStyle-> {GrayLevel[0.0],Thickness[0.01]}, BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}, AxesStyle->Directive[GrayLevel[0.0],Thickness[0.005]],TicksStyle->Directive[GrayLevel[0.0],Thickness[0.005]],ColorOutput->CMYKColor]

(* Plot the function over a small interval -- +/- 5*10^{-8) *)

fplot2=Plot[f[N[x,30]],{x,-5*10^-8,5*10^-8},PlotStyle-> {GrayLevel[0.0],Thickness[0.01]}, BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11},  AxesStyle->Directive[GrayLevel[0.0],Thickness[0.005]],TicksStyle->Directive[GrayLevel[0.0],Thickness[0.005]],ColorOutput->CMYKColor]

(* Output to postscript *)
Export["cancellation1.eps",fplot1];
Export["cancellation2.eps",fplot2];