Question: Python! please Project Program requirements: Implement named constants, with correct naming convention, for the constants to determine the output of the range method Generate a
Python! please
Project Program requirements:
Implement named constants, with correct naming convention, for the
constants to determine the output of the range method
Generate a list of multiples of in the range
Implement the NewtonRaphson algorithm and the Bisection search
algorithms to compute the square roots
Set epsilon at for both algorithms
Determine the following for each algorithm:
Max, min and average number of iterations required
Determine the execution time for each algorithm
Plot histograms, in the same figure, of the number of iterations
required by each algorithm
To solve Project do the following:
Modify the code below as follows:
Add code to implement the NewtonRaphson algorithm to compute square roots code can be found in Figure page of the text
Determine execution time
Determine number of iterations required
Verify that your code produces output similar to the sample output that youve been given in the next slide
#import package needed to compute execution time
import time
#import matplotlib to enable plotting histogram
import matplotlib.pyplot as plt
#lab solution
#First generate a series of numbers, for which to compute square root
#constants to determine start, stopPlusOneStep, step vallue for range function
STARTVAL
#NOTE: the STOPVAL is one STEPVAL beyond the last number generated
STOPVAL
STEPVAL STARTVAL
#first create an empty list:
nums
#generate all multiples of from to inclusive
for i in rangeSTARTVAL, STOPVAL, STEPVAL:
#append each new number to the nums list
nums.appendi
#create an empty list for the square roots computed using bisection search
bisans
#create an empty list for the number of iterations required using bisection search
bisnumit
#set the desired value of epsilon
epsilon
#implement bisection search to compute square root of all the numbers in the nums list
#determined the start time at which bisection search started
bisstarttime time.time
#use for loop to iterate through all numbers, computing for each the square root using bisection search
for i in rangelennums:
#begin assigning elements to the list keeping track of number of iterations required
#each element begins with zero, which will be successively incremented by one with each iteration
bisnumitappend
#initialize variables needed for bisection search
low
high max numsi
ans high low
#iterate the bisection search method until the estimated square root is within epsilon of correct value
while absans numsi epsilon:
#increment by one this element of the bisNumIt list
bisnumiti
#determine in which half of the search space the correct answer
if ans numsi:
low ans
else:
high ans
#now that the correct area of search space located, update the estimate of the square rootie ans
ans high low
#when the diff between the estimate and the correct answer is less than epsilon, append this to the bisans list
bisans.appendans
#determine the time at which the bisection search was completed
bisendtime time.time
#compute the execution time required to execute bisection search
bisexetime bisendtime bisstarttime
#determine ave number of iterations
sumiterations
for i in rangelenbisnumit:
sumiterations bisnumiti
aveiterations sumiterationslenbisnumit
#display results for bisection search
printUse bisection search algorithm to compute square roots of multiples of STEPVAL,in the following range:
print STARTVAL,STOPVAL STEPVAL
printMax num iterations required: maxbisnumit
printMin num iterations required: minbisnumit
#average number of iterations, rounded to four places
printAve number of iterations required: roundaveiterations,
#execute time rounded to six places
printBisection search execution time:fbisexetime:f
print
#plot histogram illustrating number iterations required for bisection search
#set up number of 'bins'
bins
#histogram of bisnumit list
plthistbisnumit bins,facecolor'green',label 'bisnumit
#formoat features of the histogram
pltlegendloc 'upper left'
pltxlabelNumber of iterations required'
pltylabelNum occurences of each iteration value'
plttitleHistogram Illustrating Number of Iterations: Bisection Search'
my code is giving me iterations for Netwon raphson instead of please help
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
