Advertisement
Guest User

newton

a guest
Jan 23rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import numpy as np
  2. from pylab import *
  3.  
  4. def f(x):
  5. return (x+1)**4+4*sin(x)
  6. def d2f(x):
  7. return (12*(x+1)**(2))-4*sin(x)
  8. def df(x):
  9. return 4*cos(x)+4*(x+1)**3
  10. x=linspace(-3,1,100)
  11. plot(x, f(x), 'c-')
  12. grid()
  13. show()
  14.  
  15. def N(x,eps,MI=20):
  16. if f(x)*d2f(x)<0:
  17. print 'Newtonova metoda ne konvergira.'
  18. else:
  19. print '0. iteracija: %s' % x
  20. i=1
  21. while i<=MI:
  22. if abs(df(x))<eps:
  23. print 'Horizontalna tangenta'
  24. break
  25. dx=f(x)/df(x)
  26. x=x-dx
  27. print '%s. iteracija: %s' % (i,x)
  28. if abs(dx)<eps:
  29. return x,i
  30. else:
  31. i=i+1
  32. print 'Rjesenje nije nadjeno u zadanom broju iteracija.'
  33. xn,i=N(-2.5,1.e-15)
  34. print 'Rjesenje1:'
  35. print 'x%s=%s' % (i,xn)
  36. print '\n'
  37. xm,j=N(0.,1.e-15)
  38. print 'Rjesenje2:'
  39. print 'x%s=%s' % (j,xm)
  40. print '\n'
  41.  
  42. x=linspace(-3,1,100)
  43. plot(x, f(x), 'r-',xn,f(xn),'m^',xm,f(xm),'m^')
  44. grid()
  45. show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement