Vous êtes sur la page 1sur 10

# ALGORITMA JACOBI --------------------------------------------------------------------function x = jacobi ( A, b, xold, TOL, Nmax ) %JACOBI % % % calling % % % % inputs: % % % % % % % % % % output: % % % NOTE: % % % % % % % % approximate the solution

of the linear system Ax = b by applying the Jacobi method (simultaneous relaxation) sequences: x = jacobi ( A, b, xold, TOL, Nmax ) jacobi ( A, b, xold, TOL, Nmax ) A b xold TOL NMax x coefficient matrix for linear system - must be a square matrix right-hand side vector for linear system vector containing initial guess for solution of linear system convergence tolerance - applied to maximum norm of difference between successive approximations maximum number of iterations to be performed approximate solution of linear system

if JACOBI is called with no output arguments, the iteration number and the current approximation to the solution are displayed if the maximum number of iterations is exceeded, a meesage to this effect will be displayed and the current approximation will be returned in the output value

n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'jacobi error: matrix dimensions and vector dimension not compatible' ) return end; xnew = zeros ( 1, n ); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, xold(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xold(j) ); end; disp ( s ); end; for its = 1 : Nmax xnew(1) = ( b(1) - sum ( A(1,2:n) .* xold(2:n) ) ) / A(1,1); for i = 2 : n-1 xnew(i) = ( b(i) - sum ( A(i,1:i-1) .* xold(1:i-1) ) - sum ( A(i,i+1 :n) .* xold(i+1:n) ) ) / A(i,i); end; xnew(n) = ( b(n) - sum ( A(n,1:n-1) .* xold(1:n-1) ) ) / A(n,n);

if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', its, xnew(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xnew(j) ); end; disp ( s ); end; conv = max ( abs ( xnew - xold ) ); if ( conv < TOL ) x = xnew; return else xold = xnew; end; end; disp ( 'jacobi error: maximum number of iterations exceeded' ); if ( nargout == 1 ) x = xnew; end; -------------------------------------------------------------------Iterasi Jacobi -------------------------------------------------------------------function [x, error, iter, flag] = jacobi(A, x, b, max_it, tol) % % % % % % % % % % % % % % % % % % % % % % % % -- Iterative template routine -Univ. of Tennessee and Oak Ridge National Laboratory October 1, 1993 Details of this algorithm are described in "Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods", Barrett, Berry, Chan, Demmel, Donato, Dongarra, Eijkhout, Pozo, Romine, and van der Vorst, SIAM Publications, 1993. (ftp netlib2.cs.utk.edu; cd linalg; get templates.ps). [x, error, iter, flag] = jacobi(A, x, b, max_it, tol) jacobi.m solves the linear system Ax=b using the Jacobi Method. input A x b max_it tol REAL matrix REAL initial guess vector REAL right hand side vector INTEGER maximum number of iterations REAL error tolerance REAL solution vector REAL error norm INTEGER number of iterations performed INTEGER: 0 = solution found to tolerance 1 = no convergence given max_it % initialization

## bnrm2 = norm( b ); if ( bnrm2 == 0.0 ), bnrm2 = 1.0; end r = b - A*x;

error = norm( r ) / bnrm2; if ( error < tol ) return, end [m,n]=size(A); [ M, N ] = split( A , b, 1.0, 1 ); for iter = 1:max_it, x_1 = x; x = M \ (N*x + b); error = norm( x - x_1 ) / norm( x ); if ( error <= tol ), break, end end if ( error > tol ) flag = 1; end % END jacobi.m --------------------------------------------------------------------Iterasi Jacobi ---------------------------------------------------------------------% % % % % % % % % % % % % % % % % % % % Jacobi.m - Jacobi method for iterative solution of Ax=b INPUT: A b x0 tol maxiter coefficient matrix right-hand side initial guess tolerance for estimated relative error in infinity norm maximum number of Jacobi iterations to perform % no convergence % matrix splitting % begin iteration % update approximation % compute error % check convergence

OUTPUT: x - final iteration relerr - estimated relative error in infinity norm niter - number of iterations executed Notes: - If A has many zero components, pass A in sparse format. Written by Douglas Meade Created 19 September 2005 Revised 27 September 2005

function [x,relerr,niter] = jacobi(A,b,x0,tol,maxiter) relerr = inf; niter = 1; S = diag( diag(A) ); T = S - A; while relerr >= tol & niter < maxiter, x=S \ (b+T*x0); relerr = norm( x-x0,inf )/norm( x,inf ); x0 = x;

niter = niter+1; end ---------------------------------------------------------------------Algoritma Gauss-Siedel --------------------------------------------------------------------function x = gauss_seidel ( A, b, xold, TOL, Nmax ) %GAUSS_SEIDEL % % % calling % % % % inputs: % % % % % % % % % % output: % % % NOTE: % % % % % % % % approximate the solution of the linear system Ax = b by applying the Gauss-Seidel method (successive relaxation) sequences: x = gauss_seidel ( A, b, xold, TOL, Nmax ) gauss_seidel ( A, b, xold, TOL, Nmax ) A b xold TOL NMax x coefficient matrix for linear system - must be a square matrix right-hand side vector for linear system vector containing initial guess for solution of linear system convergence tolerance - applied to maximum norm of difference between successive approximations maximum number of iterations to be performed approximate solution of linear system

if GAUSS_SEIDEL is called with no output arguments, the iteration number and the current approximation to the solution are displayed if the maximum number of iterations is exceeded, a meesage to this effect will be displayed and the current approximation will be returned in the output value

n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'gauss_seidel error: matrix dimensions and vector dimension not compat ible' ) return end; xnew = zeros ( 1, n ); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, xold(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xold(j) ); end; disp ( s ); end; for its = 1 : Nmax

xnew(1) = ( b(1) - sum ( A(1,2:n) .* xold(2:n) ) ) / A(1,1); for i = 2 : n-1 xnew(i) = ( b(i) - sum ( A(i,1:i-1) .* xnew(1:i-1) ) - sum ( A(i,i+1 :n) .* xold(i+1:n) ) ) / A(i,i); end; xnew(n) = ( b(n) - sum ( A(n,1:n-1) .* xnew(1:n-1) ) ) / A(n,n); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', its, xnew(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xnew(j) ); end; disp ( s ); end; conv = max ( abs ( xnew - xold ) ); if ( conv < TOL ) x = xnew; return else xold = xnew; end; end; disp ( 'gauss_seidel error: maximum number of iterations exceeded' ); if ( nargout == 1 ) x = xnew; end;

--------------------------------------------------------------------Algoritma GAUSS-SIEDEL --------------------------------------------------------------------ALGORITMA: Untuk menyelesaikan sistem persamaan linier AX = b dengan A adalah matriks koefisien n n , b vektor konstanta n 1 , dan X vektor n 1 yang perlu di cari. INPUT : n, A, b dan hampiran awal Y = (y1 y2 y3 ...yn)T, batas toleransi T dan maksimum iterasi N. OUTPUT : X = (x1 x2 x3 ...xn)T atau pesan "gagal". LANGKAH-LANGKAH : 1. Set penghitung iterasi k = 1 2. WHILE k N DO (a) FOR i = 1, 2, 3, ..., n, hitung : (b) Set X = (x1 x2 x3 ...xn)T (c) IF ||X - Y|| < T THEN STOP (d) Tambah penghitung iterasi, k = k + 1 (e) FOR i = 1, 2, 3, ..., n, Set yi = xi (f) Set Y = (y1 y2 y3 ...yn)T 3. Tulis pesan "metode gagal setelah N iterasi" 4. STOP. -------------------------------------------------------------------MATLAB:

function [X1,g,H] = seidel(A,b,X0,T,N) H = X0'; n = length(b); X1 = X0 ; for k=1:N, for i=1:n, S=b(i)-A(i,1:i-1)*X1(1:i-1)-A(i,i+1:n)*X0(i+1:n); X1(i)=S/A(i,i); end g=abs(X1-X0); err=norm(g); relerr=err/(norm(X1)+eps); X0=X1;H=[H,X0'];if(err<t> end end -------------------------------------------------------------------Sebagai gambaran misalkan mencari penyelesaian SPL 10x1 - x2 +2x3=6 -x1+11x2-x3+3x4=25 2x1-x2+10x3-x4=-11 3x2-x3+8x4=15 Berikut pemakaian fungsi MATLAB seidel untuk penyelesaian soal di atas dan keluaran yang diperoleh : >> A=[10 -1 2 0;-1 11 -1 3;2 -1 10 -1;0 3 -1 8] A = 10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8 >> b=[6;25;-11;15] b = 6 25 -11 15 >> X0=[0;0;0;0] X0 = 0 0 0 0 >> T=0.0001;N=25; >> [X,g,H]=seidel(A,b,X0,T,N) X = 1.0000 2.0000 -1.0000 1.0000 g = 1.0e-004 * 0.8292 0.2017 0.2840 0.1111 H = Columns Columns Columns Columns Columns Columns 1 through 5 0 0 0 0 0.6000 6 through 10 2.3273 -0.9873 0.8789 1.0302 2.0369 11 through 15 -1.0145 0.9843 1.0066 2.0036 -1.0025 16 through 20 0.9984 1.0009 2.0003 -1.0003 0.9998 21 through 25 1.0001 2.0000 -1.0000 1.0000 1.0000 26 through 28 2.0 -1.0000 1.0000

----------------------------------------------------------------------KUMPULAN ITERASI -----------------------------------------------------------------------Gauss - Siedel function x = gauss_seidel ( A, b, xold, TOL, Nmax ) %GAUSS_SEIDEL approximate th e solution of the linear system Ax = b by applying % the Gauss-Seidel method (su ccessive relaxation) % % calling sequences: % x = gauss_seidel ( A, b, xold, TOL , Nmax ) % gauss_seidel ( A, b, xold, TOL, Nmax ) % % inputs: % A coefficient ma trix for linear system - must be a % square matrix % b right-hand side vector fo r linear system % xold vector containing initial guess for solution of % linear system % TOL convergence tolerance - applied to maximum norm of % difference bet ween successive approximations % NMax maximum number of iterations to be perform ed % % output: % x approximate solution of linear system % % NOTE: % if GAUSS_SE IDEL is called with no output arguments, the % iteration number and the current approximation to the % solution are displayed % % if the maximum number of itera tions is exceeded, a meesage % to this effect will be displayed and the current approximation % will be returned in the output value % n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'gauss_seidel error: matrix dimensions and vec tor dimension not compatible' ) return end; xnew = zeros ( 1, n ); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, xold(1) ); for j = 2 : n s = sprintf ( ' %s%10f ', s, xold(j) ); end; disp ( s ); end; for its = 1 : Nmax xnew(1) = ( b(1 ) - sum ( A(1,2:n) .* xold(2:n) ) ) / A(1,1); for i = 2 : n-1 xnew(i) = ( b(i) sum ( A(i,1:i-1) .* xnew(1:i-1) ) - sum ( A(i,i+1:n) .* xold(i+1:n) ) ) / A(i,i ); end; xnew(n) = ( b(n) - sum ( A(n,1:n-1) .* xnew(1:n-1) ) ) / A(n,n); if ( na rgout == 0 ) s = sprintf ( '%3d \t %10f ', its, xnew(1) ); for j = 2 : n s = spr intf ( '%s%10f ', s, xnew(j) ); end; disp ( s ); end; conv = max ( abs ( xnew xold ) ); if ( conv < TOL ) x = xnew; return else xold = xnew; end; end; disp ( 'gauss_seidel error: maximum number of iterations exceeded' ); if ( nargout == 1 ) x = xnew; end; Gauss Elimination function x = gauss_elim ( A, b ) %GAUSS_ELIM solve the linear system Ax = b usin g Gaussian elimination % with back substitution % % calling sequences: % x = gau ss_elim ( A, b ) % gauss_elim ( A, b ) % % inputs: % A coefficient matrix for li near system % (matrix must be square) % b right-hand side vector % % output: % x solution vector (i.e., vector for which Ax = b) % % NOTE: % this is intended as a demonstration routine - no pivoting % strategy is implemented to reduce the e ffects of roundoff % error % [nrow ncol] = size ( A ); if ( nrow ~= ncol ) disp ( 'gauss_elim error: Square coefficient matrix required' ); return; end; nb = le ngth ( b ); if ( nrow ~= nb ) disp ( 'gauss_elim error: Size of b-vector not com patible with matrix dimension' ) return; end; x = zeros ( 1, nrow ); % % Gaussia n elimination % for i = 1 : nrow - 1 if ( A(i,i) == 0 ) t = min ( find ( A(i+1:n row,i) ~= 0 ) + i ); if ( isempty(t) ) disp ( 'gauss_elim error: A matrix is sin gular' ); return end; temp = A(i,:); tb = b(i); A(i,:) = A(t,:); b(i) = b(t); A( t,:) = temp; b(t) = tb; end; for j = i+1 : nrow m = -A(j,i) / A(i,i); A(j,i) = 0 ; A(j, i+1:nrow) = A(j, i+1:nrow) + m * A(i, i+1:nrow); b(j) = b(j) + m * b(i); end; end; % % back substitution % x(nrow) = b(nrow) / A(nrow, nrow); for i = nro w - 1 : -1 : 1 x(i) = ( b(i) - sum ( x(i+1:nrow) .* A(i, i+1:nrow) ) ) / A(i,i); end; LU-Factor function [lu, pvt] = LUfactor ( A ) %LUFACTOR compute the LU decomposition for t he matrix A % % calling sequence: % [lu, pvt] = LUfactor ( A ) % % input: % A co efficient matrix for linear system % (matrix must be square) % % outputs: % lu m atrix containing LU decomposition of input matrix % A - the L matrix in the deco

mposition consists of % 1's along the main diagonal together with the % strictly lower triangular portion of the matrix lu; % the U matrix in the decomposition is the upper % triangular portion of the matrix lu % pvt vector which indicates the permutation of the rows % performed during the decomposition process % % NOT E: % this routine performs partial pivoting during the % decomposition process % % the system Ax = b can be solved by first applying LUfactor % to the coefficie nt matrix A and then applying the companion % routine, LUsolve, for each right-h and side vector b % [nrow ncol] = size ( A ); if ( nrow ~= ncol ) disp ( 'LUfact or error: Square coefficient matrix required' ); return; end; % % initialize row pointers % for i=1:nrow pvt(i) = i; end; for i = 1 : nrow - 1 % % partial pivot ing % t = min ( find ( abs(A(pvt(i:nrow),i)) == max(abs(A(pvt(i:nrow),i))) ) + i -1 ); if ( t ~= i ) temp = pvt(i); pvt(i) = pvt(t); pvt(t) = temp; end; % % term inate if matrix is singular % if ( A(pvt(i),i) == 0 ) disp ( 'LUfactor error: co efficient matrix is singular' ); lu = A; return end; % % elimination steps % for j = i+1 : nrow m = -A(pvt(j),i) / A(pvt(i),i); A(pvt(j),i) = -m; A(pvt(j), i+1: nrow) = A(pvt(j), i+1:nrow) + m * A(pvt(i), i+1:nrow); end; end; lu = A; LU-Solve function x = LUsolve ( lu, b, pvt ) %LUSOLVE perform forward and backward substi tution to obtain the % solution to the linear system Ax = b, where the LU % deco mposition of the coefficient matrix has previously % been determined % % calling sequence: % x = LUsolve ( lu, b, pvt ) % LUsolve ( lu, b, pvt ) % % inputs: % l u matrix containing LU decomposition of coefficient % matrix for the linear syst em - the L matrix in the % decomposition must consists of 1's along the main % d iagonal together with the strictly lower triangular % portion of the matrix lu; the U matrix in the % decomposition must be the upper triangular portion % of th e matrix lu % b right-hand side vector for linear system % pvt vector which indi cates the permutation of the rows % performed during the LU decomposition of the % coefficient matrix % % output: % x solution vector % % NOTE: % the system Ax = b can be solved by first applying LUfactor % to the coefficient matrix A and t hen applying LUsolve once % for each right-hand side vector b % [nrow ncol] = si ze ( lu ); % % forward substitution % z(1) = b(pvt(1)); for i = 2 : nrow z(i) = b(pvt(i)) - sum ( z(1:i-1) .* lu(pvt(i), 1:i-1) ); end; % % back substitution % x(nrow) = z(nrow) / lu(pvt(nrow), nrow); for i = nrow - 1 : -1 : 1 x(i) = ( z(i) - sum ( x(i+1:nrow) .* lu(pvt(i), i+1:nrow) ) ) / lu(pvt(i),i); end; Tridiagonal function y = tridiagonal ( c, a, b, r ) %TRIDIAGONAL solve a linear system with a tridiagonal coefficient matrix % % calling sequence: % x = tridiagonal ( c, a, b, r ) % tridiagonal ( c, a, b, r ) % % inputs: % c vector containing the entri es along lower diagonal % of the coefficient matrix % a vector containing the en tries along main diagonal % of the coefficient matrix % b vector containing the entries along upper diagonal % of the coefficient matrix % r right-hand side vec tor % % output: % x solution vector % % NOTE: % the entries in the vectors c, a and b are assumed to be % numbered as follows: % % | a(1) b(1) | % | c(1) a(2) b (2) | % | c(2) a(3) b(3) | % | . . . | % | . . . | % | . . b(n-1) | % | c(n-1) a (n) | % n = length ( a ); % % factorization step % for i = 1 : n-1 b(i) = b(i) / a(i); a(i+1) = a(i+1) - c(i) * b(i); end % % forward substitution % r(1) = r(1) / a(1); for i = 2 : n r(i) = ( r(i) - c(i-1) * r(i-1) ) / a(i); end % % back su bstitution % for i = n-1 : -1 : 1 r(i) = r(i) - r(i+1) * b(i); end if ( nargout == 0 ) disp ( r ) else y = r; end Jacobi function x = jacobi ( A, b, xold, TOL, Nmax ) %JACOBI approximate the solution o f the linear system Ax = b by applying % the Jacobi method (simultaneous relaxat ion) % % calling sequences: % x = jacobi ( A, b, xold, TOL, Nmax ) % jacobi ( A, b, xold, TOL, Nmax ) % % inputs: % A coefficient matrix for linear system - mus t be a % square matrix % b right-hand side vector for linear system % xold vecto r containing initial guess for solution of % linear system % TOL convergence tol erance - applied to maximum norm of % difference between successive approximatio

ns % NMax maximum number of iterations to be performed % % output: % x approxima te solution of linear system % % NOTE: % if JACOBI is called with no output argu ments, the % iteration number and the current approximation to the % solution ar e displayed % % if the maximum number of iterations is exceeded, a meesage % to this effect will be displayed and the current approximation % will be returned i n the output value % n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'jacobi error: matrix dimensions and vector dimension not compatible' ) return e nd; xnew = zeros ( 1, n ); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, xold(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xold(j) ); end; disp ( s ); end; for its = 1 : Nmax xnew(1) = ( b(1) - sum ( A(1,2:n) .* xold(2:n) ) ) / A( 1,1); for i = 2 : n-1 xnew(i) = ( b(i) - sum ( A(i,1:i-1) .* xold(1:i-1) ) - sum ( A(i,i+1:n) .* xold(i+1:n) ) ) / A(i,i); end; xnew(n) = ( b(n) - sum ( A(n,1:n -1) .* xold(1:n-1) ) ) / A(n,n); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', its, xnew(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xnew(j) ); end; dis p ( s ); end; conv = max ( abs ( xnew - xold ) ); if ( conv < TOL ) x = xnew; re turn else xold = xnew; end; end; disp ( 'jacobi error: maximum number of iterati ons exceeded' ); if ( nargout == 1 ) x = xnew; end; SOR function x = sor ( A, b, xold, omega, TOL, Nmax ) %SOR approximate the solution of the linear system Ax = b by applying % the SOR method (successive over-relaxa tion) % % calling sequences: % x = sor ( A, b, xold, omega, TOL, Nmax ) % sor ( A, b, xold, omega, TOL, Nmax ) % % inputs: % A coefficient matrix for linear sys tem - must be a % square matrix % b right-hand side vector for linear system % x old vector containing initial guess for solution of % linear system % omega rela xation parameter % TOL convergence tolerance - applied to maximum norm of % diff erence between successive approximations % NMax maximum number of iterations to be performed % % output: % x approximate solution of linear system % % NOTE: % i f SOR is called with no output arguments, the % iteration number and the current approximation to the % solution are displayed % % if the maximum number of iter ations is exceeded, a meesage % to this effect will be displayed and the current approximation % will be returned in the output value % n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'sor error: matrix dimensions and vector dime nsion not compatible' ) return end; xnew = zeros ( 1, n ); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, xold(1) ); for j = 2 : n s = sprintf ( '%s%10f ' , s, xold(j) ); end; disp ( s ); end; for its = 1 : Nmax xnew(1) = ( 1 - omega ) * xold(1) + omega * ( b(1) - sum ( A(1,2:n) .* xold(2:n) ) ) / A(1,1); for i = 2 : n-1 xnew(i) = ( 1 - omega ) * xold(i) + omega * ( b(i) - sum ( A(i,1:i-1) .* xnew(1:i-1) ) - sum ( A(i,i+1:n) .* xold(i+1:n) ) ) / A(i,i); end; xnew(n) = ( 1 - omega ) * xold(n) + omega * ( b(n) - sum ( A(n,1:n-1) .* xnew(1:n-1) ) ) / A (n,n); if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', its, xnew(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, xnew(j) ); end; disp ( s ); end; conv = max ( abs ( xnew - xold ) ); if ( conv < TOL ) x = xnew; return else xold = xnew; end ; end; disp ( 'sor error: maximum number of iterations exceeded' ); if ( nargout == 1 ) x = xnew; end; cONJ-grad function x = conj_grad ( A, b, x, TOL, Nmax ) %CONJ_GRAD approximate the solutio n of the linear system Ax = b by applying % the conjugate gradient method % % ca lling sequences: % x = conj_grad ( A, b, x, TOL, Nmax ) % conj_grad ( A, b, x, T OL, Nmax ) % % inputs: % A coefficient matrix for linear system - must be a % sq uare matrix % b right-hand side vector for linear system - must be % a column ve ctor % x column vector containing initial guess for solution of % linear system % TOL convergence tolerance - applied to Euclidean norm of % residual vector % N Max maximum number of iterations to be performed % % output: % x approximate sol ution of linear system % % NOTE: % if CONJ_GRAD is called with no output argumen ts, the % iteration number and the current approximation to the % solution are d isplayed % % if the maximum number of iterations is exceeded, a meesage % to thi s effect will be displayed and the current approximation % will be returned in t he output value % n = length ( b ); [r c] = size ( A ); if ( c ~= n ) disp ( 'co

nj_grad error: matrix dimensions and vector dimension not compatible' ) return e nd; if ( nargout == 0 ) s = sprintf ( '%3d \t %10f ', 0, x(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, x(j) ); end; disp ( s ); end; r = A * x - b; delta0 = r' * r; d = -r; for its = 1 : Nmax h = A * d; lambda = delta0 / ( d' * h ); x = x + lambda * d; r = r + lambda * h; delta1 = r' * r; if ( nargout == 0 ) s = sp rintf ( '%3d \t %10f ', its, x(1) ); for j = 2 : n s = sprintf ( '%s%10f ', s, x (j) ); end; disp ( s ); end; if ( sqrt(delta1) < TOL ) return else alpha = delta 1 / delta0; delta0 = delta1; d = -r + alpha * d; end; end; disp ( 'conj_grad err or: maximum number of iterations exceeded' ); Newton Sys function y = newton_sys ( F, J, x0, TOL, Nmax ) %NEWTON_SYS solve the system of nonlinear equations F(x) = 0 using % Newton's method % % calling sequences: % y = newton_sys ( F, J, x0, TOL, Nmax ) % newton_sys ( F, J, x0, TOL, Nmax ) % % in puts: % F vector-valued function of a vector argument which % defines the system of equations to be solved % J matrix-valued function which computes the Jacobia n % associated with the function F % x0 vector containing initial guess for solu tion of % nonlinear system % TOL convergence tolerance - applied to maximum norm of % difference between successive approximations % NMax maximum number of iter ations to be performed % % output: % y approximate solution of nonlinear system % % dependencies: % this routine uses both LUfactor and LUsolve % % NOTE: % if N EWTON_SYS is called with no output arguments, each % approximation to the soluti on is displayed % % if the maximum number of iterations is exceeded, a meesage % to this effect will be displayed and the current approximation % will be return ed in the output value % old = x0; for i = 1 : Nmax Fold = feval(F,old); Jold = feval(J,old); [lu pvt] = LUfactor ( Jold ); dx = LUsolve ( lu, -Fold, pvt ); new = old + dx; if ( nargout == 0 ) disp ( new ) end if ( max(abs(dx)) < TOL ) if ( nargout == 1 ) y = new; end return else old = new; end end disp('newton_sys err or: Maximum number of iterations exceeded'); if ( nargout == 1 ) y = new; end; Broyden function y = broyden ( F, J, x0, TOL, Nmax ) %BROYDEN solve the system of nonlin ear equations F(x) = 0 using % Broyden's method % % calling sequences: % y = bro yden ( F, J, x0, TOL, Nmax ) % broyden ( F, J, x0, TOL, Nmax ) % % inputs: % F v ector-valued function of a vector argument which % defines the system of equatio ns to be solved % J matrix-valued function which computes the Jacobian % associa ted with the function F % x0 vector containing initial guess for solution of % n onlinear system % TOL convergence tolerance - applied to maximum norm of % diffe rence between successive approximations % NMax maximum number of iterations to b e performed % % output: % y approximate solution of nonlinear system % % % NOTE: % if BROYDEN is called with no output arguments, each % approximation to the so lution is displayed % % if the maximum number of iterations is exceeded, a meesa ge % to this effect will be displayed and the current approximation % will be re turned in the output value % Fold = feval(F,x0)'; Jold = feval(J,x0); A0 = inv ( Jold ); dx = -A0 * Fold; x0 = x0 + dx; if ( nargout == 0 ) disp ( x0' ) end for i = 2 : Nmax Fnew = feval(F,x0)'; dy = Fnew - Fold; u = A0 * dy; v = dx' * A0; denom = dx' * u; A0 = A0 + ( dx - u ) * v / denom; dx = -A0 * Fnew; x0 = x0 + dx ; if ( nargout == 0 ) disp ( x0' ) end if ( max(abs(dx)) < TOL ) if ( nargout == 1 ) y = x0; end return else Fold = Fnew; end end disp('broyden error: Maximum n umber of iterations exceeded'); if ( nargout == 1 ) y = x0; end; Sumber: http://www.pcs.cnu.edu/~bbradie/msystems.html