Skip to content
DeepForgeby svx
All problems
MediumCalculusca-350

Simpson with Odd Interval Fix

Problem

Apply composite Simpson when the number of intervals n is even. When n is odd, apply Simpson on the first n-1 intervals and the trapezoid rule on the final interval. A single interval uses the trapezoid rule.

Given sampled values and spacing dx, return the estimate.

Hint

Peel off the last interval and handle it as a trapezoid.

Test cases

case 1
in: [[0,1,4],1]
expected: 2.6666666666666665
case 2
in: [[0,1,4,9],1]
expected: 9.166666666666666
case 3
in: [[2,2],0.5]
expected: 1.6666666666666665
Starter codePython
def simpson_odd_interval_fix(values, dx):
    # Your code here
    pass

3 test cases · run your code against them in the editor