Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
77 views
ubuntu2004
Kernel: Python 3 (system-wide)
#Based on Josh Stephenson's program from class files def newton_raphson(f, f_prime, x): return x - f(x)/f_prime(x) def iterative_newton(f, f_prime, x0, tol, known_root = None): initial = x0 r = x0 iterations = 0 while abs(f(r)) > tol: #ADT: A more appriate stopping crterion is small change in iterate, not value of the function. previous_r = r r = newton_raphson(f, f_prime, r) if known_root is not None: previous_error = abs(known_root - previous_r) acloc = abs(known_root - r) if iterations > 0: print(f'ACLOC at iteration {iterations}: {math.log(acloc, 2) / math.log(previous_error, 2)}') iterations += 1 return [iterations, r]

ADT: need a text cell to explain computation and result below

from mpmath import sec from mpmath import tan f = lambda x: tan(x)- x f_prime = lambda x: (sec(x))**2 - 1 print(iterative_newton(f, f_prime, 8, 1e-10, None))
[7, mpf('10.904121659428899')]
def secant_line(f, x1, x2): return x2 - (f(x2)*(x2 - x1))/(f(x2) - f(x1)) def iterative_secant(f, x1, x2, tol, known_root = None): r = x2 iterations = 0 while abs(f(r)) > tol: previous_r = r r = secant_line(f, x1, x2) if known_root is not None: previous_error = abs(known_root - previous_r) acloc = abs(known_root - r) if iterations > 0: print(f'ACLOC at iteration {iterations}: {math.log(acloc, 2) / math.log(previous_error, 2)}') iterations += 1 return [iterations, r]
#ADT: This cell does not run properly from mpmath import tan f = lambda x: tan(x) - x print(iterative_secant(f, -2, 8, 1e-10, None))
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) /tmp/ipykernel_435/872497054.py in <cell line: 7>() 5 f = lambda x: tan(x) - x 6 ----> 7 print(iterative_secant(f, -2, 8, 1e-10, None)) /tmp/ipykernel_435/3663236574.py in iterative_secant(f, x1, x2, tol, known_root) 7 while abs(f(r)) > tol: 8 previous_r = r ----> 9 r = secant_line(f, x1, x2) 10 if known_root is not None: 11 previous_error = abs(known_root - previous_r) /tmp/ipykernel_435/3663236574.py in secant_line(f, x1, x2) 1 def secant_line(f, x1, x2): ----> 2 return x2 - (f(x2)*(x2 - x1))/(f(x2) - f(x1)) 3 4 def iterative_secant(f, x1, x2, tol, known_root = None): 5 r = x2 /tmp/ipykernel_435/872497054.py in <lambda>(x) 3 from mpmath import tan 4 ----> 5 f = lambda x: tan(x) - x 6 7 print(iterative_secant(f, -2, 8, 1e-10, None)) /usr/local/lib/python3.8/dist-packages/mpmath/ctx_mp_python.py in __sub__(self, other) /usr/local/lib/python3.8/dist-packages/mpmath/libmp/libmpf.py in mpf_sub(s, t, prec, rnd) 798 """Return the difference of two raw mpfs, s-t. This function is 799 simply a wrapper of mpf_add that changes the sign of t.""" --> 800 return mpf_add(s, t, prec, rnd, 1) 801 802 def mpf_sum(xs, prec=0, rnd=round_fast, absolute=False): /usr/local/lib/python3.8/dist-packages/mpmath/libmp/libmpf.py in mpf_add(s, t, prec, rnd, _sub) 763 man = -man 764 ssign = 1 --> 765 bc = bitcount(man) 766 return normalize1(ssign, man, sexp, bc, prec or bc, rnd) 767 # Equal exponents; no shifting necessary KeyboardInterrupt:
#ADT: This cell does not run. import scipy f = lambda x: tan(x) - x scipy.optimize.root_scalar(f)