Advertisement
Guest User

newton

a guest
Oct 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. def f(x):
  5.     return np.log(x)
  6. x0, x, x1, x2  = 1, 2, 4, 6
  7.  
  8. A0 = f(x0)
  9. A1 = ((f(x1) - f(x0)) / (x1 - x0))
  10. A2 = (((f(x2) - f(x1)) / (x2 - x1)) - A1) / (x2 - x0)
  11.  
  12. result = A0 + A1 * (x - x0) + A2 * ((x - x0) * (x - x1))
  13.  
  14. print(result)
  15.  
  16. val_x = np.linspace(.01, 5, num = 100)
  17. plt.plot(val_x, f(val_x), 'b')
  18. plt.plot(2, result, 'ro')
  19. plt.plot(2, f(2), 'go')
  20. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement