annotate scripts/polynomial/polyeig.m @ 15187:045ae93e8fe9

polyeig: return eigenvectors of the _correct_ size
author Ed Meyer <eem2314@gmail.com>
date Thu, 19 Jul 2012 04:53:34 +0100
parents 504fec921af5
children 2ad5e6212cd7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15186
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
1 ## Copyright (C) 2012 Fotios Kasolis
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
2 ##
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
4 ##
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
9 ##
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
14 ##
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
18
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{z} =} polyeig (@var{C0}, @var{C1}, @dots{}, @var{Cl})
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {[ @var{v}, @var{z} ] =} polyeig (@var{C0}, @var{C1}, @dots{}, @var{Cl})
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
22 ## Solve the polynomial eigenvalue problem of degree @var{l}.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
23 ##
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
24 ## Given a @var{n*n} matrix polynomial @var{C(s)} = @var{C0 + C1 s + @dots{} + Cl s^l} polyeig
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
25 ## solves the eigenvalue problem (@var{C0} + @var{C1} + @dots{} + @var{Cl})v = 0.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
26 ## Note that the eigenvalues @var{z} are the zeros of the matrix polynomial. @var{z} is a
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
27 ## @var{lxn} vector and @var{v} is a @var{(n x n)l} matrix with columns that correspond to
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
28 ## the eigenvectors.
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
29 ## @seealso{eig, eigs, compan}
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
30 ## @end deftypefn
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
31
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
32 ## Author: Fotios Kasolis
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
33
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
34 function [ z, varargout ] = polyeig (varargin)
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
35
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
36 if ( nargout > 2 )
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
37 print_usage ();
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
38 endif
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
39
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
40 nin = numel (varargin);
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
41
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
42 n = zeros (1, nin);
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
43
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
44 for cnt = 1 : nin
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
45 if ! ( issquare (varargin{cnt}) )
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
46 error ("polyeig: coefficients must be square matrices");
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
47 endif
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
48 n(cnt) = size (varargin{cnt}, 1);
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
49 endfor
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
50
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
51 if numel (unique (n)) > 1
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
52 error ("polyeig: coefficients must have the same dimensions");
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
53 endif
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
54 n = unique (n);
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
55
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
56 # matrix polynomial degree
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
57 l = nin - 1;
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
58
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
59 # form needed matrices
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
60 C = [ zeros(n * (l - 1), n), eye(n * (l - 1)); -cell2mat(varargin(1 : end - 1)) ];
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
61 D = [ eye(n * (l - 1)), zeros(n * (l - 1), n); zeros(n, n * (l - 1)), varargin{end} ];
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
62
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
63 % solve generalized eigenvalue problem
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
64 if ( isequal (nargout, 1) )
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
65 z = eig (C, D);
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
66 else
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
67 [ z, v ] = eig (C, D);
15187
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
68 varargout{1} = v;
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
69 % return n-element eigenvectors normalized so
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
70 % that the infinity-norm = 1
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
71 z = z(1:n,:);
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
72 % max() takes the abs if complex:
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
73 t = max(z);
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
74 z /= diag(t);
15186
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
75 endif
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
76
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
77 endfunction
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
78
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
79 %!test
15187
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
80 %! C0 = [8, 0; 0, 4]; C1 = [1, 0; 0, 1];
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
81 %! [v,z] = polyeig (C0, C1);
15186
504fec921af5 polyeig: new function
Fotios Kasolis <fotios.kasolis@gmail.com>
parents:
diff changeset
82 %! assert (isequal (z(1), -8), true);
15187
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
83 %! d = C0*v + C1*v*z
045ae93e8fe9 polyeig: return eigenvectors of the _correct_ size
Ed Meyer <eem2314@gmail.com>
parents: 15186
diff changeset
84 %! assert (isequal (norm(d), 0.0), true);