381 - A5 - Secant Method

390 days ago by Professor381

# f = x^2 - 16 f = x^2 - x -1 # exact = 4 exact = (1+sqrt(1+4))/2 # f = tan(x/4)-1 # exact = pi (plot(f,(x,0,5))).show() 
       
# x0 = 1 # x1 = 5 x0 = 1 x1 = 3 points = plot(f,(x,0,x0)) points += point((x0,0),color='red',size=40)+point((x1,0),color='red',size=40) show(points) 
       
p0 = x0 p1 = x1 errors = [] Lines = Graphics() for k in range(6): fx = f(x=p1) m = n((fx-f(x=p0))/(p1-p0)) p0 = p1 p1 = p1 - fx/m pretty_print(p1) points += point((p1,0),color='red',size=40) if p1<p0: Lines += plot(fx+m*(x-p0),(x,p1,p0),color='green') else: Lines += plot(fx+m*(x-p0),(x,p0,p1),color='green') errors.append(n(abs(p1-exact)/exact)) end show(points+Lines) pretty_print((p1,f(x=p1))) pretty_print(' or ',(n(p1),n(f(x=p1)))) pretty_print(errors) 
       

                                
                            

                                
pretty_print(' or ',(n(p1),n(f(x=p1)))) 
       

                                
                            

                                

Experimentally determine the rate of convergence

# Experiment with r to see if there is a (relatively) constant value for the following ratio of successive error terms r = 1.62 for k in range(len(errors)-1): errors[k+1]/errors[k]^r 
       
0.427067837796310
0.919356447350661
0.540333683081218
0.754621891141515
0.632238125507041
0.427067837796310
0.919356447350661
0.540333683081218
0.754621891141515
0.632238125507041