Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def f(x):
- return (2*x**4+x**2+2)/(x**4+1)
- def f_prime(x):
- return -4*x**3*(2*x**4 + x**2 + 2)/(x**4 + 1)**2 + (8*x**3 + 2*x)/(x**4 + 1)
- def f_2prime(x):
- return 32*x**6*(2*x**4 + x**2 + 2)/(x**4 + 1)**3 - 8*x**3*(8*x**3 + 2*x)/(x**4 + 1)**2 -12*x**2*(2*x**4 + x**2 + 2)/(x**4 + 1)**2 + (24*x**2 + 2)/(x**4 + 1)
- def newton_f_opt(x0, f_prime, f_2prime, maxiter = 100, tol = 0.0001):
- x = x0
- Nter = 0
- for i in range(maxiter):
- Nter += 1
- x_next = x - (f_prime(x)/f_2prime(x))
- if x_next - x < tol:
- return x_next, Nter+1
- print('Optimization terminated successfully.')
- print('Current parameter value: ' + str(x))
- print('Iterations: ' + str(i))
- break
- x = x_next
- if i == maxiter - 1 and x_next - x < tol:
- print('Optimization terminated after maximum number of iterations.')
- return x, Nter+1
- x_star, Niter = newton_f_opt(0, f_prime, f_2prime)
- print(x_star)
- print(f(x_star))
- print(Niter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement