annotate scripts/sparse/bicgstab.m @ 9665:1dba57e9d08d

use blas_trans_type for xgemm
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 26 Sep 2009 10:41:07 +0200
parents 1673a0dc019f
children be55736a0783
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9278
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
1 ## Copyright (C) 2008 Radek Salac
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
2 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
4 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
9 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
14 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
18
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {} bicgstab (@var{A}, @var{b})
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {} bicgstab (@var{A}, @var{b}, @var{tol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
22 ## This procedure attempts to solve a system of linear equations A*x = b for x.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
23 ## The @var{A} must be square, symmetric and positive definite real matrix N*N.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
24 ## The @var{b} must be a one column vector with a length of N.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
25 ## The @var{tol} specifies the tolerance of the method, the default value is 1e-6.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
26 ## The @var{maxit} specifies the maximum number of iterations, the default value is min(20,N).
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
27 ## The @var{M1} specifies a preconditioner, can also be a function handler which returns M\X.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
28 ## The @var{M2} combined with @var{M1} defines preconditioner as preconditioner=M1*M2.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
29 ## The @var{x0} is the initial guess, the default value is zeros(N,1).
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
30 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
31 ## The value @var{x} is a computed result of this procedure.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
32 ## The value @var{flag} can be 0 when we reach tolerance in @var{maxit} iterations, 1 when
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
33 ## we don't reach tolerance in @var{maxit} iterations and 3 when the procedure stagnates.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
34 ## The value @var{relres} is a relative residual - norm(b-A*x)/norm(b).
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
35 ## The value @var{iter} is an iteration number in which x was computed.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
36 ## The value @var{resvec} is a vector of @var{relres} for each iteration.
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
37 ##
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
38 ## @end deftypefn
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
39
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
40 function [x, flag, relres, iter, resvec] = bicgstab (A, b, tol, maxit, M1, M2, x0)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
41
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
42 if (nargin < 2 || nargin > 7 || nargout > 5)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
43 print_usage ();
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
44 elseif (!isnumeric (A) || rows (A) != columns (A))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
45 error ("bicgstab: the first argument must be a n-by-n matrix");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
46 elseif (!isvector (b))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
47 error ("bicgstab: b must be a vector");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
48 elseif (!any (b))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
49 error ("bicgstab: b shuldn't be a vector of zeros");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
50 elseif (rows (A) != rows (b))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
51 error ("bicgstab: the first and second argument must have the same number of rows");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
52 elseif (nargin > 2 && !isscalar (tol))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
53 error ("bicgstab: tol must be a scalar");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
54 elseif (nargin > 3 && !isscalar (maxit))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
55 error ("bicgstab: maxit must be a scalar");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
56 elseif (nargin > 4 && ismatrix (M1) && (rows (M1) != rows (A) || columns (M1) != columns (A)))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
57 error ("bicgstab: M1 must have the same number of rows and columns as A");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
58 elseif (nargin > 5 && (!ismatrix (M2) || rows (M2) != rows (A) || columns (M2) != columns (A)))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
59 error ("bicgstab: M2 must have the same number of rows and columns as A");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
60 elseif (nargin > 6 && !isvector (x0))
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
61 error ("bicgstab: x0 must be a vector");
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
62 elseif (nargin > 6 && rows (x0) != rows (b))
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
63 error ("bicgstab: x0 must have the same number of rows as b");
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
64 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
65
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
66 ## Default tolerance.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
67 if (nargin < 3)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
68 tol = 1e-6;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
69 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
70
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
71 ## Default maximum number of iteration.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
72 if (nargin < 4)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
73 maxit = min (rows (b), 20);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
74 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
75
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
76 ## Left preconditioner.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
77 if (nargin == 5)
9279
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
78 if (isnumeric (M1))
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
79 precon = @(x) M1 \ x;
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
80 endif
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
81 elseif (nargin > 5)
9279
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
82 if (issparse (M1) && issparse (M2))
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
83 precon = @(x) M2 \ (M1 \ x);
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
84 else
9279
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
85 M = M1*M2;
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
86 precon = @(x) M \ x;
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
87 endif
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
88 else
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
89 precon = @(x) x;
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
90 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
91
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
92 ## specifies initial estimate x0
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
93 if (nargin < 7)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
94 x = zeros (rows (b), 1);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
95 else
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
96 x = x0;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
97 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
98
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
99 norm_b = norm (b);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
100
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
101 res = b - A*x;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
102 rr = res;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
103
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
104 ## Vector of the residual norms for each iteration.
9278
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
105 resvec = [norm(res)/norm_b];
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
106
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
107 ## Default behaviour we don't reach tolerance tol within maxit iterations.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
108 flag = 1;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
109
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
110 for iter = 1:maxit
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
111 rho_1 = res' * rr;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
112
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
113 if (iter == 1)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
114 p = res;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
115 else
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
116 beta = (rho_1 / rho_2) * (alpha / omega);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
117 p = res + beta * (p - omega * v);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
118 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
119
9279
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
120 phat = precon (p);
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
121
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
122 v = A * phat;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
123 alpha = rho_1 / (rr' * v);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
124 s = res - alpha * v;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
125
9279
1673a0dc019f fix & improve preconditioning strategy in cgs & bicgstab
Jaroslav Hajek <highegg@gmail.com>
parents: 9278
diff changeset
126 shat = precon (s);
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
127
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
128 t = A * shat;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
129 omega = (t' * s) / (t' * t);
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
130 x = x + alpha * phat + omega * shat;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
131 res = s - omega * t;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
132 rho_2 = rho_1;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
133
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
134 relres = norm (res) / norm_b;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
135 resvec = [resvec; relres];
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
136
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
137 if (relres <= tol)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
138 ## We reach tolerance tol within maxit iterations.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
139 flag = 0;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
140 break;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
141 elseif (resvec (end) == resvec (end - 1))
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8416
diff changeset
142 ## The method stagnates.
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
143 flag = 3;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
144 break;
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
145 endif
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
146 endfor
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
147
9278
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
148 if (nargout < 2)
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
149 if (flag == 0)
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
150 printf (["bicgstab converged at iteration %i ",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
151 "to a solution with relative residual %e\n"],iter,relres);
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
152 elseif (flag == 3)
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
153 printf (["bicgstab stopped at iteration %i ",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
154 "without converging to the desired tolerance %e\n",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
155 "because the method stagnated.\n",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
156 "The iterate returned (number %i) has relative residual %e\n"],iter,tol,iter,relres);
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
157 else
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
158 printf (["bicgstab stopped at iteration %i ",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
159 "without converging to the desired toleranc %e\n",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
160 "because the maximum number of iterations was reached.\n",
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
161 "The iterate returned (number %i) has relative residual %e\n"],iter,tol,iter,relres);
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
162 endif
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
163 endif
2b35cb600d50 improve bicgstab and cgs
Radek Salac <salac.r@gmail.com>
parents: 8920
diff changeset
164
8416
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
165 endfunction
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
166
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
167 %!demo
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
168 %! % Solve system of A*x=b
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
169 %! A = [5 -1 3;-1 2 -2;3 -2 3]
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
170 %! b = [7;-1;4]
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
171 %! [x, flag, relres, iter, resvec] = bicgstab(A, b)
0a56e5c21c29 Add the bicgstab function
Radek Salac <salac.r@gmail.com>
parents:
diff changeset
172