Question: Make a copy of the original code and put it in another cell. Modify the code to find and graph backward, forward, and centered difference

Make a copy of the original code and put it in another cell. Modify the code to find and graph backward, forward, and centered difference absolute true
errors for the derivative of f(x)=sin2(x) for x=4. Use the following values of x:3E-1,3E-2,3E-3,dots3E-9. The x values on the x-axis should
use reverse log scale (as with the original code). The expression E-n means 10-n. You may enter it exaclty as it appears here, and Python will be able
to interpret it correctly. Notice that the relation between the accuracy for the three methods is different than in the original code. What is the difference?
Compute (exactly) the second derivatives f''(x) for the original code and your code for part 1. How do you suppose the values of the second derivative
are related to the relative accuracy of the three methods? Put your comments in the code.
We have remarked that the centered difference approximation is the equal average of the forward (FW) and the backward (BW) difference
approximations: (0.5FW+0.5BW). It is possible to take other (unequal) averages, such as (0.4FW+0.6BW, and so on. A general
expression for such an average is: (FW+BW), where +=1. Make a copy of the original code and put it in a new cell. In the new cell, add three
new variables and put the results for the following three combinations: (i)=0.4=0.6; (ii)=0.49=0.51; (iii)=0.499,=0.501. Add the
errors for these three cases to the existing graph. Describe and interpret the trends that you see in the graph. Pay particular attention to the slopes. If the
graphs for two different approximation methods have nearly the same slope, then what does that tell you about the accuracy of the corresponding
methods? Put your comments after the code.
In Chapter 2.02 page 10, we find the forward difference approximation for the second derivative of f(x) at the point xzi :
f''(xi)~~f(xi+2)-2f(xi+1)+f(xi)(x)2+O(x)
On page 12, we find the centered difference approximation:
f''(xi)~~f(xi+1)-2f(xi)+f(xi-1)(x)2+O(x)2
From these two formulas, you may infer the backward difference approximation formula.
Make a copy of the original code and put it in a new cell. Modify the code to compute the forward, backward, and centered difference approximations for
the second derivative of f(x)=x3 at the point x=1. Use the same values of x as in the original code. Plot the errors for the different approximations.
What value of x is required to obtain a true accuracy of 3 decimal digits for the three methods? Put your answer as a comment below the code.
In a new cell, modify your code so that you can plot the errors for the forward, backwards, and centered difference approximation of the first derivative
obtained in the original alongside the error for forward, backward, and centered difference found in part 3. Compare the slopes of these lines. What does
this tell you about the accuracy of the second derivative approximations as compared to the accuracy of the first derivative approximations? Put your
comments after the code.
# The following code finds the forward, backward, and centered difference
# derivative approximations for a given function for a range of delta
# values, then plots the absolute errors on a log-log scale plot.
# Necessary imports
import numpy as np
import matplotlib.pyplot as plt
# "Magic" statement for jupyter notebook to display plots
%matplotlib inline
## Initialization
# Point where derivative is evaluated
x=0.5
#function to be differentiated
f=np.exp(x)
#Derivative of function (theo. calculation)
fPrime=np.exp(x)
#get vector of deltax values
deltax =2.**np.arange(-1,-14,-1)
## Calculation
#Compute forward approx.
fPlus = np.exp(x+deltax)
forward =(fPlus-f)/deltax
#Compute backward approx
fMinus = np.exp(x-deltax)
backward =(f-fMinus)/deltax
#use average of forward and backward for centered
centered =(forward + backward)/2
#compute absolute errors
forwardError = abs(fPrime - forward)
backwardError = abs(fPrime - backward)
centerError = abs(fPrime - centered)
## Display section -- create figure
plt.figure(1)
#plot forward, backward, and cente
Make a copy of the original code and put it in

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!