23.12.2012 Views

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

icg<br />

2-148<br />

maxit = 15;<br />

M1 = spdiags([on/(-2) on],-1:0,n,n);<br />

M2 = spdiags([4*on -on],0:1,n,n);<br />

x = bicg(A,b,tol,maxit,M1,M2,[]);<br />

bicg converged at iteration 9 to a solution with relative<br />

residual 5.3e-009<br />

Alternatively, use this matrix-vector product function<br />

function y = afun(x,n,transp_flag)<br />

if (nargin > 2) & strcmp(transp_flag,'transp')<br />

y = 4 * x;<br />

y(1:n-1) = y(1:n-1) - 2 * x(2:n);<br />

y(2:n) = y(2:n) - x(1:n-1);<br />

else<br />

y = 4 * x;<br />

y(2:n) = y(2:n) - 2 * x(1:n-1);<br />

y(1:n-1) = y(1:n-1) - x(2:n);<br />

end<br />

as input to bicg.<br />

x1 = bicg(@afun,b,tol,maxit,M1,M2,[],n);<br />

Example 2. Start with A = west0479 and make the true solution the vector of<br />

all ones.<br />

load west0479;<br />

A = west0479;<br />

b = sum(A,2);<br />

You can accurately solve A*x = b using backslash since A is not so large.<br />

x = A \ b;<br />

norm(b-A*x) / norm(b)<br />

ans =<br />

1.2454e-017<br />

Now try to solve A*x = b with bicg.<br />

[x,flag,relres,iter,resvec] = bicg(A,b)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!