Use the built-in command
integral to compute the Fourier coefficients \(c_k\) for \(k=-3, \dots, 3\) for the complex-valued function
\begin{equation*}
f(x) = \sqrt{1+\cos(2\pi x)} + i\log(2+\sin(2\pi x))
\end{equation*}
Then plot both the function and its partial Fourier sum \(\sum_{|k|\le 3} c_k f_k\) for comparison.
Answer.
The syntax of
integral is integral(@(x) ..., a, b) where \(a, b\) are the limits of integration and the function is expressed in a form that allows vectorized evaluation. Here is the computation of Fourier coefficient
f = @(x) sqrt(1 + cos(2*pi*x)) + 1i*log(2 + sin(2*pi*x));
n = 3;
c = zeros(1, 2*n+1);
for k = -n:n
c(k+n+1) = integral(@(x) f(x).*exp(-2*pi*1i*k*x), 0, 1);
end
disp(c)
Then we combine both complex plots using different colors.
t = linspace(0, 1, 1000); k = -n:n; p = c*exp(2*pi*1i*k'*t); hold on plot(f(t)) plot(p, 'r') hold off
Note that the lack of smoothness at one point slows down the convergence of Fourier series. If the function had
sqrt(1.1 + cos(2*pi*x)) instead, the convergence would be much better.
