All problems
Trapezoid Weight Vector
Problem
The composite trapezoidal rule can be written as a dot product with a weight vector: weights are 0.5 at both endpoints and 1 at every interior node, all scaled by dx.
Given the number of nodes n and dx, return the weight list of length n. Return [] when n < 2.
Hint
Only the two endpoints carry the half weight.
Test cases
case 1
in: [5,0.5]
expected: [0.25,0.5,0.5,0.5,0.25]
case 2
in: [2,2]
expected: [1,1]
case 3
in: [3,1]
expected: [0.5,1,0.5]
Starter codePython
def trapezoid_weight_vector(n, dx):
# Your code here
pass3 test cases · run your code against them in the editor