1
$\begingroup$

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?

  • 0
    I wanted to make a link to my quesion on quora, where the idea of comparing with the recurrence solution was appeared, but it was prohibited by rules so I give it here([link](https://www.quora.com/Why-does-finite-difference-method-and-RK4-method-produce-different-result-for-the-initial-value-problem?__filter__=all&__nsrc__=1&__snid3__=696322298))2017-01-25

1 Answers 1

1

The problem with a direct recurrence is that you write the recurrence wrong. It is $$y_{i-1}-2y_i+y_{i+1} = h^2$$

therefore the solution is $y_i = 0.5(i^2 - i)h^2$. Now recall that $x = ih$, or $i = \frac{x}{h}$, so $y(x) = 0.5(x^2 - hx)$, and as $h$ tends to $0$, the approximation tends to the solution as expected.

  • 0
    Thank's a lot! That silly mistake has really tied me into knots about the methods I used.2017-01-25