My question is python 2.7. I\'ve done the top but cannot figure out the rest. He
ID: 3786752 • Letter: M
Question
My question is python 2.7. I've done the top but cannot figure out the rest. Here is the code.
from __future__ import division
import math
def close_enough(x, y, error = 0.0000001):
return abs(x - y) < error
def search_for_midpoint(f, neg_point, pos_point):
midpoint = (neg_point + pos_point)/2
if close_enough(neg_point, pos_point):
return midpoint
else:
## NEED CODE HERE
def half_interval_method(f, a, b):
a_val = f(a)
b_val = f(b)
if a_val < 0 and b_val > 0:
return search_for_midpoint(f, a, b)
elif b_val < 0 and a_val > 0:
return search_for_midpoint(f, b, a)
else:
raise Exception('Values are not of oppositive sign')
def fixed_point(f, guess, error=0.0000000000001):
prev_guess = guess
curr_guess = f(prev_guess)
num_iters = 0
while not close_enough(prev_guess, curr_guess, error=error):
##NEED CODE HERE
return (num_iters, curr_guess)
def fixed_point_sqrt(x):
return fixed_point(lambda y: (y + x/y)/2, 1.0)
def fixed_point_golden_ratio():
return fixed_point(lambda x: 1 + 1/x, 1.0)
def fixed_point_golden_ratio_with_avrg_damping():
## NEED CODE HERE
pass
def fixed_point_log(y, guess):
## NEED CODE HERE
pass
def fixed_point_log_with_avrg_damping(y, guess):
## NEED CODE HERE
pass
def fixed_point_logs_with_avrg_damping(x, y, guess):
## NEED CODE HERE
pass
"NEED CODE HERE" is what I need it.
Explanation / Answer
import math
def close_enough(x, y, error = 0.4):
return abs(x - y) < error
## Not used
def search_for_midpoint(f, neg_point, pos_point):
midpoint = (neg_point + pos_point)/2
return midpoint
def half_interval_method(f, a, b, error = 0.4):
a_val = f(a)
b_val = f(b)
## If the func of a and b are not of opposite sign then raise exception
if a_val * b_val > 0:
raise Exception('Values are not of oppositive sign')
## loop to find nearest value to x-axis
while not (close_enough(a, b, error)):
x = ( a + b ) / 2.0
fun = f(x)
##print x, fun
if fun*a_val > 0: a=x
else: b=x
return x
def fixed_point(f, guess, error=0.0000000000001, n=10):
itr=0
prev_guess = guess
curr_guess = f(prev_guess)
num_iters = 0
if(close_enough(curr_guess, prev_guess, error)):
return curr_guess
## Iterating by changing func(prev_guess) as curr_guess till we get the closest value
while not (n>=itr and close_enough(prev_guess, curr_guess, error=error)):
## itr to stop at 10th iteration, used to avoid unnecessary looping
itr+=1
prev_guess = curr_guess
curr_guess = f(prev_guess)
##print "Guess:",curr_guess
if(close_enough(curr_guess, prev_guess, error)):
return curr_guess
return None
if __name__ == '__main__':
# print half_interval_method(f, -1, 5, 0.3)
# print fixed_point(lambda x:x**2, 0.5)
# print fixed_point_sqrt(5)
pass
Comments : I have written the code for 2 methods (half_interval_method, fixed_point) since you have asked more than one question here, it is ok for us to answer first question only. Since the 2nd method is the key to other answers, I have provided you with the 2nd method also. Please check the comments in the code to understand it. And please post single question at a time.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.