Finite Element Method for non-homogenous boundary conditions I have the following model boundary value problem to be solved for $u \in H^1_0$ satisfying
$$
-(a_1u')'+a_0 u = f
$$
where $f \in L^2(\Omega)$, in a bounded region $\Omega = (a, b) \subset \mathbb{R}$ and
$$
u(a) = g_a, u(b) = g_b
$$
I want to set up an affine basis for the FEM consisting of "hats" at uniform nodes on the domain $\Omega$. The only problem is the non-homogeniety of the boundary values $(g_a \neq g_b)$, can I proceed in the usual manner where $u(a) = u(b) = 0$?
 A: For this kind of problem there are several approaches you could take. Let us define the bilinear form and linear form for the problem:
$$a_h(u_h,v_h) :=\int_a^ba_1u_h'v_h'+a_0u_hv_h,$$
$$L_h(v_h) := \int_a^bfv_h.$$
Let's also define two finite element spaces: $V_h$ is the space of piecewise linears on some mesh of the interval $[a,b]$, and $V_{h,0}=V_h\cap H^1_0(a,b)$ (the latter space is the standard one used for the homogeneous BC).
Approach 1: The BC is enforced strongly upon the solution $u_h$. I.e., we seek $u_h\in V_h$ such that $u_h(a)=g_a,u_h(b)=g_b$ and
$$a_h(u_h,v_h)=L_h(v_h)\quad \forall v_h\in V_{h,0}.$$
Notice here that the test space and solution space are different, but that the linear system + BCs provides the correct amount of conditions to determine $u_h$ uniquely. (Note that in practice the BCs are applied by appending them to the linear system).
Approach 2: Nitsche type weak BC enforcement. Here we add a BC penalty term into the weak formulation, i.e., we let $\sigma$ be a possibly mesh dependent parameter (e.g. say $\sigma = 10/h$) and we seek $u_h\in V_h$ such that
$$a_h(u_h,v_h)+\sigma (u_h(b)v_h(b)+u_h(a)v_h(a))=L_h(v_h)+\sigma (g_bv_h(b)+g_av_h(a))\quad\forall v_h\in V_h.$$
To determine the $\sigma$ parameter, typically one has to do some error analysis on the problem to determine what is a good fit. Notice that in this case the solution and test space are the same.
Approach 3: Turn your inhomogeneous problem into a homogeneous problem then work backwards. For this one it also might make more sense to work with a piecewise quadratic space, so that the final solution belongs to the finite element space. Here, what we do is take $q_h$ to be the quadratic function that satisifies $q_h(a) = g_a,q_h(b) = g_b$, so that if $u$ solves the original BVP, then the function $w:=u-q_h$ satisfies
$$-(a_1w)'+a_0w = f-(a_1q_h)'+a_0q_h$$
$$w(a)=w(b)=0.$$
Now we have a homogeneous BVP, which we can solve as normal via quadratic finite elements, e.g. we let $V_{h,0}^2$ be the space of piecewise quadratic that vanish at $a,b$, and seek $w_h\in V_{h,0}^2$ such that
$$a_h(w_h,v_h) = L_h(v_h)+\int_a^b(-(a_1q_h)'+a_0q_h)v_h\quad\forall v_h\in V_{h,0}^2.$$
Then, finally define the solution $u_h:=w_h+q_h$.
