Lab 9

MAT 229, Spring 2021

Exercises to submit

These must be done by hand on paper (or typed into Mathematica). 
Use Mathematica to check your work. Submit as pdf files in Canvas (if you can make it a single pdf, that helps the graders immensely).

Exercise 1

Let f(x)=sin(x). We want to approximate f(x) with Taylor polynomials centered at 0 for values of x in [-π/4,π/4].

Find the degree n such that you know the error in approximating f(x) with (Subscript[T, n])(x) is less than 0.00001.

f[x_]:=Sin[x]
a=-Pi/4;
b=Pi/4;
n=7 (* The first n that works *)
Plot[Abs[Derivative[n+1][f][x]],{x,a,b}]
(* Derivatives are either signs or cosines; if n is even the next higher derivative is a cosine; if odd a sine. The cosines max out at 1; the sines at the square root of 2 over 2: *)
m=If[OddQ[n],Sqrt[2.0]/2,1.0];
m/(n+1)!(b)^(n+1)

What is (Subscript[T, n])(x)?

Normal[Series[f[x],{x,0,7}]]

What is the Taylor series error estimate for x in [-π/4,π/4]?

m/(n+1)!Abs[x]^(n+1)

Exercise 2

Let g(x)=ln(x). We want to approximate g(x) with Taylor polynomials centered at 1.

Find the degree n such that you know the error in approximating ln(1.3) with (Subscript[T, n])(1.3) is less than 0.0001.

f[x_]:=Log[x]
center=1;
d=0.3;
a=center-d;
b=center+d;
n=8 (* The first n that works *)
Plot[Abs[Derivative[n+1][f][x]],{x,a,b}]
(* Derivatives are inverse powers, growing larger the closer x gets to 0; so the value of the derivative at a, the left endpoint, gives us the M: *)
m=Abs[Derivative[n+1][f][a]]
m/(n+1)!(d)^(n+1)

Repeat the same question with a different input value. Find the degree n such that you know the error in approximating ln(1.4) with (Subscript[T, n])(1.4) is less than 0.0001.

d=0.4;
a=center-d;
b=center+d;
n=15 (* The first n that works *)
Plot[Abs[Derivative[n+1][f][x]],{x,a,b}]
(* Derivatives are inverse powers, growing larger the closer x gets to 0; so the value of the derivative at a, the left endpoint, gives us the M: *)
m=Abs[Derivative[n+1][f][a]]
m/(n+1)!(d)^(n+1)

The Taylor’s error estimate says that when approximating f(x) with (Subscript[T, n])(x) for |x-a|≤d (in other words -d≤x-a≤d) choose K in
	|(Subscript[R, n])(x)|≤(K/((n+1)!))|x-a|
 so that K≥|(f^((n+1)))(x)| whenever |x-a|≤d. For g(x)=ln(x) and a=1, how big must d be for it to be impossible to find K?

When d hits 1, the derivatives won’t be defined they’re asymptoting to infinity at x=0). So that’s when it becomes impossible.

Exercise 3

The fundamental theorem of calculus has two parts. One part is that if
	F(x)=(∫_a^x)f(t)d t
then
	F'(x)=f(x).

Let F(x)=(∫_0^x)(e^(-(t^2)))d t. Approximate it with Taylor polynomials centered at 0.

What is F(0)? (This is easy; look at the integral.)

F(0)=(∫_0^0)(e^(-(t^2)))d t = 0.

What is (Subscript[T, 4])(x)?

f[x_]:=Integrate[E^(-t^2),{t,0,x}]
f'[x]
t4[x_]=Normal[Series[f[x],{x,0,4}]]
Plot[{f[x],t4[x]},{x,-2,2}]

What is an error estimate in using this Taylor polynomial to approximate (∫_0^1)(e^(-(t^2)))d t.

With an M=4, I got 0.166667.

d=1;
center=0;
a=center-d;
b=center+d;
n=4 (* The n of T4 *)
(* Derivatives are complicated -- no simple rule! *)
Plot[{12,Abs[Derivative[n+1][f][x]]},{x,a,b}]
m=Abs[Derivative[n+1][f][0]]
m/(n+1)!(d)^(n+1)
t4[1.0]
f[1.0]

Use the midpoint rule with n=4 to

Approximate F(1)=(∫_0^1)(e^(-(t^2)))d t.

mid=0.25*Sum[f'[0+(k-0.5)0.25],{k,1,4}]

Compare to the value (Subscript[T, 4])(1) and the actual value F(1).

The midpoint method is much better than using the Taylor polynomial:

f[1.0] (* true *)
t4[1.0]
mid

According to the midpoint rule error estimate, what is an error estimate for this approximation?

I get an error bound of 0.00520833:

Plot[Abs[f'''[x]],{x,0,1}] (* Note: using the third derivative of f, since the integrand of the integral is already the derivative of f *)
k2=2.0
k2(1)^3/(24 *4^2)

Compare to the actual absolute error of the midpoint estimate.

We’re doing fine: we’re well under the bound:

Abs[mid-f[1.0]]

Here’s a surprise for you:

What is (Subscript[T, 3])(x)?

t3[x_]=taylor[f,0,3]

Compare to (Subscript[T, 4])(x) in part a., and explain (symmetry!).

They’re the same. The function f is odd, and so may have no even powers (i.e., t4 contains no power 4, as we’d expect!).

Without doing any calculations, how are (Subscript[T, 400])(x) and (Subscript[T, 399])(x) related?

They’re the same.