Question: I ' m trying to use the following code to answer 4 numerical analysis questions. Each question should have its own cell and a visual
Im trying to use the following code to answer numerical analysis questions. Each question should have its own cell and a visual representation like the one screenshot. This is in python.
# The following code finds the forward, backward, and centered differencevalues, then plots the absolute errors on a logLog scale plot.import numpy as np
import matplotlib.pyplot as pltmatplotlib inlinePoint where derivative is evaluated
x
#function to be differentiated
fnpexpx
#Derivative of function theo calculation
fPrimenpexpx
#get vector of deltax values
deltax nparange#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 centeredpltfigure
#plot forward, backward, and centered errors on loglog scale
pltplotdeltax forwardError, label"Forward Difference"
pltplotdeltax backwardError, label"Backward Difference"
pltplotdeltax centerError, label"Centered Difference"pltxlime
pltylimepltxscalelog
pltyscalelogpltxlabelDelta x
pltylabelAbsolute Error" #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 centeredpltfigure
#plot forward, backward, and centered errors on loglog scale
pltplotdeltax forwardError, label"Forward Difference"
pltplotdeltax backwardError, label"Backward Difference"
pltplotdeltax centerError, label"Centered Difference"pltxlime
pltylimepltxscalelog
pltyscalelogpltxlabelDelta x
pltylabelAbsolute Error"plt legendloc
plttitleErrors for Fw Bw Ct Difference approx for
pltshow
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
