Page MenuHomec4science

IntegrationLib.py
No OneTemporary

File Metadata

Created
Sat, Jul 27, 17:16

IntegrationLib.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 30
@author: Simone Deparis
"""
import numpy as np
# This function just provides the midpoint quadrature rule.
def Midpoint( a, b, N, f ) :
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the midpoint rule
# size of the subintervals
H = (b - a) / N
# quadrature nodes
x = np.linspace(a+H/2, b-H/2, N)
# approximate integral
return H * np.sum( f(x) );
# This function just provides the midpoint quadrature rule.
def Trapezoidal( a, b, N, f ) :
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the trapezoidal rule
# size of the subintervals
H = (b - a) / N
# quadrature nodes
x = np.linspace(a, b, N+1)
# approximate integral
return H/2 * ( f(a) + f(b) ) + H * sum( f(x[1:N]) );
# This function just provides the Simpson quadrature rule.
def Simpson( a, b, N, f ) :
# [a,b] : inteval
# N : number of subintervals
# f : fonction to integrate using the trapezoidal rule
# size of the subintervals
H = (b - a) / N
# points defining intervals
x = np.linspace(a, b, N+1)
# left quadrature nodes
xl = x[0:-1]
# right quadrature nodes
xr = x[1:]
# middle quadrature nodes
xm = (xl+xr)/2
# approximate integral
return H/6 * sum( f(xl) + 4*f(xm) + f(xr) );

Event Timeline