[2022 Fall] Assignment4-2¶
Course: AP3021
In [1]:
Copied!
import matplotlib.pyplot as plt
import math
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import math
import numpy as np
import pandas as pd
import os
4-2-1¶
How many bisection iterations would be required to determine temperature to an absolute error of 0.05°C? (𝑥𝑙 = 0°C, 𝑥𝑢 = 40°C)
4-2-2¶
延續(1). Bisection program. (𝑂𝑠𝑓 = 8, 10, 𝑎𝑛𝑑 12𝑚𝑔/𝐿) (Python) PS : 溫度請帶絕對溫標
In [2]:
Copied!
def f(temperature, osf) :
absolute_temperature = temperature + 273.15 # Ta
ans = ((-8.621949 * 10 ** 11) / absolute_temperature ** 4) \
+ ((1.243800 * 10 ** 10) / absolute_temperature ** 3) \
+ ((-6.642308 * 10 ** 7) / absolute_temperature ** 2) \
+ ((1.575701 * 10 ** 5) / absolute_temperature) \
- 139.34411 - math.log(osf)
return ans
def f(temperature, osf) :
absolute_temperature = temperature + 273.15 # Ta
ans = ((-8.621949 * 10 ** 11) / absolute_temperature ** 4) \
+ ((1.243800 * 10 ** 10) / absolute_temperature ** 3) \
+ ((-6.642308 * 10 ** 7) / absolute_temperature ** 2) \
+ ((1.575701 * 10 ** 5) / absolute_temperature) \
- 139.34411 - math.log(osf)
return ans
In [3]:
Copied!
def count_et(true_value, approximation) :
true_error = true_value - approximation
et = (true_error / true_value) * 100
return et
def count_et(true_value, approximation) :
true_error = true_value - approximation
et = (true_error / true_value) * 100
return et
In [4]:
Copied!
def count_ea(new_x_root, old_x_root) :
if (old_x_root == -1) : # jump out the first data.
return 9999
else :
ea = abs((new_x_root - old_x_root) / new_x_root)
ea = ea * 100 # turn into percent
return ea
def count_ea(new_x_root, old_x_root) :
if (old_x_root == -1) : # jump out the first data.
return 9999
else :
ea = abs((new_x_root - old_x_root) / new_x_root)
ea = ea * 100 # turn into percent
return ea
In [5]:
Copied!
def count_iter_times(x_lowwer, x_upper, Ead) :
iter_times = math.log(((x_upper - x_lowwer) / Ead), 2)
return iter_times
def count_iter_times(x_lowwer, x_upper, Ead) :
iter_times = math.log(((x_upper - x_lowwer) / Ead), 2)
return iter_times
In [6]:
Copied!
def bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count) :
iter_times = count_iter_times(x_lowwer, x_upper, Ead)
print("Iterator at least:", iter_times, "times.")
x_root = -1
while True :
last_x_root = x_root
x_root = (x_lowwer + x_upper) / 2
iter_count += 1
temp = f(x_lowwer, osf) * f(x_root, osf)
# print(temp)
if (temp < 0) :
x_upper = x_root
# print("here")
elif(temp > 0) :
x_lowwer = x_root
# print("here2")
else :
return x_root
# how to get the true_value?
# true_value =
# et = (count_et(true_value, x_root))
ea = count_ea(x_root, last_x_root)
print("count:", iter_count, "root:", x_root, "ea", ea)
# print(x_lowwer, x_upper)
if iter_count >= iter_times or iter_count >= iter_max:
temperature = x_root + 273.15
print(f"if the os is {osf}\nI iterate {iter_count} times\nThe temperature is {temperature}K")
return x_root
def bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count) :
iter_times = count_iter_times(x_lowwer, x_upper, Ead)
print("Iterator at least:", iter_times, "times.")
x_root = -1
while True :
last_x_root = x_root
x_root = (x_lowwer + x_upper) / 2
iter_count += 1
temp = f(x_lowwer, osf) * f(x_root, osf)
# print(temp)
if (temp < 0) :
x_upper = x_root
# print("here")
elif(temp > 0) :
x_lowwer = x_root
# print("here2")
else :
return x_root
# how to get the true_value?
# true_value =
# et = (count_et(true_value, x_root))
ea = count_ea(x_root, last_x_root)
print("count:", iter_count, "root:", x_root, "ea", ea)
# print(x_lowwer, x_upper)
if iter_count >= iter_times or iter_count >= iter_max:
temperature = x_root + 273.15
print(f"if the os is {osf}\nI iterate {iter_count} times\nThe temperature is {temperature}K")
return x_root
Osf = 8¶
In [7]:
Copied!
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 8
iter_max = 500
iter_count = 0
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 8
iter_max = 500
iter_count = 0
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
Iterator at least: 9.643856189774725 times. count: 1 root: 20.0 ea 9999 count: 2 root: 30.0 ea 33.33333333333333 count: 3 root: 25.0 ea 20.0 count: 4 root: 27.5 ea 9.090909090909092 count: 5 root: 26.25 ea 4.761904761904762 count: 6 root: 26.875 ea 2.3255813953488373 count: 7 root: 26.5625 ea 1.1764705882352942 count: 8 root: 26.71875 ea 0.5847953216374269 count: 9 root: 26.796875 ea 0.2915451895043732 count: 10 root: 26.7578125 ea 0.145985401459854 if the os is 8 I iterate 10 times The temperature is 299.9078125K
Osf = 10¶
In [8]:
Copied!
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 10
iter_max = 500
iter_count = 0
print("Osf:", osf)
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
print(ans)
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 10
iter_max = 500
iter_count = 0
print("Osf:", osf)
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
print(ans)
Osf: 10 Iterator at least: 9.643856189774725 times. count: 1 root: 20.0 ea 9999 count: 2 root: 10.0 ea 100.0 count: 3 root: 15.0 ea 33.33333333333333 count: 4 root: 17.5 ea 14.285714285714285 count: 5 root: 16.25 ea 7.6923076923076925 count: 6 root: 15.625 ea 4.0 count: 7 root: 15.3125 ea 2.0408163265306123 count: 8 root: 15.46875 ea 1.0101010101010102 count: 9 root: 15.390625 ea 0.5076142131979695 count: 10 root: 15.3515625 ea 0.2544529262086514 if the os is 10 I iterate 10 times The temperature is 288.5015625K 15.3515625
Osf = 12¶
In [9]:
Copied!
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 12
iter_max = 500
iter_count = 0
print("Osf:", osf)
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
print(ans)
x_lowwer = 0
x_upper = 40
Ead = 0.05
osf = 12
iter_max = 500
iter_count = 0
print("Osf:", osf)
ans = bisection(x_lowwer, x_upper, Ead, osf, iter_max, iter_count)
print(ans)
Osf: 12 Iterator at least: 9.643856189774725 times. count: 1 root: 20.0 ea 9999 count: 2 root: 10.0 ea 100.0 count: 3 root: 5.0 ea 100.0 count: 4 root: 7.5 ea 33.33333333333333 count: 5 root: 6.25 ea 20.0 count: 6 root: 6.875 ea 9.090909090909092 count: 7 root: 7.1875 ea 4.3478260869565215 count: 8 root: 7.34375 ea 2.127659574468085 count: 9 root: 7.421875 ea 1.0526315789473684 count: 10 root: 7.4609375 ea 0.5235602094240838 if the os is 12 I iterate 10 times The temperature is 280.6109375K 7.4609375
In [ ]:
Copied!
Last update:
2024-04-27