You are on page 1of 12

10/14/23, 6:35 PM Trần_Vũ_Khánh_Hưng_ITCSIU21182_TMC_Lab2

In [ ]: # question a

import numpy as np
import matplotlib.pyplot as plt

# Define the equation and the range of x values


def f(x):
return np.log(x**4)

x = np.linspace(0.01, 10, 100) # Range of x values (avoid x = 0)

# Plot the function


plt.plot(x, f(x), label='ln(x^4)')
plt.axhline(y=0.7, color='r', linestyle='--', label='y = 0.7')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()

# Find the intersection point


idx = np.argwhere(np.diff(np.sign(f(x) - 0.7))).flatten()
intersection_point = (x[idx], f(x)[idx])
plt.plot(intersection_point[0], intersection_point[1], 'ro')

# Display the plot


plt.title('Graphical Solution of ln(x^4) = 0.7')
plt.show()

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

def bisection_method(f, x_low, x_up, iterations):


for i in range(iterations):
x_mid = (x_low + x_up) / 2
if f(x_mid) * f(x_low) < 0:
x_up = x_mid
else:
x_low = x_mid
return x_mid

x_low = 0.5
x_up = 2
iterations = 3

result_bisection = bisection_method(equation, x_low, x_up, iterations)


print("Approximated positive real root using bisection method:", result_bise

Approximated positive real root using bisection method: 1.0625

In [ ]: def false_position_method(f, x_low, x_up, iterations):


for i in range(iterations):
x_mid = (x_low * f(x_up) - x_up * f(x_low)) / (f(x_up) - f(x_low))
if f(x_mid) * f(x_low) < 0:
x_up = x_mid
else:
x_low = x_mid
return x_mid

result_false_position = false_position_method(equation, x_low, x_up, iterati


print("Approximated positive real root using false-position method:", result

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

while (error >0.05):


for x in range (term):
xr_prev = xr
xr =(xl+xu)/2
xr_value.append(xr)
error = np.abs ((xr-xr_prev)/xr)
error_value.append (error)
test = function_derivative (xl)*function_derivative(xr)
if (test <0):
xu = xr
elif (test>0):
xl = xr
if error <0.05:
break
term +=1
term += 1
print (f"Number of terms used to calculate maximum is {term}")
a = pd.DataFrame ({"Xr":xr_value,
"Error": error_value}, index = np.arange (1,term+1))
maximum = function (a.loc[term,"Xr"])
print (f"Maximum is {maximum}")
print (a)

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

print (f"Real value :{real_value}")


print (f"Approximation :{approximate}")
print (f"Relative error :{relative_error}")

Real value :0.029522909546313418


Approximation :0.09957413673572788
Relative error :-2.372775185979658

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

You might also like