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 for Use the following values of :dots The values on the axis should
use reverse log scale as with the original code The expression En means 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 for the original code and your code for part 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: It is possible to take other unequal averages, such as and so on A general
expression for such an average is: where 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; ii; iii 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 page we find the forward difference approximation for the second derivative of at the point :
~~
On page we find the centered difference approximation:
~~
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 at the point Use the same values of as in the original code. Plot the errors for the different approximations.
What value of is required to obtain a true accuracy of 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 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 loglog 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
#function to be differentiated
fnpexpx
#Derivative of function theo calculation
fPrimenpexpx
#get vector of deltax values
deltax nparange
## Calculation
#Compute forward approx.
fPlus npexpxdeltax
forward fPlusfdeltax
#Compute backward approx
fMinus npexpxdeltax
backward ffMinusdeltax
#use average of forward and backward for centered
centered forward backward
#compute absolute errors
forwardError absfPrime forward
backwardError absfPrime backward
centerError absfPrime centered
## Display section create figure
pltfigure
#plot forward, backward, and cente
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
