Implements method of steepest descent to find the solution of Ax=b. Input arguments: A, symmetric positive definite matrix b, column vector tol, convergence is accepted when |Ax-b|<tol Output arguments: x, solution to Ax=b k, number of iterations
0001 function [ x,k ] = steepest_descent( A,b,tol ) 0002 % Implements method of steepest descent to find the solution of Ax=b. 0003 % Input arguments: 0004 % A, symmetric positive definite matrix 0005 % b, column vector 0006 % tol, convergence is accepted when |Ax-b|<tol 0007 % Output arguments: 0008 % x, solution to Ax=b 0009 % k, number of iterations 0010 0011 [n,m]=size(A); % finding the size of A 0012 [p,q]=size(b); % finding the size of b 0013 if n~= m; 0014 error('first input is not a square matrix'); 0015 elseif q~=1; 0016 error('second input is not a column vector'); 0017 elseif p~=n; 0018 error('input dimensions do not agree'); 0019 elseif tol<=0; 0020 error('tolerance should be positive'); 0021 elseif ~issymmetric(A); 0022 error('first input is not symmetric'); 0023 else 0024 [~,a] = chol(A); 0025 if a 0026 error('first input is not positive definite'); 0027 end 0028 end 0029 0030 x=zeros(n,1); % initialize x 0031 g=-b; % initialize gradient, the negative of the steepest descent direction 0032 k=0; % initialize counter 0033 0034 while sqrt(g'*g)>tol; 0035 w=g'*g/(g'*A*g); % calculate step size in the descent direction 0036 x=x-w*g; % update x 0037 g=A*x-b; % update g 0038 k=k+1; % increment counter 0039 end 0040 0041 end 0042