[2022 Fall] Assignment4-4¶
Course: AP3021
In [1]:
Copied!
import math
import matplotlib.pyplot as plt
import math
import matplotlib.pyplot as plt
In [2]:
Copied!
def g(x) :
ans = math.sqrt((1.7 * x + 2.5) / 0.9)
return ans
def g(x) :
ans = math.sqrt((1.7 * x + 2.5) / 0.9)
return ans
In [3]:
Copied!
def fix_point(x0, es, iter_max, ea_list, iter_count_list) :
x_root = x0
iter_count = 0
while True :
last_x_root = x_root
x_root = g(last_x_root)
iter_count += 1
iter_count_list.append(iter_count)
if (x_root != 0) :
ea = abs((x_root - last_x_root) / x_root) * 100
ea_list.append(ea)
print("iter time:", iter_count, ",ea =", ea)
if (ea < es or iter_count >= iter_max) :
return x_root
def fix_point(x0, es, iter_max, ea_list, iter_count_list) :
x_root = x0
iter_count = 0
while True :
last_x_root = x_root
x_root = g(last_x_root)
iter_count += 1
iter_count_list.append(iter_count)
if (x_root != 0) :
ea = abs((x_root - last_x_root) / x_root) * 100
ea_list.append(ea)
print("iter time:", iter_count, ",ea =", ea)
if (ea < es or iter_count >= iter_max) :
return x_root
In [4]:
Copied!
x0 = 5
es = 0.01
iter_max = 500
ea_list = []
iter_count_list = []
print("\nThe approximate ans:", fix_point(x0, es, iter_max, ea_list, iter_count_list))
# print(ea_list)
# print(iter_count)
x0 = 5
es = 0.01
iter_max = 500
ea_list = []
iter_count_list = []
print("\nThe approximate ans:", fix_point(x0, es, iter_max, ea_list, iter_count_list))
# print(ea_list)
# print(iter_count)
iter time: 1 ,ea = 43.019388386838855 iter time: 2 ,ea = 14.140958348733184 iter time: 3 ,ea = 4.6679854963306715 iter time: 4 ,ea = 1.541483060629453 iter time: 5 ,ea = 0.5090321399985364 iter time: 6 ,ea = 0.16809099521169962 iter time: 7 ,ea = 0.05550608802781297 iter time: 8 ,ea = 0.01832887042630157 iter time: 9 ,ea = 0.006052438895087782 The approximate ans: 2.8601897496224673
In [5]:
Copied!
# plot
x = iter_count_list
y = ea_list
plt.plot(x, y)
plt.xlim(0, 10)
plt.ylim(0, 50)
plt.title("Growing of ea by fixed point iteration")
plt.grid()
plt.legend(["ea"], loc ="upper right")
plt.savefig("./src/imgs/A4_4_a.png", dpi=300)
plt.show()
# plot
x = iter_count_list
y = ea_list
plt.plot(x, y)
plt.xlim(0, 10)
plt.ylim(0, 50)
plt.title("Growing of ea by fixed point iteration")
plt.grid()
plt.legend(["ea"], loc ="upper right")
plt.savefig("./src/imgs/A4_4_a.png", dpi=300)
plt.show()
In [6]:
Copied!
def f(x) :
ans = -0.9 * x ** 2 + 1.7 * x + 2.5
return ans
def f(x) :
ans = -0.9 * x ** 2 + 1.7 * x + 2.5
return ans
In [7]:
Copied!
def f_prime(x) :
ans = -1.8 * x + 1.7
return ans
def f_prime(x) :
ans = -1.8 * x + 1.7
return ans
In [8]:
Copied!
def newton_raphson(x0, es, iter_max, ea_list, iter_count_list) :
iter_count = 0
while True :
next_x = x0 - (f(x0) / f_prime(x0))
x_root = next_x
iter_count += 1
iter_count_list.append(iter_count)
ea = abs((x_root - x0) / x_root) * 100
x0 = x_root
ea_list.append(ea)
print("iter time:", iter_count, ",ea =", ea)
if (ea < es or iter_count >= iter_max) :
return x_root
def newton_raphson(x0, es, iter_max, ea_list, iter_count_list) :
iter_count = 0
while True :
next_x = x0 - (f(x0) / f_prime(x0))
x_root = next_x
iter_count += 1
iter_count_list.append(iter_count)
ea = abs((x_root - x0) / x_root) * 100
x0 = x_root
ea_list.append(ea)
print("iter time:", iter_count, ",ea =", ea)
if (ea < es or iter_count >= iter_max) :
return x_root
In [9]:
Copied!
x0 = 5
es = 0.01
iter_max = 500
ea_list = []
iter_count_list = []
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max, ea_list, iter_count_list))
x0 = 5
es = 0.01
iter_max = 500
ea_list = []
iter_count_list = []
print("\nThe approximate ans:", newton_raphson(x0, es, iter_max, ea_list, iter_count_list))
iter time: 1 ,ea = 46.0 iter time: 2 ,ea = 17.108052750727655 iter time: 3 ,ea = 2.209254593287688 iter time: 4 ,ea = 0.03644225367328207 iter time: 5 ,ea = 9.913886563630006e-06 The approximate ans: 2.8601044055074283
In [10]:
Copied!
# plot
x = iter_count_list
y = ea_list
plt.plot(x, y)
plt.xlim(0, 5)
plt.ylim(0, 50)
plt.title("Growing of ea by Newton Raphson")
plt.grid()
plt.legend(["ea"], loc ="upper right")
plt.savefig("./src/imgs/A4_4_b.png", dpi=300)
plt.show()
# plot
x = iter_count_list
y = ea_list
plt.plot(x, y)
plt.xlim(0, 5)
plt.ylim(0, 50)
plt.title("Growing of ea by Newton Raphson")
plt.grid()
plt.legend(["ea"], loc ="upper right")
plt.savefig("./src/imgs/A4_4_b.png", dpi=300)
plt.show()
Last update:
2024-04-27