Solution (source code)

= Solution

Expand the unknown in <Fourier modes>:
$$
u(x)=\sum_{n\in\mathbb Z}\widehat u_n e^{i\pi nx}.
$$
By <Termwise differentiation of a Fourier series>,
$$
-\frac1{\pi^2}u''
=\sum_{n\in\mathbb Z}n^2\widehat u_n e^{i\pi nx}.
$$
Also,
$$
1+2\cos\pi x=1+e^{i\pi x}+e^{-i\pi x}.
$$
Therefore the coefficient of $e^{i\pi mx}$ on the left-hand side is
$$
(m-1)^2\widehat u_{m-1}
+(m^2+1)\widehat u_m
+(m+1)^2\widehat u_{m+1}.
$$
The right-hand side has coefficients
$$
\widehat f_m=\frac1{m^2+1},
\qquad m\in\mathbb Z,
$$
because the coefficient of each of $e^{\pm i\pi nx}$ is $1/(n^2+1)$ and the constant coefficient is one. Hence the <Fourier spectral system for a cosine-modulated second derivative> is
$$
\boxed{
(m-1)^2\widehat u_{m-1}
+(m^2+1)\widehat u_m
+(m+1)^2\widehat u_{m+1}
=\frac1{m^2+1},
\qquad m\in\mathbb Z.}
$$
Its coefficient array is an infinite <tridiagonal matrix>.

For a <spectral truncation>, retain the modes $-M\leq n\leq M$, set
$$
\widehat u_{-M-1}=\widehat u_{M+1}=0,
$$
and impose the displayed coefficient equation for every $-M\leq m\leq M$. This gives an explicit $(2M+1)$-dimensional tridiagonal system. The resulting finite <Fourier series> automatically satisfies the periodic boundary conditions.