0% found this document useful (0 votes)
29 views

2024 Week 5 - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

2024 Week 5 - Jupyter Notebook

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [1]: 1 import numpy as np

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.

In [2]: 1 def trapz(f,a,b,N=50):


2 x = np.linspace(a,b,N+1)
3 y = f(x)
4 h = (b - a)/N
5 return (h/2) * np.sum(y[1:] + y[:-1])
6 ​
7 def f(x):
8 return x**3 + x
9 ​
10 round(trapz(f, a=0, b=2, N=100), 2)

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?

In [3]: 1 def simpsons_3_8_rule(f, a, b, n):


2 if n % 3 != 0:
3 raise ValueError("Number of intervals must be a multiple of 3.")
4
5 h = (b - a) / n
6 s = f(a) + f(b)
7 for i in range(1, n):
8 if i % 3 == 0:
9 s += 2 * f(a + i * h)
10 else:
11 s += 3 * f(a + i * h)
12
13 return (3/8)*h*s
14 ​
15 def f(x):
16 return np.cos(x**2) + np.sin(x**2)
17 ​
18 round(simpsons_3_8_rule(f, a=0, b=np.pi/4, n=1001+1), 2)

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().

In [5]: 1 def gauss_legendre_integrate(f, a, b, N):


2 x, w = sc.special.roots_legendre(N)
3 d = (b - a)/2
4 s = (b + a)/2
5 p = d*w*f(d*x + s)
6 I = np.sum(p)
7 df = pd.DataFrame({'weights $W_i$':w, 'Roots $X_i$':x, 'Functions $f(x_i)$':f(d*x + s), 'Products $w_if(x_i)$':p})
8 sum_row = pd.DataFrame(df.sum(numeric_only=True)).T
9 sum_row.index = ['Sum']
10 df = pd.concat([df, sum_row])
11 display(df)
12 return I
13 ​
14 f = lambda x : (x**8)
15 ​
16 I = gauss_legendre_integrate(f,a=-1,b=1,N=4)
17 print("Approximate integral value:", round(I,2))

weights 𝑊𝑖 Roots 𝑋𝑖 Functions 𝑓(𝑥𝑖) Products 𝑤𝑖𝑓(𝑥𝑖)


0 0.347855 -8.611363e-01 0.302395 0.105190

1 0.652145 -3.399810e-01 0.000178 0.000116

2 0.652145 3.399810e-01 0.000178 0.000116

3 0.347855 8.611363e-01 0.302395 0.105190

Sum 2.000000 -1.110223e-16 0.605148 0.210612

Approximate integral value: 0.21


Write a python program to calculate the approximate value of the integral ∫−11 (1 + 𝑎𝑥2 ) 𝑑𝑥 using two point Gauss-quadrature rule.
What will be the value of integral for a = 6?

Note: Don’t use any in-built function for integration problem.

Note: For the two point rule, the weight factors and nodes/roots are given below,

Weight factor (w) Nodes/Roots (x)

1 − √13
1
1 √3
Round the answer to 2 decimal places using the python function round()

In [14]: 1 def f(x,a=6):


2 return 1 + a * x**2
3 ​
4 I = gauss_legendre_integrate(f,a=-1,b=1,N=2)
5 print("Approximate integral value:", round(I,2))

weights 𝑊𝑖 Roots 𝑋𝑖 Functions 𝑓(𝑥𝑖) Products 𝑤𝑖𝑓(𝑥𝑖)


0 1.0 -0.57735 3.0 3.0

1 1.0 0.57735 3.0 3.0

Sum 2.0 0.00000 6.0 6.0

Approximate integral value: 6.0

In [13]: 1 def gauss(f,a,b,N):


2 t, w = sc.special.roots_legendre(N)
3 x = ((b-a)/2)*t + (b+a)/2
4 k = ((b-a)/2)*f(x)
5 G = np.sum(w*k)
6 df = pd.DataFrame({'weights $W_i$':w, 'Roots $X_i$':x, 'Functions $f(x_i)$': f(x), 'Products $w_if(x_i)$': w*k})
7 sum_row = pd.DataFrame(df.sum(numeric_only=True)).T
8 sum_row.index = ['Sum']
9 df = pd.concat([df, sum_row])
10 display(df)
11 return G
12 ​
13 def f(x,a=6):
14 return 1 + a * x**2
15 ​
16 print("Approximate integral value :", round(gauss(f,a=-1,b=1,N=2),2))

weights 𝑊𝑖 Roots 𝑋𝑖 Functions 𝑓(𝑥𝑖) Products 𝑤𝑖𝑓(𝑥𝑖)


0 1.0 -0.57735 3.0 3.0

1 1.0 0.57735 3.0 3.0

Sum 2.0 0.00000 6.0 6.0

Approximate integral value : 6.0

In [10]: 1 def f(x,a=6):


2 return 1 + a * x**2
3 ​
4 round(f(-0.57735) + f(0.57735) , 2)

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,

Weight factor (w) Nodes/Roots (x)

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))

weights 𝑊𝑖 Roots 𝑋𝑖 Functions 𝑓(𝑥𝑖) Products 𝑤𝑖𝑓(𝑥𝑖)


0 0.295409 -1.224745 9.0 2.658681

1 1.181636 0.000000 3.0 3.544908

2 0.295409 1.224745 9.0 2.658681

Sum 1.772454 0.000000 21.0 8.862269

Approximate integral value : 8.86

In [12]: 1 def f(x, a=4):


2 return (a*x**2 + 3)
3 ​
4 round(0.295409 * f(-1.224745) + 1.181636 * f(0.0) + 0.295409 * f(1.224745) , 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,

Weight factor (w) Nodes/Roots (x)

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)

In [12]: 1 def f(x, c=-2):


2 return np.exp(c * x) * (np.cos(x) + np.sin(x))
3 ​
4 ans = round(gauss_legendre_integrate(f, a=1, b=2, N=5), 2)
5 print(f"The approximate value of the integral is: {ans}")

weights 𝑊𝑖 Roots 𝑋𝑖 Functions 𝑓(𝑥𝑖) Products 𝑤𝑖𝑓(𝑥𝑖)


0 0.236927 -0.906180 0.168329 0.019941

1 0.478629 -0.538469 0.108870 0.026054

2 0.568889 0.000000 0.053184 0.015128

3 0.478629 0.538469 0.022759 0.005447

4 0.236927 0.906180 0.011160 0.001322

Sum 2.000000 0.000000 0.364303 0.067892

The approximate value of the integral is: 0.07

In [11]: 1 def f(x, c=-2):


2 return np.exp(c * x) * (np.cos(x) + np.sin(x))
3 a,b=1,2
4 d = (b - a)/2
5 s = (b + a)/2
6 I=d*(0.23693*f(-0.90618*d+s)+0.47863*f(-0.53847*d+s)+0.56889*f(0.*d+s)+0.47863*f(0.53847*d+s)+0.23693*f(0.90618*d+s))
7 round(I,2)

Out[11]: 0.07

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy