Page 257 - MATLAB an introduction with applications
P. 257

242 ———  MATLAB: An Introduction with Applications

                   The MATLAB program for this presented below:

                   %Given the diagonals c and d of A = [c\d\c] and the value of l, this function returns
                   the sturn sequence P (l),P (l),P (l),……..P (l). Note that P (l) =  | A −λ     1  |
                                                1
                                                                                     n
                                         0
                                                                   n
                                                       2
                   A = [2 –1 0 0;–1 2 –2 0;0 –2 2 –1;0 0 –1 2];
                   d = diag(A)’;
                   c = [–1 –2 –1];
                   lam = input(‘Enter guess value lambda\n’);
                   n = length(d)+1;
                   p = ones(n,1);
                   p(2) = d(1)–lam;
                   for i = 2:n–1
                      p(i+1) = (d(i)–lam)*p(i)–(c(i–1)^2)*p1(i–1);
                   end
                   fprintf(‘sturn sequence p(%f) is\n’,lam);
                   disp(p);
                   %%% number of sign changes in the sturn sequence p is
                   %%% number eigenvalues of matrix A that are smaller than
                   %%% lam_min
                   n = length(p);
                   oldsign = 1;
                   num_eval = 0;
                   for i = 2:n
                      psign = sign(p(i));
                      if psign = 0  psign = –oldsign;
                      end
                      if psign*oldsign <0
                         num_eval = num_eval+1;
                      end
                      oldsign = psign;
                   end
                   fprintf(‘Number of eigenvalues less than lamda = %f are %d\n’,lam,num_eval);
                   Output of the program is as follows (in two runs)
                   >>Enter guess value lambda
                   0
                   sturn sequence p(0.000000) is
                         1
                         2
                         4
                         0
                        –4
   252   253   254   255   256   257   258   259   260   261   262