• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jul 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def find_roots(method,a,b,segments=10):
2.     seg = np.linspace(a,b,segments+1);
3.     for k in range(segments):
4.         ak, bk = seg[k:k+2];
5.         #print "searching for roots in",[ak,bk]
6.         x = (ak+bk)/2;
7.         count = 0;
8.         while ak<=x<=bk and count < 50:
9.             count += 1;
10.             xold, x = x, method(x);
11.             #print x
12.             if count==2 and abs(x-xold)>1e-1*(bk-ak): break;
13.             if abs(x-xold)<1e-8:
14.                 y,_,_ = f(x)
15.                 print "found root x=%.15f with f(x)=%.8e in %d iterations"%(x,y,count);
16.                 break;
17.
18. find roots with Newton step
19. found root x=3.000000007315551 with f(x)=-3.77475828e-15 in 23 iterations
20. found root x=10.999999991701889 with f(x)=-3.33066907e-16 in 23 iterations
21. find roots with Halley step
22. found root x=3.000000004913715 with f(x)=-1.66533454e-15 in 15 iterations
23. found root x=10.999999999234854 with f(x)=0.00000000e+00 in 16 iterations
24. find roots with Newton plus double Newton step
25. found root x=2.999999999980970 with f(x)=0.00000000e+00 in 4 iterations
26. found root x=10.999999999997232 with f(x)=0.00000000e+00 in 3 iterations
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top