Math 272: Approximation

Example 1: Find the best-fit line to the following set of data:

Gestation period in days  	Longevity in years 	(data from 1993 World Almanac and Book of Facts)
Black bear	219		18
Grizzly bear	225		25
Polar bear	240		20
Leopard	  98		12
Lion		100		15
Puma		  90		12
Tiger		105		16

Plot the data.

data={{219,18},{225,25},{240,20},{98,12},{100,15},{90,12},{105,16}};

Show[ListPlot[data,PlotStyle->PointSize[0.02]],PlotRange->{{0,250},{0,25}}]

To avoid lots of tedious data entry, we can use the Table command to generate a new list of points, a matrix, or a vector.  For example, the commands below generate the matrix A whose first column are the x's of the datapoints and whose second column consists of 1's, and the vector b consisting of the y's of the datapoints.  Note that data[[k,1]] gives the x-component of the kth datapoint and data[[k,2]] gives the y-component of the kth datapoint.

NumPoints=7;
A=Table[{data[[k,1]],1},{k,1,NumPoints}] 
b=Table[data[[k,2]],{k,1,NumPoints}]

Show[Plot[a*x+c,{x,0,250}],ListPlot[data,PlotStyle->PointSize[0.02]],PlotRange->{{0,250},{0,25}}]

Example 2a: Find the least squares approximation to Exp[2x] in (Subscript[P, 1]) on the interval [0,1]

p1=1;
p2=Sqrt[12](x-1/2);(* ON basis for (Subscript[P, 1]) *)

c1=Integrate[Exp[2x] p1,{x,0,1}]
c2=Integrate[Exp[2x] p2,{x,0,1}](* inner products of f with each basis polynomial *)

Plot[{Exp[2x],c1 p1+c2 p2},{x,0,1}] (* plot of f and its (Subscript[P, 1]) approximation *)

Example 2b: Find the least squares approximation to Exp[2x] in (Subscript[P, 2]) on the interval [0,1]

p1=1;
p2=Sqrt[12](x-1/2);
p3=Sqrt[180](x^2-x+1/6); (* ON basis for (Subscript[P, 2]) *)

c1=Integrate[Exp[2x] p1,{x,0,1}]
c2=Integrate[Exp[2x] p2,{x,0,1}]
c3=Integrate[Exp[2x] p3,{x,0,1}] (* inner products of f with each basis polynomial *)

Plot[{Exp[2x],c1 p1+c2 p2+c3 p3},{x,0,1}] (* plot of f and its (Subscript[P, 2]) approximation *)

Even a small earthquake could damage the pipeline, if the fault plane ruptures the surface and if it intersects the ground (z=0) on a line (called the "fault trace") that cuts through the pipeline. To determine the approximate location of the fault plane, geologists have recorded the locations of 32 seismic events (i.e., tiny movements picked up by sensitive instruments) along the subterranean (z<0) fault plane. The data below are the coordinates (x,y,z) of these seismic events.  We want to fit a plane z=ax+by+c to this data.  This plane will approximate the fault and we can then find the intersection of this fault plane with the surface z=0 to determine whether the fault plane is likely to intersect the pipeline (a very worrisome scenario).

Example 3: Find the fourth order Fourier approximation to f(x)=x on the interval [-pi, pi]

Clear[x, k,y, a, b]

a0=Integrate[x*1/Sqrt[2*Pi],{x,-Pi,Pi}];
a[k_]:=Integrate[x*1/Sqrt[Pi]*Cos[k*x],{x,-Pi,Pi}];
b[k_]:=Integrate[x* 1/Sqrt[Pi]*Sin[k*x],{x,-Pi,Pi}] ;(* inner products of f with each basis polynomial *)

g4[x_]:=a0*1/Sqrt[2*Pi]+Sum[a[i]*1/Sqrt[Pi]*Cos[i*x], {i,1,4}]+Sum[b[i]*1/Sqrt[Pi]*Sin[i*x], {i,1,4}]

Print[g4[x]]

Plot[{x,(*insert function here*)},{x,-4,4}] (* plot of f and its approximation *)