Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import mlab
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6.  
  7. def f(x):
  8. return 6*x**5-5*x**4-4*x**3+3*x**2
  9.  
  10. def df(x):
  11. return 30*x**4-20*x**3-12*x**2+6*x
  12.  
  13. def dx(f, x):
  14. return abs(0 - f(x))
  15.  
  16. def tangent(x):
  17. return -0.0625 - 1.625 * x
  18.  
  19. def newtons_method(f, df, x0, e):
  20. delta = dx(f, x0)
  21. while delta > e:
  22. x0 = x0 - f(x0)/df(x0)
  23. delta = dx(f, x0)
  24. print('root: ', x0)
  25. print('f(x) = ', f(x0))
  26.  
  27.  
  28. def plot(point):
  29. xmin = -1.0
  30. xmax = 1.5
  31.  
  32. dx = 0.01
  33.  
  34. xlist = mlab.frange(xmin, xmax, dx)
  35.  
  36. ylist1 = [f(x) for x in xlist]
  37. ylist2 = [tangent(x) for x in xlist]
  38.  
  39. plt.figure("Newtone's method")
  40. plt.plot(xlist, ylist1)
  41.  
  42. scatter1 = plt.scatter(0.0, 0.0)
  43. scatter2 = plt.scatter(point, 0.0)
  44.  
  45. plt.errorbar(point, 0, xerr=0.08, yerr=1)
  46. plt.plot(xlist, ylist2)
  47.  
  48. plt.tight_layout()
  49. plt.grid()
  50. plt.show()
  51.  
  52.  
  53. def main():
  54. x0s = float(input('>> '))
  55. newtons_method(f, df, x0s, 1e-5)
  56. plot(x0s)
  57.  
  58. if __name__ == '__main__':
  59. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement