I want to numerically solve (using FDM)$$-y''(t)+2y'(t)=1, t\in (0,1)\\y(0)=1, y(1)=3$$
First, I check with symbolab that the analytic solution would be $1-\frac{3}{2\left(-1+e^2\right)}+\frac{3}{2\left(-1+e^2\right)}e^{2t}+\frac{t}{2}$:
$\frac{d}{dt}\left(1-\frac{3}{2\left(-1+e^2\right)}+\frac{3}{2\left(-1+e^2\right)}e^{2t}+\frac{t}{2}\right) = \frac{3}{e^2-1}e^{2t}+\frac{1}{2}$, and
$\frac{d^2}{dt^2}\left(1-\frac{3}{2\left(-1+e^2\right)}+\frac{3}{2\left(-1+e^2\right)}e^{2t}+\frac{t}{2}\right) = \frac{6}{e^2-1}e^{2t}$,
and we can easily check that the boundary conditions are satisfied.
Now, I attempt this problem numerically:
Using the centered difference approximations for the first and second derivative, I get two equations:
$D^2y_j = (y_{j-1}-2y_j+y_{j+1})/h^2\\Dy_j = (y_{j-1}+y_{j+1})/2h$.
I use the second order equation of the problem to derive the relation $(\frac{-1}{h^2} + \frac{1}{h})y_{j-1}+\frac{2}{h^2}y_j+(\frac{-1}{h^2} + \frac{1}{h})y_{j+1}$.
For example, if we take a mesh size of $.25$, we would have to solve 3 unknowns at $.25, .5, .75$, and our matrix equation would look like $$\begin{pmatrix}32&-12&0\\ -12&32&-12\\ 0&-12&32\end{pmatrix}v = \begin{pmatrix}1+12\\ \:\:1\\ \:\:1+12\cdot 3\end{pmatrix} = \begin{pmatrix}13\\ 1\\ 37\end{pmatrix}\\v= \begin{pmatrix}y_{.25}\\ \:\:y_{.5}\\ y_{.75}\end{pmatrix}$$
But if we solve this $\begin{pmatrix}y_{.25}\\ \:\:y_{.5}\\ y_{.75}\end{pmatrix} = \begin{pmatrix}\frac{67}{92}\\ \frac{79}{92}\\ \frac{34}{23}\end{pmatrix}$, and plot it, it looks off from the analytic solution.
One would think that increasing the number of nodes between 0 and 1 would be better, but increasing the unknown to 500 only made it worse:
Now, I may have made the program incorrectly, but the matrix vector equations seem to come out fine. I will upload the code if anyone is interested, but I think that my problem comes from a misunderstanding of the algorithm of the finite difference method. Any help would be lovely.
Best Answer
$$\color{gray}{Dy_j=\frac{y_{j−1}+y_{j+1}}{2h}}$$ is wrong and is in this wrong form used in the further equations. The central difference quotient is $$ Dy_j=\frac{y_{j+1}-y_{j-1}}{2h}=\frac{-y_{j−1}+y_{j+1}}{2h}. $$ It might be that the minus sign in the last form was simply lost sometime.