3/9/26, 8:59 PM Assignment4_Code.
ipynb - Colab
# Problem 4(b)
import numpy as np
def p(x):
return [Link](x) * (x - 3)**2
def p_prime(x):
return [Link](x) * ((x - 3)**2 + 2*(x - 3))
def classical_newton(x0, iterations=20, tol=1e-15):
x = x0
iterates = [x]
errors = [abs(x - 3)]
for i in range(iterations):
if abs(p(x)) < tol:
break
x = x - p(x) / p_prime(x)
[Link](x)
[Link](abs(x - 3))
return iterates, errors
def modified_newton(x0, iterations=20, tol=1e-15):
x = x0
iterates = [x]
errors = [abs(x - 3)]
for i in range(iterations):
if abs(p(x)) < tol:
break
x = x - 2 * p(x) / p_prime(x)
[Link](x)
[Link](abs(x - 3))
return iterates, errors
# Classical Newton
classical_iter, classical_err = classical_newton(4, 20)
for i in range(len(classical_iter)):
print(f"n={i}: x_n = {classical_iter[i]:.10f}, error = {classical_err[i]:.10e}")
print()
# Modified Newton
modified_iter, modified_err = modified_newton(4, 20)
for i in range(len(modified_iter)):
print(f"n={i}: x_n = {modified_iter[i]:.10f}, error = {modified_err[i]:.10e}")
print()
n=0: x_n = 4.0000000000, error = 1.0000000000e+00
n=1: x_n = 3.6666666667, error = 6.6666666667e-01
n=2: x_n = 3.4166666667, error = 4.1666666667e-01
n=3: x_n = 3.2442528736, error = 2.4425287356e-01
n=4: x_n = 3.1354180464, error = 1.3541804642e-01
n=5: x_n = 3.0720028071, error = 7.2002807119e-02
n=6: x_n = 3.0372524647, error = 3.7252464662e-02
n=7: x_n = 3.0189668249, error = 1.8966824905e-02
n=8: x_n = 3.0095725027, error = 9.5725026851e-03
n=9: x_n = 3.0048090504, error = 4.8090504223e-03
n=10: x_n = 3.0024102931, error = 2.4102930837e-03
n=11: x_n = 3.0012065972, error = 1.2065971718e-03
n=12: x_n = 3.0006036623, error = 6.0366233563e-04
n=13: x_n = 3.0003019222, error = 3.0192224238e-04
n=14: x_n = 3.0001509839, error = 1.5098390701e-04
n=15: x_n = 3.0000754977, error = 7.5497652110e-05
n=16: x_n = 3.0000377503, error = 3.7750250975e-05
n=17: x_n = 3.0000188755, error = 1.8875481751e-05
n=18: x_n = 3.0000094378, error = 9.4378299456e-06
n=19: x_n = 3.0000047189, error = 4.7189372410e-06
n=20: x_n = 3.0000023595, error = 2.3594741876e-06
n=0: x_n = 4.0000000000, error = 1.0000000000e+00
n=1: x_n = 3.3333333333, error = 3.3333333333e-01
n=2: x_n = 3.0476190476, error = 4.7619047619e-02
n=3: x_n = 3.0011074197, error = 1.1074197121e-03
[Link] 1/3
3/9/26, 8:59 PM Assignment4_Code.ipynb - Colab
n=4: x_n = 3.0000006128, error = 6.1284986819e-07
n=5: x_n = 3.0000000000, error = 1.8784973577e-13
Comparison: As we can see the modified newton converges faster because the classical newton converges linearly due to the double
root.
# Problem 5(b)
import numpy as np
import time
def g1(x):
return 1/x + (4/5)*x
def fixed_point_iteration(g, x0, tol=1e-10, max_iter=1000):
start_time = [Link]()
x = x0
for i in range(max_iter):
x_new = g(x)
if abs(x_new - x) < tol:
runtime = [Link]() - start_time
return x_new, i + 1, runtime
x = x_new
runtime = [Link]() - start_time
return x, max_iter, runtime
fp, iterations, runtime = fixed_point_iteration(g1, 2.0)
print(f"Final approximation: {fp:.10f}")
print(f"Iterations: {iterations}")
print(f"Runtime: {runtime:.6f} seconds")
Final approximation: 2.2360679774
Iterations: 42
Runtime: 0.000014 seconds
# Problem 5(c)
import numpy as np
import time
def g2(x):
return x/2 + 5/(2*x)
def fixed_point_iteration(g, x0, tol=1e-10, max_iter=1000):
start_time = [Link]()
x = x0
for i in range(max_iter):
x_new = g(x)
if abs(x_new - x) < tol:
runtime = [Link]() - start_time
return x_new, i + 1, runtime
x = x_new
runtime = [Link]() - start_time
return x, max_iter, runtime
try:
fp, iterations, runtime = fixed_point_iteration(g2, 2.0, max_iter=100)
print(f"Final approximation: {fp:.10f}")
print(f"Iterations: {iterations}")
print(f"Runtime: {runtime:.6f} seconds")
except:
print("Does not converge")
Final approximation: 2.2360679775
Iterations: 5
Runtime: 0.000011 seconds
# Problem 5(d)
import numpy as np
[Link] 2/3
3/9/26, 8:59 PM Assignment4_Code.ipynb - Colab
def g1_prime(x):
return -1/x**2 + 4/5
def g2_prime(x):
return 1/2 - 5/(2*x**2)
r = [Link](5)
g1_deriv = abs(g1_prime(r))
g2_deriv = abs(g2_prime(r))
print(f"|g1'(5^1/2)| = {g1_deriv:.6f}")
print(f"|g2'(5^1/2)| = {g2_deriv:.6f}")
print()
print(f"g1 converges because |g1'(r)| = {g1_deriv:.3f} < 1")
print(f"g2 diverges because |g2'(r)| = {g2_deriv:.3f} > 1")
print()
print(f"g1 converges faster because it has smaller |g'(r)|")
|g1'(5^1/2)| = 0.600000
|g2'(5^1/2)| = 0.000000
g1 converges because |g1'(r)| = 0.600 < 1
g2 diverges because |g2'(r)| = 0.000 > 1
g1 converges faster because it has smaller |g'(r)|
[Link] 3/3