Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Numerical-Integration 24/11/2021 22'35

In [5]: from math import *


import numpy as np

Rectangle Rule
In [12]: def CompLeftRect(f, a, b, m):
# m = number of subintervals
# a = lower bound
# b = upper bound
step = (b-a)/m
s = 0
for k in range(m):
s +=f(a+k*step)
return step*s

In [13]: def f(x):


return 1/(x+1)

In [14]: CompLeftRect(f,0,1,3)

Out[14]: 0.7833333333333333

Right Rectangle Rule


In [15]: def CompRightRect(f, a, b, m):
# m = number of subintervals
# a = lower bound
# b = upper bound
step = (b-a)/m
s = 0
for k in range(1,m+1):
s +=f(a+k*step)
return step*s

In [16]: CompRightRect(f,0,1,3)

Out[16]: 0.6166666666666667

Midpoint Rule

about:srcdoc Page 1 sur 3


Numerical-Integration 24/11/2021 22'35

In [19]: def CompMidPoint(f, a, b, m):


# m = number of subintervals
# a = lower bound
# b = upper bound
step = (b-a)/m
s = 0
for k in range(m):
s +=f(((a+k*step)+(a+(k+1)*step))/2)
return step*s

In [18]: CompMidPoint(f, 0, 1, 3)

Out[18]: 0.6897546897546897

Trapezoidal Rule
In [20]: def CompTrapezeRule(f, a, b, m):
# m = number of subintervals
# a = lower bound
# b = upper bound
step = (b-a)/m
s = (f(a)+f(b))/2
for k in range(1,m):
s +=f(a+k*step)
return step*s

In [22]: CompTrapezeRule(f, 0, 1, 3)

Out[22]: 0.7

Simpson Rule
In [47]: def SimpsonRule(f,a,b):
return (b-a)/6*(f(a)+4*f(0.5*(a+b))+f(b))

def CompSimpsonRule(f,a,b,m):
s = 0
step = (b-a)/m
for k in range(m):
s += SimpsonRule(f,a+k*step,a+(k+1)*step)
return s

In [46]: CompSimpsonRule(f,0,1,3)

Out[46]: 0.6931697931697931

In [38]: SimpsonRule(f,0,1)

Out[38]: 0.6944444444444443

about:srcdoc Page 2 sur 3


Numerical-Integration 24/11/2021 22'35

In [48]: def g(x):


return sin(x)/(1+cos(x))

In [50]: [CompLeftRect(g, 1, 3, 3),CompRightRect(g, 1, 3, 3),CompMidPoint(g, 1, 3, 3

Out[50]: [2.656889614809846,
11.69363458636176,
4.317319736680682,
7.175262100585803,
5.2699671913157236]

In [63]: exact = log(1+cos(1))-log(1+cos(3))

In [67]: print('Left Rectangle for n=3 ', CompLeftRect(g, 1, 3, 100)),


print('Right Rectangle for n=3 ', CompRightRect(g, 1, 3, 100)),
print('Midpoint for n=3 ',CompMidPoint(g, 1, 3, 100)),
print('trapeze for n=3 ', CompTrapezeRule(g,1,3,100)),
print('Simpson for n=3 ',CompSimpsonRule(g,1,3,100))
print('Exact value ',exact)

Left Rectangle for n=3 4.904150273224503


Right Rectangle for n=3 5.175252622371061
Midpoint for n=3 5.034749975660191
trapeze for n=3 5.039701447797781
Simpson for n=3 5.0364004663727195
Exact value 5.036398827069419

In [70]: def h(x):


return exp(x**2)

In [72]: print('Left Rectangle for n=3 ', CompLeftRect(h, 0, 1, 100)),


print('Right Rectangle for n=3 ', CompRightRect(h, 0, 1, 100)),
print('Midpoint for n=3 ',CompMidPoint(h, 0, 1, 100)),
print('trapeze for n=3 ', CompTrapezeRule(h,0,1,100)),
print('Simpson for n=3 ',CompSimpsonRule(h,0,1,100))
print('Exact = ', 1.462651746)

Left Rectangle for n=3 1.4541056407069766


Right Rectangle for n=3 1.4712884589915671
Midpoint for n=3 1.4626290942192772
trapeze for n=3 1.462697049849272
Simpson for n=3 1.4626517460959425
Exact = 1.462651746

In [ ]:

about:srcdoc Page 3 sur 3

You might also like