381 - A3 - Bisection

449 days ago by Professor381

f = tan(x/4)-1 plot(f,(x,0,5)).show() 
       
a = 0 b = 5 points = Graphics() points += point((a,0),color='red',size=40)+point((b,0),color='blue',size=40) show(points) 
       
fa = f(a) fb = f(b) for k in range(230): p = (a+b)/2 fp = f(p) if f(p)==0: break if f(p)*f(a)<0: b = p else: a = p end points += point((a,0),color='red',size=40)+point((b,0),color='blue',size=40) show(points) pretty_print((p,fp),' or ',(n(p),n(fp))) pretty_print('Final interval width is= ',n(b-a))