The Algorithms logoThe Algorithms
About

The Trapezium Method

C

The trapezium rule is a way of estimating the area under a curve. We know that the area under a curve is given by integration, so the trapezium rule gives a method of estimating integrals.

Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\varepsilon = 0.001$

import math 
import numpy as np

n = 4 
a = 2.
b = 3.
def f(x):
    return  (math.e**x / 2)*(math.cos(x)-math.sin(x))

def trapezoid(a,b,n):
  z = (b-a)/n
  i=a
  s=0
  while (i+z)<b:
    s=s+f(i)
    i=i+z 
  s=z*(f(a)+f(b))/2+s
  print('Result: ',s)
    
trapezoid(a,b,n)
Result:  -22.12539445092147