[2022 Fall] Assignment4-6¶
Course: AP3021
4-6-1¶
If R = 3 m, what depth must the tank be filled to so that it holds 30 𝑚3?
4-6-2¶
Newton-Raphson method (3 iterations; determine relative error after each iterations)(Python)
In [1]:
Copied!
import math
import math
In [2]:
Copied!
def f(h, R, V) :
PI = math.pi
ans = PI * (h ** 2) * (((3 * R) - h) / 3) - V
return ans
def f(h, R, V) :
PI = math.pi
ans = PI * (h ** 2) * (((3 * R) - h) / 3) - V
return ans
In [3]:
Copied!
def f_prime(h, R) :
PI = math.pi
ans = (2 * PI * h * R) - (PI * (h ** 2))
return ans
def f_prime(h, R) :
PI = math.pi
ans = (2 * PI * h * R) - (PI * (h ** 2))
return ans
In [4]:
Copied!
def newton_raphson(x0, es, iter_max, R, V) :
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, R, V) / f_prime(x_root, R))
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 newton_raphson(x0, es, iter_max, R, V) :
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, R, V) / f_prime(x_root, R))
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
In [5]:
Copied!
R = 3
V = 30
x0 = R
es = 0.01
iter_max = 3
print("\nThe constrains of h:", newton_raphson(x0, es, iter_max, R, V))
R = 3
V = 30
x0 = R
es = 0.01
iter_max = 3
print("\nThe constrains of h:", newton_raphson(x0, es, iter_max, R, V))
x0 = 3 iter time: 1 ,ea = 45.55808019742351 root: 2.061032953945969 iter time: 2 ,ea = 1.6768713794196717 root: 2.0270420656974903 iter time: 3 ,ea = 0.0067262694875214685 root: 2.026905730555795 total use 3 times. The constrains of h: 2.026905730555795
4-6-3¶
What are the constraints of h?
In [6]:
Copied!
print("\nThe constrains of h:", newton_raphson(x0, es, iter_max, R, V))
print("\nThe constrains of h:", newton_raphson(x0, es, iter_max, R, V))
x0 = 3 iter time: 1 ,ea = 45.55808019742351 root: 2.061032953945969 iter time: 2 ,ea = 1.6768713794196717 root: 2.0270420656974903 iter time: 3 ,ea = 0.0067262694875214685 root: 2.026905730555795 total use 3 times. The constrains of h: 2.026905730555795
In [ ]:
Copied!
Last update:
2024-04-27