All problems
Gauss-Legendre 2-Point Integration
Problem
Approximate the integral of f(x) = sum(coeffs[i] * x^i) over [a, b] with the 2-point Gauss-Legendre rule:
integral ≈ (b - a)/2 * (f(m - d) + f(m + d))
where m = (a + b)/2 and d = (b - a)/(2*sqrt(3)). The rule is exact for polynomials up to degree 3.
Hint
Shift the standard nodes ±1/sqrt(3) from [-1, 1] onto the interval [a, b].
Test cases
case 1
in: [[0,0,1],0,2]
expected: 2.6666666666666665
case 2
in: [[0,0,0,1],-1,1]
expected: 0
case 3
in: [[1],0,5]
expected: 5
case 4
in: [[0,1],0,4]
expected: 8
case 5
in: [[0,0,0,0,1],-1,1]
expected: 0.22222222222222235
Starter codePython
def gauss_legendre_2(coeffs, a, b):
# Your code here
pass5 test cases · run your code against them in the editor