An integral:
  


can be approximated by the so-called Simpson’s rule:




Here `h = (b-a)/n`, `n` being an even integer and `a <= b`. 

We want to try Simpson's rule with the function f:



The task is to write a function called `simpson` with parameter `n` which returns the value of the integral of f on the interval `[0, pi]` (pi being 3.14159265359...).
## Notes:
- Don't round or truncate your results. See in "RUN EXAMPLES" the function `assertFuzzyEquals` or `testing`.
- `n` will always be even.

- We know that the exact value of the integral of f on the given interval is `2`.

You can see: 
about rectangle method and trapezoidal rule.

Difficulty: introductory

Starter code:

def simpson(n):
	

Solutions:
def simpson(n):
    from math import sin, pi
    
    a = 0
    b = pi
    h = (b - a)/n
    
    f = lambda x: (3/2) * sin(x)**3
    
    integral = 0
    integral += f(a) + f(b)
    integral += 4*sum(f(a +(2*i - 1)*h) for i in range(1, n//2 + 1))
    integral += 2*sum(f(a + 2*i*h) for i in range(1, n//2))
    integral *= h/3
    
    return integral

from math import sin, pi

def simpson(n, a=0, b=pi):
    f = lambda x: 1.5 * sin(x)**3
    h = (b-a)/n
    return (h/3 * (f(a) + f(b) 
                   + 4 * sum( f(a + (2*i-1) * h) for i in range(1, n//2 +1))
                   + 2 * sum( f(a + 2*i*h) for i in range(1, n//2))))

from math import sin, pi

def simpson(n):
    f = lambda x : 1.5*sin(x)**3
    h = pi / float(n)
    s1 = 0
    for i in range(1, int(n / 2) + 1):
        s1 += f((2 * i - 1) * h)
    s2 = 0
    for i in range(1, int(n / 2)):
        s2 += f(2 * i * h)
    res = pi / (3 * n) * (f(0) + f(pi) + 4 * s1 + 2 * s2) 
    return res

from math import sin, pi

f = lambda x: sin(x)**3*(3/2)

def simpson(n):
    a, b = 0, pi
    h = (b-a)/n
    return (h/3)*(f(a)+f(b)+4*sum([f(a+(2*i-1)*h) for i in range(1, n//2 + 1)])+2*sum([f(a+2*i*h) for i in range(1, (n//2))]))

import numpy as np
import math
def f(x):
    return 3/2 * np.power(math.sin(x), 3)

def simpson(n:int):
    a, b = 0, np.pi #integration limits
    h = (b - a)/n 
    suma1, suma2 = 0, 0
    #sum nr 1
    for i in range(1, n//2+1):
        suma1 = suma1 + f(a + (2*i-1)*h)
    #sum nr 2
    for i in range(1, n//2):
        suma2 = suma2 + f(a + 2*i*h)
    #output integral
    calka = h/3 * (f(a) + f(b) + 4*suma1 + 2*suma2)
    return calka

import math


def f(x):
    return 3 / 2 * math.sin(x) ** 3


def simpson(n):
    h = math.pi / n
    a = 0
    res = f(0) + f(math.pi)
    for i in range(1, n // 2 + 1):
        res += 4 * f((2 * i - 1) * h)
    for i in range(1, n // 2):
        res += 2 * f(2 * i * h)
    res *= math.pi / (3 * n)
    return res

import numpy as np

def simpson(n):
  sum = 0
  h = (np.pi - 0) / n
  for k in range(n + 1):
    x = 0 + (k * h)
    summand = 1.5*(np.sin(x))**3
    if (k != 0) and (k != n):
      summand *= (2 + (2 * (k % 2)))
    sum += summand
  return (h/3) * sum



import math

def simpson(n):
    a, b = 0, math.pi
    f = lambda x: (3/2) * (math.sin(x) ** 3)
    h = (b - a) / n
    res = f(a)
    res += 4 * sum(f(a + i*h) for i in range(1, n, 2))
    res += 2 * sum(f(a + i*h) for i in range(2, n, 2))
    res += f(b)
    return (h / 3) * res

from math import pi
from math import sin

def simpson(n):
    
    f = lambda x : (3/2) * (sin(x) ** 3)
    
    b = pi
    a = 0
    
    h = (b-a)/n
    coeff = 1/3*h
    sum_a = 4 * sum([f(a + (2*i - 1)*h) for i in range(1, n//2 + 1)])
    sum_b = 2 * sum([f(a + 2*i * h) for i in range(1, n//2)])
    
    return coeff * (f(a) + f(b) + sum_a + sum_b)

from math import sin
from math import pi

def f(x):
    return 3/2*(sin(x)**3)

def simpson(n):
    return pi/(3*n)*(f(0) + f(pi) + 4*sum(f((2*i - 1)*(pi/n))  for i in range(1,n//2+1)) + 2*sum(f(2*i*(pi/n)) for i in range(1,n//2)))