SHARE
TWEET

Untitled

a guest May 21st, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from matplotlib import pyplot as plt
  2. import numpy as np
  3. from scipy import optimize as opt
  4.  
  5. f = lambda x: np.sin(x**2)-6*x+1
  6. phi = lambda x: (np.sin(x**2)+1)/6
  7. x = np.arange(0, 1, 0.1)
  8. plt.plot(x, f(x), x, np.zeros(len(x)))
  9. plt.show()
  10.  
  11.  
  12. def fixed_point_iter(f, x0, eps):
  13.      x = x0
  14.      while True:
  15.          y = f(x)
  16.  
  17.          if abs(y - x) < eps:
  18.              return y
  19.          else:
  20.              x = y
  21.  
  22.  
  23. my_sol = fixed_point_iter(phi, 2, 1e-6)
  24. print(my_sol)
  25.  
  26. sc_sol = opt.root_scalar(f, method='brentq', bracket=[1, 2]).root
  27. print(sc_sol)
  28.  
  29. print(abs(sc_sol - my_sol))
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. OK, I Understand
 
Top