Created
June 27, 2020 10:38
-
-
Save aburousan/9eb9ddc4bdc5cb73cc7a2cbc3028d2fb to your computer and use it in GitHub Desktop.
This shows a program to calculate Integration using Trapezoidal Rule with a desired Accuracy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
original_value = np.pi/4 | |
def f(x): | |
return 1/(1+x*x)#1st step is done | |
def trapizoid(f,a,b,tol): | |
fa = f(a); fb = f(b);I = 0 | |
n =10; it = 1#Iteration number | |
while True: | |
h = (b-a)/n | |
sum = 0 | |
for i in range(1,n):#It takes i = 1 upto i = n-1 | |
sum = sum + f(a+i*h) | |
sum = h/2*(fa+2*sum+fb) | |
if abs(sum-I)<=tol: | |
break | |
it += 1#same as it = it + 1 | |
I = sum | |
n += 15 | |
return sum | |
tol = 1.0e-6 | |
Integration_value = trapizoid(f,0,1,tol) | |
print("The value of Integration : ",Integration_value) | |
print("Error : ", abs(Integration_value-original_value)) | |
#OutPut: | |
#The value of Integration : 0.7853956979142137 | |
#Error : 2.46548323457052e-06 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment