0% found this document useful (0 votes)
5 views3 pages

Numerical Methods Integration in Python

integration using numerical methods

Uploaded by

ravenwixxx
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)
5 views3 pages

Numerical Methods Integration in Python

integration using numerical methods

Uploaded by

ravenwixxx
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

1 Newton-Cotes Quadrature Formula

1.1 Source Code


1 import numpy as np
2
3 def f ( x ) :
4 return (4/(1+ x **2) )
5
6 def trapezoid (f ,a ,b , n ) :
7 h =( b - a ) / n
8 result = f ( a ) + f ( b )
9 for i in range (1 , n ) :
10 result += 2* f ( a + i * h )
11 result = h /2 * result
12 return result
13
14 def simpson_onethird (f ,a ,b , n ) :
15 if n %2!=0:
16 print ( f " { n } must be multiple of 2 " )
17 return None
18 h =( b - a ) / n
19 result = f ( a ) + f ( b )
20 for i in range (1 , n ) :
21 if i %2==0:
22 m =2
23 else :
24 m =4
25 result += m * f ( a + i * h )
26 result *= h /3
27 return result
28
29 def s impson _three ight (f ,a ,b , n ) :
30 if n %3!=0:
31 print ( f " { n } must be multiple of 3 " )
32 return None
33 h =( b - a ) / n
34 result = f ( a ) + f ( b )
35 for i in range (1 , n ) :
36 if i %3==0:
37 m =2
38 else :
39 m =3
40 result += m * f ( a + i * h )
41 result *=3* h /8
42 return result
43
44 def boole (f ,a ,b , n ) :
45 if n %4!=0:
46 print ( f " { n } must be multiple of 4 " )
47 return None
48 h =( b - a ) / n
49 result =7* f ( a ) + 7* f ( b )
50 for i in range (1 , n ) :
51 if i %4==0:
52 m =14
53 elif i %2==0:
54 m =12
55 else :
56 m =32
57 result += m * f ( a + i * h )
58 result *=2* h /45
59 return result
60
61 def weedle (f ,a ,b , n ) :
62 if n %6!=0:
63 print ( " n must be multiple of 6 " )
64 return None
65 h =( b - a ) / n
66 result = f ( a ) + f ( b )
67 for i in range (1 , n ) :
68 if i %6==0:
69 m =2
70 elif i %3==0:
71 m =6
72 elif i %2==0:
73 m =1
74 else :
75 m =5
76 result += m * f ( a + i * h )
77 result *=3* h /10
78 return result
79
80 trape_zoid = trapezoid (f , 0 , 1 , 12)
81 simpson1 = simpson_onethird (f , 0 , 1 , 12)
82 simpson2 = simp son_th reeigh t (f , 0 , 1 , 12)
83 booles_method = boole (f , 0 , 1 , 12)
84 weedles_method = weedle (f , 0 , 1 , 12)
85
86 print ( f " Integraton using Trapezoid Rule is :{ trape_zoid } " )
87 print ( f " Integraton using Simposon 1/3 Rule is :{ simpson1 } " )
88 print ( f " Integraton using Simpson 3/8 Rule is :{ simpson2 } " )
89 print ( f " Integraton using Boole ’s Method is :{ booles_method } " )
90 print ( f " Integraton using Weedle ’s Method is :{ weedles_method } " )

1.2 Output

Figure 1: Code Output


2 Gauss Legendre Integration
2.1 Source Code
1 import numpy as np
2
3 def f ( x ) :
4 return (4/(1+ x **2) )
5
6 def g au s s le g e nd r e 2p o i n t (f ,a , b ) :
7 w = np . array ([1 , 1])
8 z = np . array ([ -1/ np . sqrt (3) , 1/ np . sqrt (3) ])
9 p = (b - a ) /2
10 q = ( a + b ) /2
11 x = p*z + q
12 i = np . dot ( f ( x ) , w ) * p
13 return i
14
15 def g au s s le g e nd r e 3p o i n t (f ,a , b ) :
16 w = np . array ([5/9 , 8/9 , 5/9])
17 z = np . array ([ - np . sqrt (3/5) , 0 , np . sqrt (3/5) ])
18 p = (b - a ) /2
19 q = ( a + b ) /2
20 x = p*z + q
21 i = np . dot ( f ( x ) , w ) * p
22 return i
23

24 result2 = g au s s le g e nd r e 2p o i nt (f , 0 , 1)
25 result3 = g au s s le g e nd r e 3p o i nt (f , 0 , 1)
26
27 print ( result2 )
28 print ( result3 )

2.2 Output

Figure 2: Code Output

You might also like