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?



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?

  • $\begingroup$ It is Ok that the error is that close to 0, as a second order method it is exact in the case where the solutions are up to cubic functions (all derivatives making up the error term are zero). Make the right side something non-constant like 2*sin(x) and you should see errors according to the theory. $\endgroup$ Oct 29, 2015 at 13:48
  • $\begingroup$ Ok, thanks again :) But I'm asking just to make sure. My right hand side, i.e. b (having 2*deltax^2 's in the middle) is correct, right? $\endgroup$
    – jeff
    Oct 29, 2015 at 13:52
  • $\begingroup$ No, it should be 4*dx^2, since y''=4. There is no factor 1/2 to be found. The exact solution should be 2x^2-x+5. $\endgroup$ Oct 29, 2015 at 15:05
  • $\begingroup$ @LutzL you are right, I actually changed the original question being afraid of plagiarism, thanks :) $\endgroup$
    – jeff
    Oct 29, 2015 at 15:13

1 Answer 1


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]$.

  • $\begingroup$ Thank you so much! Can you please take a look at my edit and confirm if I got it right? $\endgroup$
    – jeff
    Oct 29, 2015 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .