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

Python Codes on numerical integration

Numerical methods integration trapezoidal and Simpsons rule Code on python

Uploaded by

sabulearns
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)
4 views

Python Codes on numerical integration

Numerical methods integration trapezoidal and Simpsons rule Code on python

Uploaded by

sabulearns
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/ 4

def f(x):

return 1/(1 + x**2)

# Implementing trapezoidal method


def trapezoidal(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)

# Finding final integration value


integration = integration * h/2

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call trapezoidal() method and get result


result = trapezoidal(lower_limit, upper_limit, sub_interval)
print("Integration result by Trapezoidal method is: %0.6f" % (result) )

Output

Enter lower limit of integration: 0


Enter upper limit of integration: 1
Enter number of sub intervals: 6
Integration result by Trapezoidal method is: 0.784241
def f(x,y):
return x+y
def rk4(x0,y0,xn,n):

# Calculating step size


h = (xn-x0)/n

print('\n--------SOLUTION--------')
print('-------------------------')
print('x0\ty0\tyn')
print('-------------------------')
for i in range(n):
k1 = h * (f(x0, y0))
k2 = h * (f((x0+h/2), (y0+k1/2)))
k3 = h * (f((x0+h/2), (y0+k2/2)))
k4 = h * (f((x0+h), (y0+k3)))
k = (k1+2*k2+2*k3+k4)/6
yn = y0 + k
print('%.4f\t%.4f\t%.4f'% (x0,y0,yn) )
print('-------------------------')
y0 = yn
x0 = x0+h

print('\nAt x=%.4f, y=%.4f' %(xn,yn))

# Inputs
print('Enter initial conditions:')
x0 = float(input('x0 = '))
y0 = float(input('y0 = '))

print('Enter calculation point: ')


xn = float(input('xn = '))

print('Enter number of steps:')


step = int(input('Number of steps = '))

# RK4 method call


rk4(x0,y0,xn,step)

Output
Enter initial conditions:
x0 = 0
y0 = 1
Enter calculation point:
xn = 1
Enter number of steps:
Number of steps = 2

--------SOLUTION--------
-------------------------
x0 y0 yn
-------------------------
0.0000 1.0000 1.7969
-------------------------
0.5000 1.7969 3.4347
-------------------------

At x=1.0000, y=3.4347
def f(x):
return 1/(1 + x**2)

# Implementing Simpson's 3/8


def simpson38(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h

if i%3 == 0:
integration = integration + 2 * f(k)
else:
integration = integration + 3 * f(k)

# Finding final integration value


integration = integration * 3 * h / 8

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call trapezoidal() method and get result


result = simpson38(lower_limit, upper_limit, sub_interval)
print("Integration result by Simpson's 3/8 method is: %0.6f" % (result) )

Output
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Integration result by Simpson's 3/8 method is: 0.735877
def f(x):
return 1/(1 + x**2)

# Implementing Simpson's 1/3


def simpson13(x0,xn,n):
# calculating step size
h = (xn - x0) / n

# Finding sum
integration = f(x0) + f(xn)

for i in range(1,n):
k = x0 + i*h

if i%2 == 0:
integration = integration + 2 * f(k)
else:
integration = integration + 4 * f(k)

# Finding final integration value


integration = integration * h/3

return integration

# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))

# Call trapezoidal() method and get result


result = simpson13(lower_limit, upper_limit, sub_interval)
print("Integration result by Simpson's 1/3 method is: %0.6f" % (result) )

Output
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Integration result by Simpson's 1/3 method is: 0.785398

You might also like