The domain we want to obtain the solution on is $x \in [0,1]$.
Let's write the second difference equation corresponding to this differential equation problem $\frac{y_{i-1}-2y_i+y_{i+1}}{h^2} = 1$ (is that step valid by the way?). The first condition is $ y_0 = 0 $. For the second condition we use the first forward difference: $\frac{y_1-y_0}{h} = 0$, so $y_1 = y_0 = 0$. We choose $h = 0.1111$ for the grid. Then we solve $Ay = b$,
where the matrix $A = \begin{bmatrix} 1 & 0 & 0 & 0 & \dots & 0 \\ 0 & 1 & 0 & 0 & \dots & 0 \\ 1 & -2 & 1 & 0 & \dots & 0\\ 0 & 1 & -2 & 1 & \dots & 0 \\ \vdots & & \ddots & \ddots & \ddots & \vdots \\ 0 & \dots & 0 & 1 & -2 & 1 \end{bmatrix},$ $b = [0, 0, h^2, h^2, \dots,h^2]^T.$
This is a script (you could play with it), solving this problem:
k = 10;
x = linspace(0, 1, k);
h = (x(end) - x(1)) / (k - 1);
D = sparse(1 : k, 1 : k, 1 * ones(1, k), k, k);
Elow = sparse(2 : k, 1 : k-1, -2 *ones(1, k-1), k, k);
Elow2 = sparse(3 : k, 1 : k - 2, ones(1, k - 2), k, k);
S = Elow + D + Elow2;
S(1,1) = 1;
S(1,2) = 0;
S(2,1) = 0; S(2,2) = 1; S(2,3) = 0;
Sf = full(S);
b = h^2 * ones(1,k)'; b(1) = 0; b(2) = 0;
u = S \ b;
Increasing the number of $k$ we can see that finite difference solution looks like the analytical solution $y = 0.5 x^2$
On the other hand we can solve $y_{i-1}-2y_i+y_{i+1} = 1, y_0 = 0, y_1 =0$ directly as 2nd order nonhomogeneous recurrence equation, the solution would be $y_i = 0.5(i^2 - i)$.
ps I know that the RK4 method is OK for such problem, but I'm trying to study different numerical methods, so I took finite difference method for this equation to be sure that it's working. enter image description here
Summing up, I have several questions: is this the case when my finite difference scheme should converge to actual solution? why the recurrence equation solution gives so bad result? Is it correct actually?