Skip to content
DeepForgeby svx
All problems
HardCalculusca-092

Fourier Cosine Coefficient

Problem

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

Integration by parts gives a_n = 2 * ((-1)^n - 1) / (pi * n^2). Return a_n for a given positive integer n, using 3.141592653589793 for pi.

Hint

Odd coefficients vanish because the boundary term alternates sign: cos(n*pi) - 1.

Test cases

case 1
in: [1]
expected: -1.2732395447351628
case 2
in: [2]
expected: 0
case 3
in: [3]
expected: -0.1414710605261292
case 4
in: [4]
expected: 0
case 5
in: [5]
expected: -0.05092958178940651
Starter codePython
def fourier_cosine_x(n):
    # Your code here
    pass

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