For an assignment I have to implement a 1D Poisson PDE with inhomogeneous Dirichlet BC's $$\Delta_1 u = f,  \quad u(a)=g(a), \: u(b) = g(b) $$I have managed to make it work, but I am not seeing the expected convergence behaviour. I used the standard (?) discretisation on the unit interval [0,1], $$-u_{j+1} + 2u_j - u_{j-1} = h^2f_j$$I have the following MATLAB code:function [sol, xVals, L, fVals] = poissonSolveFD2(f, g, N)h = 1/(N+2);h2= h*h;sol = zeros(N+2,1);sol(1) = g(0);sol(N+2) = g(1);xVals = linspace(0,1,N+2)';L = makeLaplace(N);fVals = f(xVals);fVals = h2*fVals;fVals(2) = fVals(2) + g(0);fVals(N+1) = fVals(N+1) + g(1);sol(2:N+1) = L\fVals(2:N+1);plot(xVals,sol)L=makeLaplace(N) returns the tridiagonal matrix $L=\operatorname{tridiag}(-1,2,-1) \in \mathbb{R}^{n \times n}$This code yields me a solution, but only shows an error reduction of $\mathcal{O}(h)$ instead of $\mathcal{O}(h^2)$.I have studied the answers at Testing 1D Poisson Solver, but that didn't get me any closer. That question also only considers homogeneous BC's, so I think this question has a right of existence.It is probably something small and stupid, but for some reason I can't figure it out.Thanks in advance.
1D inhomogeneous Poisson PDE with Dirichlet BCs, slow convergence
matlab;pde;poisson
Update: the advice below is necessary, but was written just to expand on the comments above and isn't the actual issue with the code above. See the other answers.You have a discretized system $Ax=b$, and certain entries of $x$ are specified by your boundary conditions. Let $x_1$ denote the vector of free (unknown) values, and let $x_2$ denote the vector of fixed (known) values. Similarly partition $A$ and $b$, obtaining the block system$\left[ \begin{array}{cc}  A_{11} & A_{12} \\ A_{21} & A_{22} \end{array} \right]\left[ \begin{array}{c}  x_1 \\ x_2 \end{array} \right] = \left[ \begin{array}{c}  b_1 \\ b_2 \end{array}  \right] $The values of $x_2$ are specified by your problem, so your task is to compute $x_1 = A_{11}^{-1}(b_1 - A_{12}x_2$).The values of $b_2$ should be consistent (if they are specified). 