Lab 11: Approximation over an Interval

This lab is based on 8.4.15 and 8.4.16 in the text.  I have also added two additional questions, marked (★★★), for you to do.  You may work in groups of 2-3.  One written lab report (hard copy) should be submitted per group, by next Thursday’s lab.

Your report should have a title and an introductory paragraph, stating the purpose of the report and your principal findings.  Then your report should include several paragraphs explaining what you did (procedure) and what you discovered (results).  (Make sure that your paragraphs answer all the questions that are asked.)  Incorporate well-labeled supporting graphs or tables into your report as appropriate.  Finally, your report should include a paragraph with your conclusions and insights.

Introduction

We have discussed the use of Taylor polynomials in approximating the values of a function near a given “good point.”  For example, to approximate (Sqrt[5]), we could use a Taylor polynomial at x=4 for f(x)=(Sqrt[x]), since x=4 is near x=5 and it is easy to compute (Sqrt[4]).  We also know how to determine what degree Taylor polynomial is needed to obtain an approximation that is “good enough,” namely, within a given desired degree of accuracy.

We have also observed that, although Taylor polynomials are local approximations, it is often the case that increasing the degree of the Taylor polynomial seems to give decent approximations over larger ranges.

Thus it is reasonable to hope that if we want to approximate a given function over a given interval, we might be able to find a Taylor polynomial does the job decently well.

In order to make this more precise, we will need a way to describe the error over an interval.

Two Taylor Polynomials at a Central Point (8.4.15)

Suppose that we want to find a polynomial that approximates the function f(x)=sin(x) + (e^x) reasonably well on the interval [-6, 2].  We will use Mathematica to find two Taylor polynomials approximating this function at x=-2.  See the statement of 8.4.15 in the text for instructions.  The following Mathematica commands will be helpful.

We define the function in Mathematica with the following command:

f[x_] := Sin[x] + Exp[x];

Since x=-2 is in the middle of the interval [-6,2], we consider Taylor polynomials at x=-2.  The fourth and eighth degree Taylor polynomials, denoted (Subscript[T, 4])(x) and (Subscript[T, 8])(x) respectively, are defined below:

T4[x_] := Normal[Series[f[t], {t, -2, 4}]]/.t->x;
T8[x_] := Normal[Series[f[t], {t, -2, 8}]]/.t->x;

You can see what these polynomials are by evaluating the following commands:

T4[x]
T8[x]

The Best Fourth Degree Polynomial (8.4.16)

Again, we are looking for a polynomial to approximate the function f(x)=sin(x) + (e^x) on the interval [-6, 2].  In the previous exercise, we used Taylor polynomials, of degree four and eight, at the central point x=-2.  Now, instead of increasing the degree of the polynomial, we will vary the point at which we approximate.  See the statement of the 8.4.16 in the text for instructions.  I have also added two intermediate steps between 8.4.16(a) and (b).  See the instructions marked (★★★) below.

A Graphical Approach Using the Manipulate Command

The following command defines the degree four Taylor polynomial (Subscript[T, 4, a])(x) for f(x) at an unspecified x=a.

T4Param[x_,a_] = Normal[Series[f[t], {t, a, 4}]]/.t->x;

To see the mathematical expression for this polynomial evaluate the following:

T4Param[x,a]

In Mathematica, the Manipulate command, used with Plot, creates an interactive graph.  We will use the Manipulate command to see what the (Subscript[T, 4, a])(x) looks like for various values of a between -6 and 2.  Notice that the PlotRange option fixes the range of the y-axis.  (If you do not specifically tell Mathematica to fix the y-range, the y-range might change as a changes, and then the graphs are hard to compare.)

Manipulate[Plot[{f[x],T4Param[x,a]}, {x, -6,2}, PlotRange->{-5,8}], {a, -6,2}]

(★★★) Use the  Manipulate command to estimate three different values for a that seem to give good approximations for f(x) over the whole interval [-6, 2].  (Click on the plus sign to the right of the slider for a to see the values of a as you move the slider.)

A Careful Definition of Error

So far we have avoided saying exactly what we mean by a “good approximation” over a whole interval.  We need a way to quantify the “distance” between two functions on an interval.  There are many ways to do this.  One is to say that the “distance” between two functions on an interval is the area between the curves, which can be described as an integral:

({{({{(Area between f(x) and g(x) on [a,b]) = ((∫_a)^b)|f(x)-g(x)|d x}})}})

Notice that if g(x) is a function approximating f(x), e.g. if g(x) is a Taylor polynomial for f(x), and if the interval [a,b] is of length one, then this formula finds the average of the absolute error over the interval [a,b].

One difficulty with this formula is the fact that it uses the absolute value function, which tends to wreak differentiability.  In order to avoid this problem, we use the following variant instead:

({{({{Magnitude of Error = ((∫_a)^b)(((f(x)-(Subscript[T, n])(x)))^2)d x}})}})

In our case, the error of the approximation of f(x) by (Subscript[T, 4, a])(x), as it depends on the point of approximation x=a, is:

({{({{E(a)=}})(∫_(-6)^( 2))(((f(x)-(Subscript[T, 4, a])(x)))^2)dx}})

(★★★) Use Mathematica to compute the errors for the three Taylor polynomials that you chose in the previous section.  The following command will make a table of the errors for (Subscript[T, 4, a])(x) for a=-6, -3, and 1.  Replace these a-values with your own.

Errors=TableForm[Table[NIntegrate[(f[x]-T4Param[x,a])^2, {x, -6, 2}], {a, {-6, -3, 1}}]]

Next, we define error, as a function of a, with the following Mathematica command (which takes a while to execute.  Wait until the Mathematica is done executing the command before trying to execute any other commands.)

ErrorFcn[a_]=Integrate[(f[x]-T4Param[x,a])^2, {x, -6, 2}, Assumptions -> a≥ -6 && a ≤ 2];

Minimizing the Error Using Calculus

Recall that derivatives are useful in finding local maxima and minima.  The following command computes the derivative of the error function E(a) with respect to a.

DerivError[a_]=ErrorFcn'[a];

Plot the derivative function and estimate the points where the derivative is equal to zero.

To find the critical points, set the derivative equal to zero.  (This will take a while too.)

CriticalPoints =  NSolve[DerivError[a]==0 &&  -6 ≤a && a <= 2, a]

By looking at graphs, determine which of these critical points corresponds to the best value for a.  The following command will define the BestPoint to be the third critical point.  You might want to change the 3, so that you are using the correct critical point for your “best point.”

BestPoint = a/.CriticalPoints[[3]]

Then your best approximating Taylor polynomial will be (Subscript[T, 4, b])(x) where b is the “best point.”

BestApproxFcn[x_] =T4Param[x,BestPoint] ;