Solution (source code)

= Solution
{c}

Testing against compactly supported smooth <functions> gives the distributional identity $u''=\cos x\,u-f$. The right side belongs to $L^2$, since $u\in H^1$ and $\cos x$ is bounded. Therefore $u'\in H^1$ and \b[$u\in H^2(0,1)$]. This also makes the endpoint <derivative> traces meaningful, and integrating the weak form by parts recovers the Robin conditions.

The standard piecewise linear <finite element interpolation estimate> is
$$
\|u-I_hu\|_{L^2}\leq C_0h^2\|u''\|_{L^2},\qquad
\|(u-I_hu)'\|_{L^2}\leq C_1h\|u''\|_{L^2},
$$
with constants independent of $h$. Hence $\|u-I_hu\|_{H^1}\leq Ch\|u''\|_{L^2}$ for $h\leq1$. By <Céa's lemma>, with the fixed continuity and <coercivity> constants of $a$,
$$
\boxed{\|u-u_h\|_{H^1}\leq Ch\|u''\|_{L^2}=O(h).}
$$
The constant depends on the continuous problem but not on <mesh> refinement.