Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error:
#Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error:
def f(x):
y=1-exp(-x)
return y
a=0
b=1
n=input("Enter the no. of divisions:")
n=int(n)
h=(b-a)/(n*1.)
s=0
from math import*
for i in range(1,n):
s=s+f(a+i*h)
y=(h/2)*(f(a)+f(b)+2*s)
print("The value of integration using trapezoidal rule is:",y)
r=abs(exp(-1)-y)
print("Error obtained in trapezoidal method is:",r)
z=0
c=0
for i in range(1,n,2):
z=z+f(a+i*h)
for i in range(2,n-1,2):
c=c+f(a+i*h)
g=(h/3)*(f(a)+f(b)+4*z+2*c)
print("The value of integration using Simpson's 1/3 rule is:",g)
v=abs(exp(-1)-g)
print("Error obtained in Simpson's 1/3 method is:",v)
def f(x):
y=1-exp(-x)
return y
a=0
b=1
n=input("Enter the no. of divisions:")
n=int(n)
h=(b-a)/(n*1.)
s=0
from math import*
for i in range(1,n):
s=s+f(a+i*h)
y=(h/2)*(f(a)+f(b)+2*s)
print("The value of integration using trapezoidal rule is:",y)
r=abs(exp(-1)-y)
print("Error obtained in trapezoidal method is:",r)
z=0
c=0
for i in range(1,n,2):
z=z+f(a+i*h)
for i in range(2,n-1,2):
c=c+f(a+i*h)
g=(h/3)*(f(a)+f(b)+4*z+2*c)
print("The value of integration using Simpson's 1/3 rule is:",g)
v=abs(exp(-1)-g)
print("Error obtained in Simpson's 1/3 method is:",v)
Comments
Post a Comment