% Matlab code mgvrhs1D.m % For "Applied Numerical Linear Algebra", Question 6.16 % Written by James Demmel, Apr 22, 1995 % Modified, Jun 2, 1997 % % Multigrid on a 1 D grid % Restrict a matrix from grid n to grid m, where n and m % are (powers of 2) minus 1 % used to get right-hand-side for multigrid solve % Written by J. Demmel, UC Berkeley, April 22, 1995 % % function nrhs=mgvrhs1D(n,m,rhs) % function nrhs=mgvrhs1D(n,m,rhs) nk=round(log(n-1)/log(2)); mk=round(log(m-1)/log(2)); nrhs=rhs; r=rhs; for i=nk-1:-1:mk, mi=2^i+1; ni=2^(i+1)+1; nrhs=zeros(mi,1); nrhs(2:mi-1)=.5*r(3:2:ni-2) + .25*(r(2:2:ni-3)+r(4:2:ni-1)); r=nrhs; end return