You are on page 1of 12

BIBHATSU KUIRI / 16PH40012

ASSIGNMENT 4
INTREGATION(II)

Problem 1
Method
BIBHATSU KUIRI / 16PH40012

OUTPUT GRAPHS
(simpsons method: graph in order x,x^2,sinx,xsinx )
BIBHATSU KUIRI / 16PH40012
BIBHATSU KUIRI / 16PH40012

(trapezoidal method:order of graph: x,x^2,sinx,xsinx


BIBHATSU KUIRI / 16PH40012
BIBHATSU KUIRI / 16PH40012

RESULT
The plot of relative error vs N ( number of division while calculating numerical
intregation ) is plotted and found that , the error is reduced at very high N

APPENDIX
(simpson method)
F(x)=x

a=0
s=0
def f(x):
y=x
return y
for i in range(2,1025,2):
j=1
x=0
s=0
h=1/i
while j<i:
if j%2!=0:
s=s+4*f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(1)
total=s*h/3
e=(total-0.5)/0.5
print(i,e)

f(x)=x*x

a=0
s=0
def f(x):
y=x*x
return y
for i in range(2,1025,2):
j=1
BIBHATSU KUIRI / 16PH40012

x=0
s=0
h=1/i
while j<i:
if j%2!=0:
s=s+4*f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(1)
total=s*h/3
e=(total-(1/3))/(1/3)
print(i,e)

f(x)=sinx

a=0
s=0
import math
def f(x):
x=math.sin(x)
y=x
return y
for i in range(2,1025,2):
j=1
x=0
s=0
h=3.141/i
while j<i:
if j%2!=0:
s=s+4*f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(1)
total=s*h/3
e=(total-2)/2
print(i,e)

f(x)=x*sinx

a=0
s=0
import math
BIBHATSU KUIRI / 16PH40012

def f(x):
z=math.sin(x)
y=x*z
return y
for i in range(2,1025,2):
j=1
x=0
s=0
h=3.141/i
while j<i:
if j%2!=0:
s=s+4*f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(1)
total=s*h/3
e=(total-(math.pi))/(math.pi)
print(i,e)

(trapezoidal)

F(x)=x

a=0
s=0
def f(x):
y=x
return y
for i in range(2,1025,2):
j=0
x=0
s=0
h=1/i
while j<=i:
if x==0 and x==1:
s=s+f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
total=s*h/2
e=(total-0.5)/0.5
print(i,e)
BIBHATSU KUIRI / 16PH40012

f(x)=x*x

a=0
s=0
def f(x):
y=x*x
return y
for i in range(2,1025,2):
j=0
x=0
s=0
h=1/i
while j<=i:
if x==0 and x==1:
s=s+f(x)
else:
s=s+2*f(x)
x=x+h
j=j+1
total=s*h/2
e=(total-(1/3))/(1/3)
print(i,e)

f(x)=sinx

a=0
s=0
import math
def f(x):
x=math.sin(x)
y=x
return y
for i in range(2,1025,2):
j=1
x=0
s=0
h=3.141/i
while j<i:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(3.141592)
total=s*h/2
e=(total-2)/2
print(i,e)
BIBHATSU KUIRI / 16PH40012

f(x)=x*sinx

a=0
s=0
import math
def f(x):
z=math.sin(x)
y=x*z
return y
for i in range(2,1025,2):
j=1
x=0
s=0
h=3.141/i
while j<i:
s=s+2*f(x)
x=x+h
j=j+1
s=s+f(0)+f(3.141592)
total=s*h/2
e=(total-(math.pi))/(math.pi)
print(i,e)
BIBHATSU KUIRI / 16PH40012

Problem 2






METHOD
BIBHATSU KUIRI / 16PH40012

RESULT

The actual value of the intregation is


Numerical method gives: 3.128379968095706

Relative error: 0.0042057284158050715

APPENDIX

a=0
import math
s=0
x=0.00001
h=.01
import math
def f(x):
if x!=0:
z=math.sin(x)
y=(z*z)/(x*x)
else:
y=1
return y
while x<30:
s=s+2*f(x)
x=x+h
# print(x)
s=s+f(0)
total=s*h/2
print(2*total)
e=(2*total-math.pi)/(math.pi)
print(e)

You might also like