Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def f(x):
  2.     return 4*x - 1.8*x*x + 1.2*x*x*x + - 0.3*x*x*x*x
  3.  
  4.  
  5. def f_prime(x):
  6.     return -1.2*x*x*x + 3.6*x*x - 3.6*x + 4
  7.  
  8.  
  9. def f_prime_prime(x):
  10.     return -3.6*x*x + 7.2*x - 3.6
  11.  
  12.  
  13. def newton(x0, max_error, iterations):
  14.     x = x0
  15.     error = 1
  16.     ite = 0
  17.     while error > max_error and ite < iterations:
  18.         temp = x
  19.         x = x - f_prime(x) / f_prime_prime(x)
  20.         ite += 1
  21.         error = abs(x - temp) / x
  22.     return [x, f(x), error, ite]
  23.  
  24.  
  25. res = newton(0, 1e-2, 100)
  26.  
  27. print('The maximum of function f(x) is', res[1], 'at x =', res[0])
  28. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement