9.5. GMRES 371
40. g = rho*eye(kmax+1,1);
41. v(2:nx,2:ny,1) = r(2:nx,2:ny)/rho;
42. k = 0;
43. % Begin gmres loop.
44. while((rho
A errtol) & (k ? kmax))
45. k = k+1;
46. % Matrix vector product.
47. v(2:nx,2:ny,k+1) = -aw*v(1:nx-1,2:ny,k)-ae*v(3:nx+1,2:ny,k)-...
48. as*v(2:nx,1:ny-1,k)-an*v(2:nx,3:ny+1,k)+...
49. ac*v(2:nx,2:ny,k);
50. % This preconditioner is SSOR.
51. rhat = ssorpc(nx,ny,ae,aw,as,an,ac,rac,w,v(:,:,k+1),rhat);
52. v(2:nx,2:ny,k+1) = rhat(2:nx,2:ny);
53. % Begin modified GS. May need to reorthogonalize.
54. for j=1:k
55. h(j,k) = sum(sum(v(2:nx,2:ny,j).*v(2:nx,2:ny,k+1)));
56. v(2:nx,2:ny,k+1) = v(2:nx,2:ny,k+1)-h(j,k)*v(2:nx,2:ny,j);
57. end
58. h(k+1,k) = sum(sum(v(2:nx,2:ny,k+1).*v(2:nx,2:ny,k+1)))^.5;
59. if(h(k+1,k) ~= 0)
60. v(2:nx,2:ny,k+1) = v(2:nx,2:ny,k+1)/h(k+1,k);
61. end
62. % Apply old Givens rotations to h(1:k,k).
63. if k
A1
64. for i=1:k-1
65. hik = c(i)*h(i,k)-s(i)*h(i+1,k);
66. hipk = s(i)*h(i,k)+c(i)*h(i+1,k);
67. h(i,k) = hik;
68. h(i+1,k) = hipk;
69. end
70. end
71. nu = norm(h(k:k+1,k));
72. % May need better Givens implementation.
73. % Define and Apply new Givens rotations to h(k:k+1,k).
74. if nu~=0
75. c(k) = h(k,k)/nu;
76. s(k) = -h(k+1,k)/nu;
77. h(k,k) = c(k)*h(k,k)-s(k)*h(k+1,k);
78. h(k+1,k) = 0;
79. gk = c(k)*g(k) -s(k)*g(k+1);
80. gkp = s(k)*g(k) +c(k)*g(k+1);
81. g(k) = gk;
82. g(k+1) = gkp;
83. end
84. rho=abs(g(k+1));
© 2004 by Chapman & Hall/CRC