• No results found

Inputting Special Matrices .1 Numerical matrix input

Linear Algebra Problems

4.1 Inputting Special Matrices .1 Numerical matrix input

Although all the matrices can be entered into MATLAB workspace using the low-level statement discussed earlier, it might be complicated for some matrices with special structures. For instance, if one wants to enter an identity matrix, one should use the existing function eye() instead. In this section, the specifications of some special matrices will be presented.

Matrices of zeros, ones and identity matrices

In matrix theory, a matrix with all its elements 0 is referred to as a zero matrix, while a matrix with all its elements 1 is referred to as a matrix of ones.

If the diagonal elements are 1 with the rest of the elements 0, the matrix is referred to as an identity matrix. This concept can be extended to m × n matrices. Matrix of zeros, matrix of ones and identity matrix can be entered into MATLAB using the following statements

A=zeros(n), B=ones(n), C=eye(n) % n × n square matrix

A=zeros(m, n); B=ones(m, n); C=eye(m, n) % m × n rectangular matrix A=zeros(size(B)) % with the same size of B

Example 4.1 A 3 × 8 zero matrix A and an extended identity matrix B, with the same size of A can be entered into MATLAB environment using the following statements

>> A=zeros(3,8), B=eye(size(A))

The two matrices can be established in MATLAB workspace A=

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 , B =

1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0

 .

Functions zeros() and ones() can also be used to define multi-dimensional arrays. For instance, zeros(3,4,5) can be used to define a 3×4×5 zero array.

Matrices with random elements

If all the elements in a matrix satisfy uniform distribution within the [0, 1]

interval, it can be defined using MATLAB function rand(). The syntax of

such a function is

A=rand(n) % generates an n × n uniformly distributed random matrix A=rand(n,m) % generates an n × m random matrix

Function rand() can also be used to define multi-dimensional random arrays. Another function randn() can be used to define standard normal distributed random matrices. The statement rand(size(A)) can be used to declare a random matrix of size A.

Here, the random number is in fact pseudo-random numbers, and can be generated mathematically. It is easy to generate random numbers satisfy-ing certain predefined distributions. Another advantages of pseudo-random numbers are that they can be generated repeatedly.

If one wants to obtain a uniformly distributed random number over (a, b) in-terval, one may generate uniformly distributed pseudo-random matrix V =rand(n,m) over (0, 1) interval, then the expected matrix can be generated by V1= a + (b − a)*V command.

Diagonal matrices

Mathematical description to a diagonal matrix is

diag(α1, α2, · · · , αn) =

where all the non-diagonal elements are 0. A MATLAB function diag() can be used to deal with diagonal matrix related problems

A=diag(V ) % define a matrix from given vector

V =diag(A) % extract diagonal vector from a given matrix A=diag(V ,k) % define the kth diagonal elements V

Example 4.2 MATLAB function diag() is an interesting function. Different syntax are allowed for different tasks. For instance, the following statements can be used to define different matrices

>> C=[1 2 3]; V=diag(C)

V1=diag(V) % diagonal elements extraction C=[1 2 3]; V2=diag(C,2), V3=diag(C,-1) and they yield

In fact, k can be assigned to negative integers, indicating one wants to specify the kth lower-diagonal elements. With the use of such properties

>> V=diag([1 2 3 4])+diag([2 3 4],1)+diag([5 4 3],-1)

the tri-diagonal matrix V =

 can then be established.

Assume that there exist the following matrices A1, A2, · · · , An, the follow-ing MATLAB function diagm() can be written to construct block diagonal matrices

1 function A=diagm(varargin)

2 A=[];

3 for i=1:length(varargin),

4 A1=varargin{i}; [n,m]=size(A); [n1,m1]=size(A1);

5 A(n+1:n+n1,m+1:m+m1)=A1;

The general form of a Hankel matrix is given below, with all the elements in each back-diagonal the same.

H=

In MATLAB, the Hankel matrix based on two given vectors c and r can be constructed by H =hankel(c,r), and the first column of matrix H can be assigned to vector c, with the last row assigned to r. Thus using the properties of a Hankel matrix, the full Hankel matrix can then be established.

If there is only one vector c specified, the command H =hankel(c) can be used to construct an upper-triangular Hankel matrix.

Example 4.3 Establish the following Hankel matrices using MATLAB statements.

H1=

Solution In order to construct the above Hankel matrix, the c and r vectors should be assigned to c=[1,2,3], r=[3 4 5 6 7 8 9]. The Hankel matrices can then be established with the following statements.

>> c=[1 2 3]; r=[3 4 5 6 7 8 9];

H1=hankel(c,r), H2=hankel(c) Hilbert matrices and their inverses

Hilbert matrix is a special matrix whose (i, j)th element is defined as hi,j = 1/(i + j − 1). An n × n square Hilbert can be written as

H=





1 1/2 1/3 · · · 1/n

1/2 1/3 1/4 · · · 1/(n + 1) ... ... ... . .. ... 1/n 1/(n + 1) 1/(n + 2) · · · 1/(2n − 1)



. (4.4)

The syntax for generating the Hilbert matrix is A=hilb(n).

Large-sized Hilbert matrices are bad-conditioned matrices. Overflow will often occur during inverting such a matrix. Thus a direct inverse Hilbert matrix can be obtained with function B=invhilb(n).

Since Hilbert matrices are very close to singular matrices, one must be very careful in dealing with such matrices. It is suggested here that symbolic computation be used. If numerical methods are used, do validate the results.

Vandermonde matrices

For a given sequence c = {c1, c2, · · · , cn}, a Vandermonde matrix can be established such that the (i, j)th element is defined as vi,j = cni−j, i, j = 1, 2, · · · , n.

V =





cn1−1 cn1−2 · · · c1 1 cn2−1 cn2−2 · · · c2 1 ... ... . .. ... ...

cnn−1 cnn−2 · · · cn 1



. (4.5)

A Vandermonde matrix can be established with V =vander(c) function in MATLAB for a given vector c.

Example 4.4 Establish a Vandermonde matrix A =





1 1 1 1 1

16 8 4 2 1

81 27 9 3 1 256 64 16 4 1 625 125 25 5 1





.

Solution For such a matrix, one should select c=[1,2,3,4,5]. Thus with the following statements, the corresponding Vandermonde can be constructed.

>> c=[1, 2, 3, 4, 5]; V=vander(c) Companion matrices

Assume that there exists a monic polynomial (the highest order term with coefficient 1)

P (s) = sn+ a1sn−1+ a2sn−2+ · · · + an−1s + an (4.6)

a companion matrix can be established such that

Ac=







−a1 −a2 · · · −an−1 −an

1 0 · · · 0 0

0 1 · · · 0 0

... ... . .. ... ...

0 0 · · · 1 0







. (4.7)

A companion matrix can be established using B=compan(p), where p is a polynomial coefficient vector, and compan() function will automatically transform it into monic form.

Example 4.5 Consider a polynomial P (s) = 2s4+4s2+5s+6. Find its companion matrix.

Solution The characteristic polynomial can be entered first and then the com-panion matrix A can be established using the following statements

>> P=[2 0 4 5 6]; A=compan(P)

and the matrix A =



0 −2 −2.5 −3

1 0 0 0

0 1 0 0

0 0 1 0



 can be obtained.

4.1.2 Defining symbolic matrices

For a given numerical matrix A, one may transform it by B=sym(A) into a symbolic matrix. Thus all the numerical matrices can be transformed into symbolic matrices so as to achieve higher accuracy. In some cases, it is even possible to find analytical solutions.

For other special matrices, such as Vandermonde matrix, Hankel matrix and companion matrix, the functions described above do not support symbolic matrices. One may write special purpose functions as follows to set up such matrices from given vectors. These functions should be placed under the @sym directory.

For instance, the symbolic companion matrix can be established from the following function

1 function A=compan(c)

2 if min(size(c))>1, error(’Input argument must be a vector.’), end

3 n=length(c);

4 if n<=1, A=[];

5 elseif n==2, A=-c(2)/c(1);

6 else, c=c(:).’; A=sym(diag(ones(1,n-2),-1)); A(1,:)=-c(2:n)./ c(1);

7 end

Example 4.6 Establish a companion matrix from the following polynomial P (λ) = a1λ9+ a2λ8+ a3λ7+ · · · + a8λ2+ a9λ + a10.

Solution With the use of the extended compan() function given above, the required matrix can be established using the following statements

>> syms a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 A=compan([a1 a2 a3 a4 a5 a6 a7 a8 a9 a10]) the following matrix can be generated













−a2/a1−a3/a1−a4/a1−a5/a1−a6/a1 −a7/a1 −a8/a1−a9/a1−a10/a1

1 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0

0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 1 0













 .

Similarly, the Hankel matrix can be established using the new function

1 function H=hankel(c,r)

2 c=c(:); nc=length(c); if nargin==1, r=zeros(size(c)); end

3 r=r(:); nr=length(r); x=[c; r((2:nr)’)]; cidx=(1:nc)’;

4 ridx=0:(nr-1); H1=cidx(:,ones(nr,1))+ridx(ones(nc,1),:); H=x(H1);

and the symbolic Vandermonde matrix can also be set up from using

1 function A=vander(v)

2 n=length(v); v=v(:); A=sym(ones(n));

3 for j=n-1:-1:1, A(:,j)=v.*A(:,j+1); end