Skip to content
DeepForgeby svx
All problems
MediumCalculusca-086

Fourier Sine Coefficient

Problem

For f(x) = x on the interval (0, pi), the Fourier sine coefficients are b_n = (2/pi) * integral from 0 to pi of x*sin(n*x) dx.

Integration by parts gives b_n = 2 * (-1)^(n+1) / n. Return b_n for a given positive integer n.

Hint

The integral of x sin(nx) over one period produces a factor cos(n*pi) = (-1)^n.

Test cases

case 1
in: [1]
expected: 2
case 2
in: [2]
expected: -1
case 3
in: [3]
expected: 0.6666666666666666
case 4
in: [4]
expected: -0.5
case 5
in: [5]
expected: 0.4
Starter codePython
def fourier_sine_x(n):
    # Your code here
    pass

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