Skip to content
DeepForgeby svx
All problems
HardCalculusca-044

Triple Integral Midpoint Rule

Problem

Approximate a triple integral over a box with the 3D midpoint rule.

grid[k][j][i] holds f sampled at the midpoint of cell (i, j, k) with uniform spacings dx, dy, dz. Sum all values and multiply by the cell volume dx * dy * dz.

Hint

This is the same averaging idea as the 2D rule, with one more nested loop.

Test cases

case 1
in: [[[[1,1],[1,1]],[[1,1],[1,1]]],0.5,0.5,0.5]
expected: 1
case 2
in: [[[[1,2],[3,4]],[[5,6],[7,8]]],1,1,1]
expected: 36
case 3
in: [[[[2]]],1,2,3]
expected: 12
case 4
in: [[],1,1,1]
expected: 0
Starter codePython
def triple_midpoint(grid, dx, dy, dz):
    # Your code here
    pass

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