%
%   SR_DEMO1.M   Analyze SR Method for a Simple 3x3 System
%
%   This program solves a system of equations using the successive 
%   relaxation (SR) method for a range of relaxation parameters.  It also
%   computes the spectral radius for each case.  The idea here is to
%   illustrate several key points concerning iterative methods for the 
%   solution of linear equations.
%
%   This script file calls the SR.M routine to apply the SR method.
%
%   File written by J. R. White, UMass-Lowell (Aug. 2003)
%

%
%   getting started
      clear all,  close all,  nfig = 0;
%
%   define system of interest and vector of relaxation parameters
      A = [1 2 3;2 9 6;3 6 27];  b = [2 -4 24]';
      alf = 0.8:0.025:1.6;   Nalf = length(alf);
%
%   solve system for range of alf and save k = # of iterations to convergence
      tol = 1e-5;   M = 1000;   k = zeros(size(b));
      for j = 1:Nalf
        xo = zeros(size(b));  [x,k(j)] = sr(A,b,xo,alf(j),tol,M);
      end
%
%   now compute the spectral radius
      L = tril(A,-1);  D = diag(diag(A));  U = triu(A,1);
      p = zeros(size(b));
      for j = 1:Nalf
        B = inv(alf(j)*L + D)*((1-alf(j))*D - alf(j)*U);
        v = eig(B);   p(j) = max(abs(v));
      end
%
%   plot results
      nfig = nfig+1;   figure(nfig)
      subplot(2,1,1), plot(p,k,'b.','LineWidth',2),grid
      xlabel('spectral radius'),ylabel('# iterations')
      title('SR\_Demo1:  Results for Analysis of Convergence Rates for SR Method')
      subplot(2,2,3), plot(alf,k,'r-','LineWidth',2),grid
      xlabel('relaxation parameter'),ylabel('# iterations')
      subplot(2,2,4), plot(alf,p,'g-','LineWidth',2),grid
      xlabel('relaxation parameter'),ylabel('spectral radius')
%
%   end of program
 
