Trần Vũ Khánh Hưng ITCSIU21182 TMC Lab2
Trần Vũ Khánh Hưng ITCSIU21182 TMC Lab2
Trần Vũ Khánh Hưng ITCSIU21182 TMC Lab2
In [ ]: # question a
import numpy as np
import matplotlib.pyplot as plt
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 1/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
In [ ]: import math
def equation(x):
return math.log(pow(x, 4)) - 0.7
x_low = 0.5
x_up = 2
iterations = 3
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 2/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
Approximated positive real root using false-position method: 1.217533717387
9655
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 3/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 4/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
In [1]: #Question 2
import numpy as np
import pandas as pd
xr_value = []
error_value = []
function = lambda x: -2* np.power (x,6)-1.6*np.power (x,4)+12*x+1
function_derivative = lambda x: -12*np.power (x,5)-6.4 *np.power (x,3)+12
xl =0
xu =1
term =0
error =1
xr = 0
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 5/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
Number of terms used to calculate maximum is 5
Maximum is 9.687830067798496
Xr Error
1 0.50000 1.000000
2 0.75000 0.333333
3 0.87500 0.142857
4 0.93750 0.066667
5 0.90625 0.034483
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 6/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
In [2]: #Question 3
import numpy as np
import pandas as pd
function = lambda x: np.power (x,4)* np.exp (-3*np.power(x,2))
real_value = function (0.5)
taylor_serie_first_order = lambda a,h: np.power (a,4)* np.exp (-3*np.power(a
x1= 0.5
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 7/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
x0=1
h= x1-x0
approximate = taylor_serie_first_order(x0,h)
relative_error = (real_value-approximate)/real_value
In [3]: #Question 4
import numpy as np
import pandas as pd
#Analytically
Index = np.arange (3,4,0.1)
value = [np.power(x,3.5)-80 for x in Index]
df = pd.DataFrame ({"index": Index,
"value": value}, index = np.arange (1,len(Index)+1))
print ("Analytically")
print (df)
function = lambda x: np.power (x,3.5)-80
#false-position method
es = 2.5/100
xl =2
xu = 5
xr_value_fp= []
error_value_fp = []
Xr_fp = lambda xu, xl,f: xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)))
term =0
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 8/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
error =1
xr = 0
while (error >es):
for x in range (term):
xr_prev = xr
xr =Xr_fp (xu,xl,function)
xr_value_fp.append(xr)
error = np.abs ((xr-xr_prev)/xr)
error_value_fp.append (error)
test = function(xl)*function(xr)
if (test <0):
xu = xr
elif (test>0):
xl = xr
if error <0.05:
break
term +=1
term += 1
dffp = pd.DataFrame ({"X r": xr_value_fp,
"Error": error_value_fp}, index = np.arange (1,term))
print ("Estimate by false position method:")
print (dffp)
Analytically
index value
1 3.0 -33.234628
2 3.1 -27.547532
3 3.2 -21.382820
4 3.3 -14.717198
5 3.4 -7.527005
6 3.5 0.211780
7 3.6 8.523536
8 3.7 17.432991
9 3.8 26.965220
10 3.9 37.145639
Estimate by false position method:
X r Error
1 2.768318 1.000000
2 3.176817 0.128588
3 3.364213 0.055703
4 3.443493 0.023023
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 9/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 10/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 11/12
10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2
file:///Users/hungtran/Downloads/Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2.html 12/12