[Math] How to solve a second order differential equation with numerical approximation

approximationderivativesfinite differencesordinary differential equations

This is a homework question, but I'm also asking for self-teaching. So I hope it's OK 🙂

In the question, I am given a 2nd order ODE in the form $y'' = 4$ with two initial (or boundary?) conditions $f(0) = 5$ and $f(1) = 6$, and I'm asked to solve this with FDM (not sure if means forward-differencing method or finite-difference method, let's assume forward).

I think what I need is to express this system with $N$ discrete nodes, therefore somehow obtain $N$ linear equations and solve that system. But I'm not sure how to obtain these equations from the initial conditions.

So can you please guide me to the right direction here?

Thanks..

Edit:

So is this what I'm supposed to do? :

% dx = argument. default = 0.01
xs = 0:dx:1;

A = full(gallery('tridiag',Nx,1,-2,1));
A(1,:) = [1, zeros(1,Nx-1)];
A(Nx,:) = [zeros(1,Nx-1), 1];
% this generates a matrix (for Nx=4) like : 
     1  0  0  0
A =  1 -2  1  0
     0  1 -2  1
     0  0  0  1
% and to compute b : 
b = ones(Nx,1) * 2 * (dx^2); 
b(1) = 5; b(Nx) = 6;
% solve : 
f_Numerical = linsolve(A,b);

it gives a correct solution, but it's "too good", i.e. the error is in the order of 1e-15 and as I decrease Nx, the solution gets better, opposite of what I expected.. So is this the correct way of numerically solving this equation?

Best Answer

I'd say it is finite difference method. Approximate the derivative by the central second order difference quotient $$ \frac{f(x+h)-2f(x)+f(x-h)}{h^2}=f''(x)+O(h^2) $$ and apply it to the grid with $x_k=x_0+k·h$, $x_f=x_0+N·h$ over the interval $[x_0,x_f]$.

Related Question