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

This notebook demonstrates piecewise linear interpolation.
Figure 8.2 was generated with this notebook.

Input: a function f(x). Here f(x) = x^2 + 100sin(x) + 10
Output: a piecewise linear approximation using 15 evaluations

Whenever we plot a curve on a computer, we are creating a piecewise linear function. Typically, the software evaluates the function at so many places that the pw linear nature cannot be detected.


See Chapter 8: Data Fitting for details.

Updated July 2008

(* Clear these because we tend to use them in other notebooks *)
Clear[f];
Clear[x];

(* Specify the output folder *)

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


(* Define function *)
f[x_]:=x^2+100* Sin[x]+10;


(* Evaluate the function at x=1,2,3,...15 *)
pw:=Table[f[x],{x,1,15} ] 

(* Plot the function. Mathematica determines a good number of evaluations so the plot looks smooth. *)
plotf = Plot[f[x],{x,1,15}, PlotStyle->Thickness[0.01] , Ticks->None]

(* Let's plot the pw linear approximation in different styles  *)
(* First plot just the points *)
plotpw1=ListPlot[pw,Ticks->None,PlotStyle->PointSize[0.02`]] 

(* Plot the lines connecting the points on the function *)
plotpw2=ListPlot[pw,Joined->True,Ticks->None] 

(* Now put them all together *)
pic=Show[{plotf,plotpw1,plotpw2}, PlotRange->Automatic] 

(* Create a postscript file *)
Export["pwlinear_approx.eps",pic]