# Finite difference method, boundary conditions

I'm not gonna show every approximation here since I know many of you are familiar with it, but my start equation is:

$$-u''(x)+u(x)=f(x)$$ $$u(0)=0$$ $$u(1)=1$$

I can approximate this using finite difference method (I'm not really familiar with how to write it nicely), but my problem is the boundary condition, how do you know how the vector containing the boundary condition is going to look like, been struggling with this for a while now.

Thanks in advance for any help!

• I would appreciate even a link at this point, I simply cant find a good example.... – A.Maine Apr 4 '17 at 7:45

Let $h = 1/(n+1)$. Let $u_{0} = u(0)$, $u_{n+1} = u(1)$, $u_{j} = u(jh)$.
Using the second-order finite-difference scheme \begin{align*} u''(x) \approx \frac{u_{j+1} - 2u_{j} + u_{j-1}}{h^2} \end{align*} our linear system is \begin{align*} -u_{j+1} + 2u_{j} - u_{j-1} + h^2u_{j} = h^{2}f_{j}, j = 1, 2, .. n \end{align*} If $j = 1$, then \begin{align*} -u_{2} + (2 + h^2)u_{1} = h^{2}f_{1} \end{align*} and if $j = n$ \begin{align*} -1 + 2u_{n} - u_{n-1} + h^2u_{n} = h^{2}f_{n} \end{align*} which is equivalent to \begin{align*} - u_{n-1} + (2+h^2)u_{n} = h^{2}f_{n} + 1 \end{align*} Can you write down the matrix now?