-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.py
executable file
·31 lines (30 loc) · 1.21 KB
/
test3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: 0.1*x**3 - 0.8*x**2 - 1.5*x + 5.4
fd = lambda x: 0.3*x**2 - 1.6*x - 1.5
fdd = lambda x: 0.6*x - 1.6
viz_range = np.array([-6, 12])
max_iter = 100
min_tol = 1e-6
x_init = -2 # Try -2, 0, and 16/6 (a saddle point)
# Prepare visualization
xs = np.linspace(*viz_range, 100)
plt.plot(xs, f(xs), 'r-', label='f(x)', linewidth=2)
plt.plot(x_init, f(x_init), 'b.', label='Each step', markersize=12)
plt.axis((*viz_range, *f(viz_range)))
plt.legend()
x = x_init
for i in range(max_iter):
# Run the Newton method
xp = x
x = x - fd(x) / abs(fdd(x)+1) # Replace the denominator as abs(fdd(x)) and (abs(fdd(x)) + 1) to resolve the maxima and saddle point problems
# Update visualization for each iteration
print(f'Iter: {i}, x = {xp:.3f} to {x:.3f}, f(x) = {f(xp):.3f} to {f(x):.3f} (f\'(x) = {fd(xp):.3f}, f\'\'(x) = {fdd(xp):.3f})')
lcolor = np.random.rand(3)
approx = 0.5*fdd(xp)*(xs-xp)**2 + fd(xp)*(xs-xp) + f(xp)
plt.plot(xs, approx, '-', linewidth=1, color=lcolor, alpha=0.8)
plt.plot(x, f(x), '.', color=lcolor, markersize=12)
# Check the terminal condition
if abs(x - xp) < min_tol: break;
plt.show()