Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

Unit5.ipynb - Numerical Integration

Python Program using Trapezoidal rule Simpsons 1/3 rd rule and simpsons 3/8 th rule to solve definite integration problems

Uploaded by

BhikulalJaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Unit5.ipynb - Numerical Integration

Python Program using Trapezoidal rule Simpsons 1/3 rd rule and simpsons 3/8 th rule to solve definite integration problems

Uploaded by

BhikulalJaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Double-click (or enter) to edit

Trapezoidal Rule

# Trapezoidal Method

def f(x):               # Define function to integrate
    return 1/(1 + x**2)
x0,xn,n=5,10,900
# Implementing trapezoidal method
h = (xn - x0) / n  # calculating step size
# Finding sum 
sum = f(x0) + f(xn)   
for i in range(1,n):
        k = x0 + i*h
        sum = sum + 2 * f(k)    
# Finding final integration value
integration = sum * h/2    
print("Integration result by Trapezoidal method is: %0.6f" % (integration) )    

Integration result by Trapezoidal method is: 0.097727

Simpsons 1/3 rd Rule

#Simpson's 1/3 Rule
def f(x):               # Define function to integrate
    return 1/(1 + x**2)
x0,xn,n=5,10,999
# Implementing Simpson's 1/3 
h = (xn - x0) / n  # calculating step size
# Finding sum 
sum = f(x0) + f(xn)   
for i in range(1,n):
    k = x0 + i*h
        
    if i%2 == 0:
            sum = sum + 2 * f(k)
    else:
            sum = sum + 4 * f(k)
   
 # Finding final integration value       
integration = sum * h/3    
print("Integration result by Simpsons 1/3 rd Rule is: %0.6f" % (integration) )    

I t ti lt b T id l th d i 0 097710
Simpsons 3/8 th rule

#Simpson's 3/8th Rule
def f(x):               # Define function to integrate
    return 1/(1 + x**2)
x0,xn,n=5,10,900
# Implementing Simpson's 3/8
h = (xn - x0) / n  # calculating step size
# Finding sum 
sum = f(x0) + f(xn)   
for i in range(1,n):
    k = x0 + i*h
        
    if i%3 == 0:
            sum = sum + 2 * f(k)
    else:
            sum = sum + 3 * f(k)
   
 # Finding final integration value       
integration = sum * 3*h/8    
print("Integration result by Simpsons 3/8 th Rule is: %0.6f" % (integration) )    

import sympy as sp
a=0
b=5
x = sp.Symbol('x')
f=.5*x+3
f.integrate((x,a,b))

21.25

import numpy as np
f=lambda x :400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
a=0
b=0.6
x=[a,b]
y=[f(a),f(b)]
np.trapz(y,x)

0.8591999999999943

import sympy as sp
a=0
b=0.6
x = sp.Symbol('x')
f=400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
f.integrate((x,a,b))

0.963600000000001

import sympy as sp
x = sp.Symbol('x')
f=400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
f.integrate((x,a,b))

576613.583333333

Double-click (or enter) to edit

import matplotlib.pyplot as plt
a=0
b=0.6
f=lambda x :400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
x =np.arange(a,b,0.01)
y=f(x)
plt.plot(x,y)
plt.fill_between([a,b],[f(a),f(b)],alpha = 0.5)
plt.show()

import matplotlib.pyplot as plt
import numpy as np
a=0
b=0.6
f=lambda x :400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
x =np.arange(a,b,0.01)
y=f(x)
plt.plot(x,y)
def SingleTrap(f,a,b):
  plt.fill_between([a,b],[f(a),f(b)],alpha=0.5)
  I=(b-a)*(f(a)+f(b))/2
  return I
I= SingleTrap(f,a,b)
print(I)
plt.show()
0.8591999999999943

import matplotlib.pyplot as plt
import numpy as np
def SingleTrap(f,a,b):
  plt.fill_between([a,b],[f(a),f(b)],alpha=0.5)
  I=(b-a)*(f(a)+f(b))/2
  return I
a,b,n=0,0.6,10
a0=a
h=(b-a)/n
I_sum = 0
f=lambda x :400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
x =np.arange(a,b,0.01)
y=f(x)
plt.plot(x,y)
for i in range(n):
  b0=a0+h
  I= SingleTrap(f,a0,b0)
  I_sum = I_sum + I
  a0 =b0
print(I_sum)
plt.show()

0.9548577599999999
import matplotlib.pyplot as plt
import numpy as np
def SingleSimpson13(f,a,b):
  h=(b-a)/2
  plt.fill_between([a,b],[f(a),f(b)],alpha=0.5)
  x0=a
  x1=x0+h
  x2=x1+h
  I=(b-a)*(f(x0)+4*f(x1)+f(x2))/6
  return I
a,b,n=0,0.6,10
a0=0
I_sum = 0
f=lambda x :400*x**5 -900*x**4+675*x**3 -200*x**2+25*x-0.2
x =np.arange(a,b,0.01)
y=f(x)
plt.plot(x,y)
for i in range(n):
  b0=a0+h
  I= SingleSimpson13(f,a0,b0)
  I_sum = I_sum + I
  a0 =b0
print(I_sum)
plt.show()

0.9635805599999998

You might also like