Page 139 - Numerical methods for chemical engineering
P. 139

Numerical calculation of eigenvalues and eigenvectors               125



                  e = eigs(A,3,‘SM’),
                  e=
                     0.1300
                     0.0581
                     0.0146

                  and
                  e = eigs(A,4,1.0),
                  e=
                     1.2908
                     1.0706
                     0.8639
                     0.6738
                  In addition to this output, eigs writes information about its internal calculations to the
                  screen. To turn this off, use
                  OPTS.disp = 0;
                  e = eigs(A,5,‘LM’,OPTS);
                  Other fields in OPTS allow us to modify the behavior of the algorithm, e.g. by decreasing
                  the tolerances. Type help eigs for more information.
                    With two arguments, eigs returns a matrix W of eigenvectors and a diagonal matrix D of
                  eigenvalues such that AW = WD. The following code computes and plots the eigenvectors
                  of largest and smallest magnitudes,

                  [W,D] = eigs(A,2,‘BE’,OPTS);
                  figure; plot(W(:,1),‘–’);
                  hold on; plot(W(:,2));
                  xlabel(‘component’); ylabel(‘w(k)’);
                  title(‘Eigenvectors of 1-D diffusion matrix’);
                  phrase1 = [‘ \ lambda = ’, num2str(D(1,1))];
                  phrase2 = [‘ \ lambda = ’, num2str(D(2,2))];
                  legend(phrase1, phrase2, ‘Location’, ’Best’);

                  The graph generated by this code is shown in Figure 3.5.
                    Above, we have called eigs for a sparse-format matrix A. A matrix stored in full matrix
                  format can also be used. Additionally, we can merely supply a routine that returns for each
                  input v, the corresponding vector Av. For the matrix above, this is done by

                  function Av = diff matrix 1D mult(v);
                  N = length(v);
                  Av = zeros(N,1);
                  Av(1) = 2*v(1) - v(2);
                  for k = 2:(N-1)
                      Av(k) = -v(k-1) +2*v(k)-v(k+1);
   134   135   136   137   138   139   140   141   142   143   144