$$\newcommand{\D}{\displaystyle} \renewcommand{\eqref}{Eq.~(\ref{#1})}$$

# 4.1 Introduction

Consider the following boundary value problem \begin{align} y''(x) = f(x)\,, \textrm{ for } 0 < x < 1\,, \tag{4.1} \end{align} with boundary conditions \begin{align} y(0) = \alpha\,,\quad y(1)=\beta\,. \tag{4.2} \end{align}

Our goal is to solve this problem using finite differences. First we discretize the independent variable $$x_i = i h$$, with $$h = 1/(N+1)$$. Having defined a grid, automatically defines our unknowns, i.e. $$y_0, y_1, \dots y_N, y_{N+1}$$. From boundary conditions we know that $$y_0=\alpha$$ and $$y_{N+1}=\beta$$, so that the number of true unknowns is $$N$$. Next we approximate $$y''(x)$$ by a centered finite difference approximation \begin{align} y''(x_i) \approx \frac{1}{h^2} (y_{i-1} -2 y_i + y_{i+1})\,, \tag{4.3} \end{align} obtaining the following algebraic equations \begin{align} \frac{1}{h^2} (y_{i-1} -2 y_i + y_{i+1}) = f(x_i) \tag{4.4} \end{align} for $$i=1,2,\dots,N$$. It is important to note that the first equation ($$i=1$$) involves the boundary value $$y_0=\alpha$$ and that the last equation ($$i=N$$) involves the value $$y_{N+1}=\beta$$. It can be easily noted that the set of equations (4.4) is as a linear system of $$N$$ equations for $$N$$ unknowns, which can be written as \begin{align} A Y = F\,, \tag{4.5} \end{align} where $$Y$$ is the vector of unknowns $$Y=[y_1,y_2,\dots,y_N]^T$$ and $$\begin{equation} \tag{4.6} A = \frac{1}{h^2}\begin{bmatrix} -2 & 1 & & & & & & & \\ 1 & -2 & 1 & & & & & & \\ & \cdot & \cdot & \cdot & & & & & \\ & & \cdot & \cdot & \cdot & & & & \\ & & & \cdot & \cdot & \cdot & & & \\ & & & & & & 1 & -2 & 1 \\ & & & & & & & 1 & -2 \end{bmatrix} \,, F= \begin{bmatrix} f(x_1)-\alpha/h^2 \\ f(x_2) \\ \cdot \\ \cdot \\ \cdot \\ f(x_{N-1}) \\ f(x_N)-\beta/h^2 \end{bmatrix} \end{equation}$$

This is a nonsingular tridiagonal system, that can be solved for $$Y$$ from any right-hand side $$F$$.