Problem with multigrid method implementation in MATLAB - Stack Overflowmost recent 30 from stackoverflow.com2026-04-14T22:18:28Zhttps://stackoverflow.com/feeds/question/79674366https://creativecommons.org/licenses/by-sa/4.0/rdfhttps://stackoverflow.com/q/796743660Problem with multigrid method implementation in MATLABCymek3https://stackoverflow.com/users/161283862025-06-21T11:03:12Z2025-06-21T11:03:12Z
<p>I'm implementing a 2-grid method for solving discrete Poisson equation. I have read many papers to understand multigrid methods, for this implementation I'm following an example from <a href="https://ocw.mit.edu/courses/16-920j-numerical-methods-for-partial-differential-equations-sma-5212-spring-2003/95427b55fae4d6b675869c192dc7c3f4_lec7_notes.pdf" rel="nofollow noreferrer">this paper</a> (p. 10). I believe I've implemented everything correctly, but I see a weird behaviour.</p>
<p>After prolonging the coarse grid correction back to fine grid the smooth error component should have become smaller, bringing us closer to the solution and at the same time the error should have become more oscillatory. I don't see this behaviour in my implementation. The error barely changes after coarse grid correction. I tried following <a href="https://www.math.hkust.edu.hk/%7Emamu/courses/531/tutorial_with_corrections.pdf" rel="nofollow noreferrer">another example</a> (p. 38) and I see the same problem.</p>
<p>Am I not understanding something correctly? Here is my code and results:</p>
<pre><code>%Grid size
m = 32 - 1;
h = 1/(m+1);
X = h:h:(1-h);
%The fine grid has m=31 points.
%The coarse grid has mr=15 points
mr = (m-1)/2;
%Restriction and prolongation matrices
%R - full weighting
R = zeros(mr, m);
for i = 0:mr-1
R(i + 1, 2*i + 1) = 1/4;
R(i + 1, 2*i + 2) = 1/2;
R(i + 1, 2*i + 3) = 1/4;
end
%P - linear interpolation
P = zeros(m, mr);
for j = 0:mr-1
P(2*j + 1, j + 1) = 1/2;
P(2*j + 2, j + 1) = 1;
P(2*j + 3, j + 1) = 1/2;
end
%------------------------------------------
%I'll be solving discrete Poisson equation
%-\nabla^2 u = f
%u(0)=u(1)=0
%Given function f
f = 25*pi^2*(sin(5*pi*X) + 9*sin(15*pi*X));
%System of equations Ax=b with initial guess x=0
%tridiag produces a square m by m matrix
A = tridiag(-1, 2, -1, m);
b = f' * h^2;
x = zeros(m,1);
%Analytic solution
u = sin(5*pi*X) + sin(15*pi*X);
%------------------------------------------
%Multigrid
%Pre-smoothing
%3 iterations of weighted Jacobi method with w=2/3
x = Jacobi(A,x,b,2/3,3);
%Up to this point everything is fine
%Restric the residue r -> rr
r = b - A * x;
rr = R * r;
%Ae = r on the coarse grid
Ar = tridiag(-1, 2, -1, mr);
er = zeros(mr,1);
%4 iterations of weighted Jacobi method with w=2/3
er = Jacobi(Ar,er,rr,2/3,4);
%Prolongation er -> e
e = P * er;
x = x + e;
</code></pre>
<pre><code>function x = Jacobi(A, x0, b, w, Imax)
n = size(x0, 1);
x = zeros(n,1);
I = 0;
while I < Imax
for i = 1:n
x(i) = w/A(i,i) * (b(i) - sum(A(i,:)'.*x0) + A(i,i)*x0(i)) + (1-w)*x0(i);
end
x0 = x;
I = I + 1;
end
end
</code></pre>
<p><a href="https://i.sstatic.net/xVkqd44i.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xVkqd44i.png" alt="plots" /></a></p>