function [vout,fout]=twog(vin,fin,nu1,nu2) % TWOG two grid method with the full data structure % global sigma % % cheap hack to back out the level % ntl=length(fin); l=max(1,floor(log2(ntl))-1); % % lower and upper bounds for the current mesh and the one below % fout=fin; vout=vin; ifu=ntl; ifl=ntl-2^l; icu=ifl-1; icl=icu-2^(l-1); nl=2^l; h=1/nl; hm2=nl*nl; nt=nl+1; for ij=1:nu1 vout(ifl:ifu)=dampj(vout(ifl:ifu),fin(ifl:ifu)); end av=zeros(nt,1); av(2:nt-1)=-vout(ifl:ifu-2) +(2+sigma*h*h)*vout(ifl+1:ifu-1) -vout(ifl+2:ifu); % % prepare coarse mesh data % r=fin(ifl:ifu)-hm2*av; fout(icl:icu)=ftoc(r); % % call direct solver % vout(icl:icu)=helmholtz(fout(icl:icu)); vout(ifl:ifu)=vout(ifl:ifu)+ctof(vout(icl:icu)); for ij=1:nu2 vout(ifl:ifu)=dampj(vout(ifl:ifu),fin(ifl:ifu)); end