Question: Exercises 1 . Copy your code into another cell, and modify to plot the 1 0 th order Taylor series approximation and theoretical error of

Exercises
1. Copy your code into another cell, and modify to plot the 10 th order Taylor series approximation and theoretical error of f(x)=cos(3x) with expansion point x=0.5,
on the interval -0.5,2.5 with h-increment 0.1. NOTE (i)'10th order' means that you use 10 derivatives. (ii) Since the expansion point is not zero, in your graph you will
need to plot x+h on the x-axis versus the Taylor series value on the y axis. (iii) Make sure to choose an appropriate value for when estimating the true error. will be
different than the warm-up.
2. Copy your code into another cell, and modify it to do the 6 th order Taylor series approximation and theoretical error for f(x)=ln(x) with expansion point x=1, on
the interval 0.5,2 with h-increment 0.05. Note you must choose the point c in the interval 0.5,2 which makes fn+1(c) as large as possible. You cannot assume that c
is equal to the expansion point.
3. Copy your code into another cell, and compute the 12 th order MacLaurin series approximation and theoretical error for f(x)=e2x-e-2x on the interval -1,1 with
increment 0.05. Use the expansion point x=0.
4.Create a new code cell. Consider the function f(x)=e2x.(a) Write a python function that can output the nth term of a MacLaurin series with input n and h.(b)Write
code that estimates e2 with minimum 6 decimal points of accuracy by using the approximate relative error calcuation. (c)Output your estimate, the true value (using
pythons built in exponential function), and the true error. Does your calculation have at least 6 decimal points of accuracy? This is the original code and it must be change to be able to be able to work with the exercises in different codes import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Factorial comes from 'math' package
from math import factorial
#
### Initialize section
#array of derivatives at 0 compute by hand
deriv = np.array([1,0,-1,0,1,0,-1,0])
# degree of Taylor polynomial
N=len(deriv)-1
# Max abs. value of N+1 deriv on interval (Compute using calculus)
# (used in error formula: error = abs(h^(N+1)/(N+1))* max(abs(N+1th deriv.))
maxAbsNplus1deriv=1
#array with h values
h = np.arange(-3,3.05,0.01)
# Give the expansion point for the Taylor series
expansion=0
# Create an array to contain Taylor series
taylor=np.zeros(len(h))
#
### Calculation section
#calculate Taylor series for all h at once using a loop
for n in range(N+1):
taylor = taylor +(h**n)/factorial(n)*deriv[n]
#actual function values
actualFn = np.cos(expansion+h)
#error actual and taylor series
actualAbsError = abs(taylor-actualFn)
#theoretical error=|h^(deg+1)/(deg+1)!*max|n+1 deriv||
theoAbsError = abs(h**(N+1)/factorial(N+1)*maxAbsNplus1deriv)
#Compute actual x values (displaced by expansion point)
xActual = expansion+h
#
### Output section
#plot figures for cos and errors using
# Object-oriented style plotting
# Two subplots in a row
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))
# objects for the left subplot (attached to axes #0)
axes[0].plot(xActual, taylor, "r",label="Taylor series")
axes[0].plot(xActual, actualFn, "b--", label="Cos(x)")
axes[0].legend(loc=2) #legend in upper left
axes[0].set_title("Taylor Series for cos(x)")
# objects for the right subplot (attached to axes #1)
axes[1].plot(xActual, actualAbsError, label="Actual Absolute Error")
axes[1].plot(xActual, theoAbsError, label="Theoretical Absolute Error")
axes[1].legend(loc=2) #legend in upper left
axes[1].set_title("Actual and Theoretical Error")
Exercises 1 . Copy your code into another cell,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!