matlabdifferential-equations

1D Poisson equation in MATLAB


I'm solving Poisson equation and I've come across a problem that I cannot understand.

All methods I'm using give a different result than the algebraic solution, which I believe is correct. Everything is defined properly, I cannot see the error. I think the differences are too big to be numerical errors.

Poisson equation is finding u when given f such that

-u''(x) = f(x) and u(0) = u(1) = 0.

The problem is discrete on [0,1] with evenly spaced interior gridpoints X as in the code. It can be written as a system of equations Ax = b, where A is a tridiagonal Poisson matrix, and b = h^2*f(X).

Here is my code and results:

m = 32;
h = 1/(m+1);
X = h:h:(1-h);

%Given function f
f = @(X) 25*pi^2*(sin(5*pi*X) + 9*sin(15*pi*X));
b = f(X)' * h^2;

%Algebraic solution
u = @(X) sin(5*pi*X) + sin(15*pi*X);

%1D Poisson matrix
A = tridiag(-1, 2, -1, m);

hold on
plot(u(X))
plot(linsolve(A, b))
legend('Algebraic solution', 'linsolve')

MATLAB vs algebraic solution plot


Solution

  • Nothing is wrong with your code.

    Turns out discretizing a system and solving that tends to have errors. Change to m=1000 and see your differences disappear. Discretizing continuous functions causes errors.