Solution (source code)

= Solution

Choose a partition $0=x_0<x_1<\cdots<x_N=1$, with the usual nodal <piecewise-linear hat functions> $\phi_i$, including the half hats at the endpoints. Write $u_h=\sum_{i=0}^NU_i\phi_i$ with $U_0=1$. Differentiating the <Ritz method> energy with respect to $U_i$, $1\le i\le N$, gives
$$
\sum_{j=0}^NA_{ij}U_j=0,\qquad A_{ij}=\int_0^1(\phi_i'\phi_j'+x\phi_i\phi_j)dx.
$$
On an element $[a,b]$ of length $\ell$, its two basis functions are $(b-x)/\ell$ and $(x-a)/\ell$. Direct integration yields the element <matrix>
$$
\boxed{A^{[a,b]}=\frac1\ell\begin{pmatrix}1&-1\\-1&1\end{pmatrix}
+\frac\ell{12}\begin{pmatrix}3a+b&a+b\\a+b&a+3b\end{pmatrix}.}
$$
For example the first weighted diagonal is $\int_a^bx(b-x)^2/\ell^2\,dx=\ell(3a+b)/12$; the off-diagonal is $\int_a^bx(b-x)(x-a)/\ell^2\,dx=\ell(a+b)/12$. This gives the <affine-weighted hat mass matrix> and explicit equations on any partition by assembling adjacent elements.

For the uniform choice $h=1/N$, $x_i=ih$, the assembled interior equations are
$$
\boxed{\left[-\frac1h+\frac h{12}(2x_i-h)\right]U_{i-1}
+\left[\frac2h+\frac{2hx_i}{3}\right]U_i
+\left[-\frac1h+\frac h{12}(2x_i+h)\right]U_{i+1}=0,\quad1\le i<N.}
$$
The last node contributes only one element, so its equation is
$$
\boxed{\left[-\frac1h+\frac h{12}(2-h)\right]U_{N-1}
+\left[\frac1h+\frac h3-\frac{h^2}{12}\right]U_N=0,\qquad U_0=1.}
$$
In the first interior equation the known $U_0$ term moves to the right, giving $1/h-h^2/12$. For $N=1$ only the endpoint equation is needed. The last row enforces the weak <Neumann boundary condition>, so no artificial value outside the interval is introduced. The <matrix> on $U_1,\ldots,U_N$ is a <symmetric positive-definite matrix>: its <quadratic form> is $a(v_h,v_h)>0$ for every nonzero trial variation $v_h$. The discrete minimizer is therefore unique.