Question: My code: # 3 key statements: numpy, pyplot, inline display import numpy as np import matplotlib.pyplot as plt % matplotlib inline ## Initialization #Define the
My code:
# key statements: numpy, pyplot, inline display
import numpy as np
import matplotlib.pyplot as plt
matplotlib inline
## Initialization
#Define the function that you're finding the root of
def fnx:
return npcosx
# Desired error tolerance
tolerance E
# No iterations large enough to guarantee tolerance
maxIt
#guess for lower and upper bound on root
lower
upper
# Create array for error results
maxError npzerosmaxIt
# Create array for actual error
actualError npzerosmaxIt
#actual root for cosx between lower and upper theoretical
actual nppi
#first average to set up first iteration
avgupper lower
## Calculation
for i in rangemaxIt:
maxErroriupperavg
actualErroriabsavgactual
# If tolerance has been met, then exit loop
if maxErroritolerance:
break
## Narrow down location of root depending on signs
# First option: if avg is root, then exit loop done
if fnavgactual:
break
# Second option: if fn changes sign on left, then move left
elif fnlowerfnavg:
upperavg
# Last resort: if fn changes sign on right, then move right
else:
loweravg
avglowerupper
# Done! Print result
printEstimated root isavg,"with tolerance",tolerance
## Presentation in a figure
#plot max and actual error versus iteration number
pltfigure
pltplotnparangei maxError:i label"Max Error"
pltplotnparangei actualError:i label"Actual Error"
# Change y scale to logarithmic
pltyscalelog
# Add labels to axes
pltxlabelIteration Number"
pltylabelAbsolute Error"
# Add legend and title.
pltlegendloc
plttitleMax and Actual Error for Bisection Method"
pltshow Estimated root is with tolerance e
Modify your code to approximate the following
square root of
the cube root of
the fifth root of
the tenth root of
to within Youll probably need different loops Plot the theoretical and actual errors for all of these approximations on the same graph. What
does your graph show about the actual and theoretical rate of convergence for these three different cases? In particular, if the slopes for two different
methods are the same, what does that tell you about the relative accuracy of the two methods? Put your comments as a Markdown cell after your code.
Consider the following system of equations:
Use the bisection method to to find an approximate solution to the equaitons on the interval Set your level of tolerence to
Questions and in python
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
