2024 Week 5 - Jupyter Notebook
2024 Week 5 - Jupyter Notebook
2 import scipy as sc
3 import pandas as pd
Write a program to calculate the approximate value of the integral ∫𝑎𝑏 (𝑥3 + 𝑥) 𝑑𝑥 using Trapezoidal rule.
The numerical integration has to be performed over N points, where N = 100, a = 0, and b = 2.
What will be the value of integral?
Round the answer to two decimal places using the Python function round() as below: ans = round(ans, 2)
Note: Don't use any in-built function for integration problem.
Out[2]: 6.0
Write a python program to calculate the value of the integral ∫0𝜋/4 (cos(𝑥2 ) + sin(𝑥2 )) 𝑑𝑥 using Simpson’s 3/8 rule.
Divide the integration interval into N equally spaced points where N = 1001 and print your answer rounded off up to two decimal places.
Round the answer to two decimal places using the Python function round() as below: ans = round(ans, 2)
Note: Don’t use any in-built function for integration problem.
What is the value of the integral calculated?
Out[3]: 0.91
Calculate the value of the integral ∫−11 𝑥8 𝑑𝑥 using 4 point gauss-legendre quadrature method.
You can use the scipy module for this problem.
Round your answer to 2 decimal places using the python function round().
Note: For the two point rule, the weight factors and nodes/roots are given below,
1 − √13
1
1 √3
Round the answer to 2 decimal places using the python function round()
Out[10]: 6.0
Write a python program to calculate the approximate value of the integral ∫−∞∞ exp(−𝑥2 )(𝑎𝑥2 + 3) 𝑑𝑥 using three point Hermite-Gauss quadrature rule.
What will be the value of the above integral for a = 4?
Note: Don’t use any in-built function for integration problem.
Note: For the three point rule, the weight factors and nodes/roots are given below,
1.18164 0
0.295409 −1.22474
0.295409 1.22474
Round the answer to two decimal places using the python function round() as below: ans = round(ans,2)
In [11]: 1 def gauss_hermite_quadrature(f, c, N):
2 x, w = sc.special.roots_hermite(N)
3 I = np.sum(w * f(x, c))
4 df = pd.DataFrame({'weights $W_i$':w,'Roots $X_i$':x,r'Functions $f(x_i)$':f(x, c),r'Products $w_if(x_i)$':w*f(x, c)})
5 sum_row = pd.DataFrame(df.sum(numeric_only=True)).T
6 sum_row.index = ['Sum']
7 df = pd.concat([df, sum_row])
8 display(df)
9 return I
10
11 def f(x, const):
12 a = const
13 return (a*x**2 + 3)
14
15 print("Approximate integral value :", round(gauss_hermite_quadrature(f, 4, N=3) , 2))
Out[12]: 8.86
Write a python program to calculate the approximate value of the integral ∫𝑎𝑏 exp(𝑐𝑥)(cos𝑥 + sin𝑥) 𝑑𝑥 using five point Legendre-Gauss-quadrature rule.
Taking a = 1, b = 2 and c = -2, what will be the output?
Note: You should not use ”scipy.integrate” package in your code.
Note: For the five points Legendre-Gauss-quadrature rule, the weight factors and nodes/roots are given below,
0.23693 −0.90618
0.47863 −0.53847
0.56889 0.00000
0.47863 0.53847
0.23693 0.90618
Round the answer to 2 decimal places using the python function round() as below: ans: round(ans,2)
Out[11]: 0.07