Professional Documents
Culture Documents
Question 1
Trapezoidal rule
Observation:
As we increase the value of n our answer will be more accurate.
Input:
lower_limit = 4
upper_limit = 5.2
n = 50
Code:
import numpy as np
# Function to integrate
def f(x):
return np.log(x)
# Value of h
h = (xn - x0) / n
# Finding sum
integration = f(x0) + f(xn)
return integration
# Input section
lower_limit = 4
upper_limit = 5.2
n = 50
Output:
Integration result by Trapezoidal method is: 1.827845
lower_limit = 4
upper_limit = 5.2
n = 50
Code:
import numpy as np
def func(x):
return np.log(x)
def approximate_integral(lower_limit, upper_limit, n):
# Value of h
h = (upper_limit - lower_limit)/n
# Calculating result
result = 0
i = 0
for i in range(0, n+1):
if i == 0 or i == n:
result += fx[i]
elif i % 2 != 0:
result += 4 * fx[i]
else:
result += 2 * fx[i]
result = result * (h / 3)
return result
# Driver code
lower_limit = 4
upper_limit = 5.2
n = 50
print("%.6f" % approximate_integral(lower_limit, upper_l
imit, n))
Output:
Integration result by Simpson’s 1/3 Rule is: 1.827847
Question 2
Input:
def func(x):
return np.log(x)
lower_limit = 4.0
upper_limit = 5.2
Code:
import numpy as np
# Function to integrate
def func(x):
return np.log(x)
lower_limit = 4.0
upper_limit = 5.2
print("""Integration result by Newton-
Cotes numerical integration formula is: %0.6f""" %
(integrate(func, lower_limit, upper_limit)))
Output: