Friday, July 24, 2009

continued..

bicgstab

BiConjugate Gradients Stabilized method

Syntax

  • x = bicgstab(A,b)
    bicgstab(A,b,tol)
    bicgstab(A,b,tol,maxit)
    bicgstab(A,b,tol,maxit,M)
    bicgstab(A,b,tol,maxit,M1,M2)
    bicgstab(A,b,tol,maxit,M1,M2,x0)
    bicgstab(afun,b,tol,maxit,m1fun,m2fun,x0,p1,p2,...)
    [x,flag] = bicgstab(A,b,...)
    [x,flag,relres] = bicgstab(A,b,...)
    [x,flag,relres,iter] = bicgstab(A,b,...)
    [x,flag,relres,iter,resvec] = bicgstab(A,b,...)

Description

x = bicgstab(A,b) attempts to solve the system of linear equations A*x=b for x. The n-by-n coefficient matrix A must be square and should be large and sparse. The column vector b must have length n. A can be a function afun such that afun(x) returns A*x.

If bicgstab converges, a message to that effect is displayed. If bicgstab fails to converge after the maximum number of iterations or halts for any reason, a warning message is printed displaying the relative residual norm(b-A*x)/norm(b) and the iteration number at which the method stopped or failed.

bicgstab(A,b,tol) specifies the tolerance of the method. If tol is [], then bicgstab uses the default, 1e-6.

bicgstab(A,b,tol,maxit) specifies the maximum number of iterations. If maxit is [], then bicgstab uses the default, min(n,20).

bicgstab(A,b,tol,maxit,M) and bicgstab(A,b,tol,maxit,M1,M2) use preconditioner M or M = M1*M2 and effectively solve the system inv(M)*A*x = inv(M)*b for x. If M is [] then bicgstab applies no preconditioner. M can be a function that returns M\x.

bicgstab(A,b,tol,maxit,M1,M2,x0) specifies the initial guess. If x0 is [], then bicgstab uses the default, an all zero vector.

bicgstab(afun,b,tol,maxit,m1fun,m2fun,x0,p1,p2,...) passes parameters p1,p2,... to functions afun(x,p1,p2,...), m1fun(x,p1,p2,...), and m2fun(x,p1,p2,...).

[x,flag] = bicgstab(A,b,...) also returns a convergence flag.


Flag
Convergence
0
bicgstab converged to the desired tolerance tol within maxit iterations.
1
bicgstab iterated maxit times but did not converge.
2
Preconditioner M was ill-conditioned.
3
bicgstab stagnated. (Two consecutive iterates were the same.)
4
One of the scalar quantities calculated during bicgstab became too small or too large to continue computing.

Whenever flag is not 0, the solution x returned is that with minimal norm residual computed over all the iterations. No messages are displayed if the flag output is specified.

[x,flag,relres] = bicgstab(A,b,...) also returns the relative residual norm(b-A*x)/norm(b). If flag is 0, relres <= tol.

[x,flag,relres,iter] = bicgstab(A,b,...) also returns the iteration number at which x was computed, where 0 <= iter <= maxit. iter can be an integer + 0.5, indicating convergence half way through an iteration.

[x,flag,relres,iter,resvec] = bicgstab(A,b,...) also returns a vector of the residual norms at each half iteration, including norm(b-A*x0).

Example

Example 1. This example first solves Ax = b by providing A and the preconditioner M1 directly as arguments. It then solves the same system using functions that return A and the preconditioner.

  • A = gallery('wilk',21);
    b = sum(A,2);
    tol = 1e-12;
    maxit = 15;
    M1 = diag([10:-1:1 1 1:10]);

    x = bicgstab(A,b,tol,maxit,M1,[],[]);

displays this message

  • bicgstab converged at iteration 12.5 to a solution with relative
    residual 2.9e-014

Alternatively, use this matrix-vector product function

  • function y = afun(x,n)
    y = [0;
    x(1:n-1)] + [((n-1)/2:-1:0)';
    (1:(n-1)/2)'] .*x + [x(2:n);
    0];

and this preconditioner backsolve function

  • function y = mfun(r,n)
    y = r ./ [((n-1)/2:-1:1)'; 1; (1:(n-1)/2)'];

as inputs to bicgstab

  • x1 = bicgstab(@afun,b,tol,maxit,@mfun,[],[],21);

Note that both afun and mfun must accept bicgstab's extra input n=21.

Example 2. This examples demonstrates the use of a preconditioner. Start with A = west0479, a real 479-by-479 sparse matrix, and define b so that the true solution is a vector of all ones.

  • load west0479;
    A = west0479;
    b = sum(A,2);
    [x,flag] = bicgstab(A,b)

flag is 1 because bicgstab does not converge to the default tolerance 1e-6 within the default 20 iterations.

  • [L1,U1] = luinc(A,1e-5);
    [x1,flag1] = bicgstab(A,b,1e-6,20,L1,U1)

flag1 is 2 because the upper triangular U1 has a zero on its diagonal. This causes bicgstab to fail in the first iteration when it tries to solve a system such as U1*y = r using backslash.

  • [L2,U2] = luinc(A,1e-6);
    [x2,flag2,relres2,iter2,resvec2] = bicgstab(A,b,1e-15,10,L2,U2)

flag2 is 0 because bicgstab converges to the tolerance of 3.1757e-016 (the value of relres2) at the sixth iteration (the value of iter2) when preconditioned by the incomplete LU factorization with a drop tolerance of 1e-6. resvec2(1) = norm(b) and resvec2(13) = norm(b-A*x2). You can follow the progress of bicgstab by plotting the relative residuals at the halfway point and end of each iteration starting from the initial estimate (iterate number 0).

  • semilogy(0:0.5:iter2,resvec2/norm(b),'-o')
    xlabel('iteration number')
    ylabel('relative residual')



    bin2dec









    Binary to decimal number conversion

    Syntax

  • bin2dec(binarystr)

Description

bin2dec(binarystr) interprets the binary string binarystr and returns the equivalent decimal number.

Examples

bin2dec('010111') returns 23.



bitand

Bit-wise AND

Syntax

  • C = bitand(A,B)

Description

C = bitand(A,B) returns the bit-wise AND of two nonnegative integer arguments A and B. To ensure the operands are integers, use the ceil, fix, floor, and round functions.

Examples

The five-bit binary representations of the integers 13 and 27 are 01101 and 11011, respectively. Performing a bit-wise AND on these numbers yields 01001, or 9.

  • C = bitand(13,27)

    C =

    9

No comments:

Post a Comment