Computes the integral with Gauss-Legendre quadrature using m nodes in Python

## module gaussQuad
''' I = gaussQuad(f,a,b,m).
    Computes the integral of f(x) from x = a to b
    with Gauss-Legendre quadrature using m nodes.
'''
from gaussNodes import *
 
def gaussQuad(f,a,b,m): 
    c1 = (b + a)/2.0
    c2 = (b - a)/2.0
    x,A = gaussNodes(m)
    sum = 0.0
    for i in range(len(x)):
        sum = sum + A[i]*f(c1 + c2*x[i])
    return c2*sum

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.