[2022 Fall] Assignment4-5¶
Course: AP3021
4-5-1¶
Use the Newton-Raphson method to find the root of $f(x) = e^{-0.5x} (4 - x) - 2$
4-5-2¶
Employ initial guesses of (a) 2, (b) 6, and (c) 8. Explain your results. (Python+解釋) Hint: Think about the problems of this method.
In [1]:
Copied!
import math
import math
In [2]:
Copied!
def f(x) :
e = math.e
ans = ((e ** (-0.5 * x)) * (4 - x)) - 2
return ans
def f_prime(x) :
e = math.e
ans = (-0.5 * (e ** (-0.5 * x)) * (4 - x)) - (e ** (-0.5 * x))
return ans
def newton_raphson(x0, es, iter_max) :
iter_count = 0
iter_count_list = []
x_root = x0
print("x0 =", x0)
print()
while True :
last_x_root = x_root
try :
x_root = last_x_root - (f(x_root) / f_prime(x_root))
except :
print("total use", iter_count, "times.")
return "Divergence"
iter_count += 1
iter_count_list.append(iter_count)
if x_root != 0 :
ea = abs((x_root - last_x_root) / x_root) * 100
print("iter time:", iter_count, ",ea =", ea, "root:", x_root)
if (ea < es or iter_count >= iter_max) :
print("total use", iter_count, "times.")
return x_root
def f(x) :
e = math.e
ans = ((e ** (-0.5 * x)) * (4 - x)) - 2
return ans
def f_prime(x) :
e = math.e
ans = (-0.5 * (e ** (-0.5 * x)) * (4 - x)) - (e ** (-0.5 * x))
return ans
def newton_raphson(x0, es, iter_max) :
iter_count = 0
iter_count_list = []
x_root = x0
print("x0 =", x0)
print()
while True :
last_x_root = x_root
try :
x_root = last_x_root - (f(x_root) / f_prime(x_root))
except :
print("total use", iter_count, "times.")
return "Divergence"
iter_count += 1
iter_count_list.append(iter_count)
if x_root != 0 :
ea = abs((x_root - last_x_root) / x_root) * 100
print("iter time:", iter_count, ",ea =", ea, "root:", x_root)
if (ea < es or iter_count >= iter_max) :
print("total use", iter_count, "times.")
return x_root
a. $x_0 = 2$¶
In [3]:
Copied!
x0 = 2
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 2
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 2 iter time: 1 ,ea = 609.9293556607687 root: 0.2817181715409549 iter time: 2 ,ea = 63.73755414477369 root: 0.7768868450453745 iter time: 3 ,ea = 11.888408438696143 root: 0.8817078789285671 iter time: 4 ,ea = 0.4510949099401444 root: 0.8857032411666447 iter time: 5 ,ea = 0.000627839236345159 root: 0.8857088019940234 total use 5 times. The approximate ans: 0.8857088019940234
b. $x_0 = 6$¶
In [4]:
Copied!
x0 = 6
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 6
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 6 total use 0 times. The approximate ans: Divergence
c. $x_0 = 8$¶
In [5]:
Copied!
x0 = 8
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 8
es = 0.01
iter_max = 500
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max))
x0 = 8 iter time: 1 ,ea = 93.39913842615294 root: 121.19630006628846 iter time: 2 ,ea = 100.0 root: 7.212131452880089e+24 total use 2 times. The approximate ans: Divergence
Last update:
2024-04-27