comparison scripts/sparse/pcg.m @ 14335:ce2b59a6d0e5

maint: periodic merge of stable to default.
author Rik <octave@nomad.inbox5.com>
date Sun, 05 Feb 2012 15:32:24 -0800
parents 11949c9795a0 4d917a6a858b
children f3d52523cde1
comparison
equal deleted inserted replaced
14333:6dd710b73150 14335:ce2b59a6d0e5
122 ## Let us consider a trivial problem with a diagonal matrix (we exploit the 122 ## Let us consider a trivial problem with a diagonal matrix (we exploit the
123 ## sparsity of A) 123 ## sparsity of A)
124 ## 124 ##
125 ## @example 125 ## @example
126 ## @group 126 ## @group
127 ## n = 10; 127 ## n = 10;
128 ## A = diag (sparse (1:n)); 128 ## A = diag (sparse (1:n));
129 ## b = rand (n, 1); 129 ## b = rand (n, 1);
130 ## [l, u, p, q] = luinc (A, 1.e-3); 130 ## [l, u, p, q] = luinc (A, 1.e-3);
131 ## @end group 131 ## @end group
132 ## @end example 132 ## @end example
133 ## 133 ##
134 ## @sc{Example 1:} Simplest use of @code{pcg} 134 ## @sc{Example 1:} Simplest use of @code{pcg}
135 ## 135 ##
136 ## @example 136 ## @example
137 ## x = pcg(A,b) 137 ## x = pcg (A,b)
138 ## @end example 138 ## @end example
139 ## 139 ##
140 ## @sc{Example 2:} @code{pcg} with a function which computes 140 ## @sc{Example 2:} @code{pcg} with a function which computes
141 ## @code{@var{A} * @var{x}} 141 ## @code{@var{A} * @var{x}}
142 ## 142 ##
143 ## @example 143 ## @example
144 ## @group 144 ## @group
145 ## function y = apply_a (x) 145 ## function y = apply_a (x)
146 ## y = [1:N]'.*x; 146 ## y = [1:N]' .* x;
147 ## endfunction 147 ## endfunction
148 ## 148 ##
149 ## x = pcg ("apply_a", b) 149 ## x = pcg ("apply_a", b)
150 ## @end group 150 ## @end group
151 ## @end example 151 ## @end example
152 ## 152 ##
153 ## @sc{Example 3:} @code{pcg} with a preconditioner: @var{l} * @var{u} 153 ## @sc{Example 3:} @code{pcg} with a preconditioner: @var{l} * @var{u}
154 ## 154 ##
155 ## @example 155 ## @example
156 ## x = pcg (A, b, 1.e-6, 500, l*u); 156 ## x = pcg (A, b, 1.e-6, 500, l*u)
157 ## @end example 157 ## @end example
158 ## 158 ##
159 ## @sc{Example 4:} @code{pcg} with a preconditioner: @var{l} * @var{u}. 159 ## @sc{Example 4:} @code{pcg} with a preconditioner: @var{l} * @var{u}.
160 ## Faster than @sc{Example 3} since lower and upper triangular matrices 160 ## Faster than @sc{Example 3} since lower and upper triangular matrices
161 ## are easier to invert 161 ## are easier to invert
162 ## 162 ##
163 ## @example 163 ## @example
164 ## x = pcg (A, b, 1.e-6, 500, l, u); 164 ## x = pcg (A, b, 1.e-6, 500, l, u)
165 ## @end example 165 ## @end example
166 ## 166 ##
167 ## @sc{Example 5:} Preconditioned iteration, with full diagnostics. The 167 ## @sc{Example 5:} Preconditioned iteration, with full diagnostics. The
168 ## preconditioner (quite strange, because even the original matrix 168 ## preconditioner (quite strange, because even the original matrix
169 ## @var{A} is trivial) is defined as a function 169 ## @var{A} is trivial) is defined as a function
170 ## 170 ##
171 ## @example 171 ## @example
172 ## @group 172 ## @group
173 ## function y = apply_m (x) 173 ## function y = apply_m (x)
174 ## k = floor (length (x) - 2); 174 ## k = floor (length (x) - 2);
175 ## y = x; 175 ## y = x;
176 ## y(1:k) = x(1:k)./[1:k]'; 176 ## y(1:k) = x(1:k) ./ [1:k]';
177 ## endfunction 177 ## endfunction
178 ## 178 ##
179 ## [x, flag, relres, iter, resvec, eigest] = ... 179 ## [x, flag, relres, iter, resvec, eigest] = ...
180 ## pcg (A, b, [], [], "apply_m"); 180 ## pcg (A, b, [], [], "apply_m");
181 ## semilogy (1:iter+1, resvec); 181 ## semilogy (1:iter+1, resvec);
182 ## @end group 182 ## @end group
183 ## @end example 183 ## @end example
184 ## 184 ##
185 ## @sc{Example 6:} Finally, a preconditioner which depends on a 185 ## @sc{Example 6:} Finally, a preconditioner which depends on a
186 ## parameter @var{k}. 186 ## parameter @var{k}.
187 ## 187 ##
188 ## @example 188 ## @example
189 ## @group 189 ## @group
190 ## function y = apply_M (x, varargin) 190 ## function y = apply_M (x, varargin)
191 ## K = varargin@{1@}; 191 ## K = varargin@{1@};
192 ## y = x; 192 ## y = x;
193 ## y(1:K) = x(1:K)./[1:K]'; 193 ## y(1:K) = x(1:K) ./ [1:K]';
194 ## endfunction 194 ## endfunction
195 ## 195 ##
196 ## [x, flag, relres, iter, resvec, eigest] = ... 196 ## [x, flag, relres, iter, resvec, eigest] = ...
197 ## pcg (A, b, [], [], "apply_m", [], [], 3) 197 ## pcg (A, b, [], [], "apply_m", [], [], 3)
198 ## @end group 198 ## @end group
199 ## @end example 199 ## @end example
200 ## 200 ##
201 ## References: 201 ## References:
202 ## 202 ##
214 ## @seealso{sparse, pcr} 214 ## @seealso{sparse, pcr}
215 ## @end deftypefn 215 ## @end deftypefn
216 216
217 ## Author: Piotr Krzyzanowski <piotr.krzyzanowski@mimuw.edu.pl> 217 ## Author: Piotr Krzyzanowski <piotr.krzyzanowski@mimuw.edu.pl>
218 ## Modified by: Vittoria Rezzonico <vittoria.rezzonico@epfl.ch> 218 ## Modified by: Vittoria Rezzonico <vittoria.rezzonico@epfl.ch>
219 ## - Add the ability to provide the pre-conditioner as two separate 219 ## - Add the ability to provide the pre-conditioner as two separate matrices
220 ## matrices
221 220
222 function [x, flag, relres, iter, resvec, eigest] = pcg (A, b, tol, maxit, m1, m2, x0, varargin) 221 function [x, flag, relres, iter, resvec, eigest] = pcg (A, b, tol, maxit, m1, m2, x0, varargin)
223 222
224 ## M = M1*M2 223 ## M = M1*M2
225 224