How to numerically calculate second derivate with different lengths each side?

44 Views Asked by At

I need to calculate a second derivate numerically for a finite differences program. But the mesh is not uniform, so I can't use $$\frac{d^2 f}{dx^2}\approx \frac{f(x_{i+1})+f(x_{i-1})-2f(x_i)}{\Delta x^2}$$ where $i$ is the index of the node and $\Delta x$ is the distance between nodos.

I've tried to put $$\frac{d^2 f}{dx^2}\approx \frac{f(x_{i+1})+f(x_{i-1})-2f(x_i)}{\Delta x_{i-1}\Delta x_{i+1}}$$ with $\Delta_{j}$ the distance between the $i$-th and $j$-th nodes, but the results aren't as expected in the distribution of temperature (what the program is for). This image is for clarity.

1

There are 1 best solutions below

0
On

You want to assume that the difference is some combination of the three function values and Taylor expand to find the coefficients that ensure the best possible cancellations. Thus you want $a(f(x)-h_- f'(x)+h_-^2f''(x)/2)+bf(x)+c(f(x)+h_+ f'(x)+h_+^2 f''(x)/2)=f''(x)$. Thus you have the system $a+b+c=0,-h_- a + h_+ c =0, h_-^2a + h_+^2c=2$ which you can solve. You find $b=-(a+c),a=\frac{2}{h_-^2 + h_+ h_-},c=\frac{2}{h_+^2 + h_+ h_-}$.

Your version has $a=c$, which causes the first derivative terms here to not cancel out.