Matrix Operations

Nel documento An Introduction to Programming and Numerical Methods in MATLAB (pagine 184-190)

Root Finding 4

Task 5.6 Given that the data

6.1.2 Matrix Operations

We shall now consider how matrix operations are performed within MATLAB, referring to the mathematical definitions given in the Appendix. We shall start with addition and subtraction, using simple examples:

Example 6.2 Consider the addition (C = A + B) and subtraction (D = A− B) of the matrices

We shall start by working through these by hand and then proceed to give the MATLAB code. Firstly the addition

C =

and now the subtraction D = The MATLAB code to achieve these operations is

>>A = [1 2; 3 4]; B = [4 3; 2 1];

We note that matrices need to be the same size to perform addition or subtraction since there needs to be a corresponding element in each matrix.

Example 6.3 Consider the expression A− 3BT where

A =

The MATLAB code is:



Note that although the matrix B is not the same size as A, its transpose BT is.

Before we proceed we add this word of caution within the context of MAT-LAB. As we have already seen MATLAB will adapt its variable structure to accommodate solutions. However, it will object to calculations that are impos-sible (that is, not well defined). For instance if we try to add a three-by-three matrix to a two-by-two one (both full of 1’s)2

>>A = ones(3); B = ones(2);

>>C = A+B

??? Error using ==> +

Matrix dimensions must agree.

This example demonstrates that MATLAB is not able to add a two-by-two matrix to a three-by-three one and it returns a sensible error message if we try to do so; in this case the dimension (or size) of the matrices are not the same.

The only operation of this kind which is possible within MATLAB involves

>>A = ones(1); B = ones(2);

>>C = A+B

2 We will use the command ones(n) which sets up an n-by-n matrix full of ones.

The other form of this command takes two arguments, ones(m,n), which unsur-prisingly gives a m-by-n matrix with all elements equal to one. This is similar to the command zeros(n) that we saw earlier. An associated command that gener-ates an n-by-n matrix with entries made up of random numbers between zero and one is rand(n). The precise structure of these commands can be found by using the help command.

C =

2 2

2 2

In this case although the variable A is a one-by-one matrix it is treated as a scalar. In this case the command is interpreted by MATLAB as

ci,j= λ + bi,j, i = 1,· · · , m and j = 1,· · · , n;

where C = λ1 + B, with 1 a matrix full of ones (which can be obtained in MATLAB using ones ). This operation is mathematically correct and it is also a well-defined operation within MATLAB. Similarly if a matrix is multiplied by a scalar then each element is multiplied by that scalar (that is ci,j = λai,j where C = λA and again the calculation is viable in mathematical terms).

Before proceeding to matrix multiplication within MATLAB we note that this idea of applying and performing an operation on every element extends to functions. We can demonstrate this with a simple example:

>> A = [pi/4 pi/2; pi pi/3]

Here we have set up the matrix3 A =

and it returns the matrix B as B =

3 Notice that the variable pi is predefined in MATLAB and returns the value of π = 3.1415926535897· · · .

We shall return to this method in due course, but we now turn our attention to matrix multiplication within MATLAB. The operation C = A*B is performed as one might expect and returns the “normal” result of matrix multiplication so that if A is n-by-m and B is m-by-p then C is n-by-p whose elements are defined on page 326. We need to remember that when taking the product of two matrices the number of columns of the left-hand matrix must match the number of rows in the right-hand matrix. If this is not so then MATLAB will give an error message: Inner matrix dimensions must agree. This is somewhere we need to be very careful and although most of the time our errors will be flagged by MATLAB, sometimes the calculations will proceed even though the results are not as we would expect: for instance

>> A = [1 2 3];

>> B = [4; 5; 6];

>> A*B ans =

32

>> B*A ans =

4 8 12

5 10 15

6 12 18

In each case MATLAB is prepared to make the calculation (as it should):

however the exchange of the two matrices yields very different results. Because MATLAB is willing to proceed with calculations with scalars or matrices as arguments, it may well be some time before this error is realised.

Example 6.4 Calculate 3A− B and AB where

A =

⎝ 1 5 6

0 2 3

−1 0 0

and B =

⎝ 0 −3 2

1 0 −2

1 0 −4

⎠ .

The code for this should be relatively self explanatory:

>> A = [1 5 6; 0 2 3; -1 0 0];

>> B = [0 -3 2; 1 0 -2; 1 0 -4];

>> disp(3*A-B)

>> disp(A*B)

Note that it is not necessary to use the command disp here.

Dot arithmetic can be readily extended to work with matrices. With ma-trices A and B the matrix C = A.*B is given by

ci,j= ai,jbi,j, i = 1,· · · , m and j = 1,· · · , n and similarly those of D = A./B by

di,j=ai,j

bi,j, i = 1,· · · , m and j = 1,· · · , n.

In addition to multiplication .* and division ./ we can also use dot arithmetic for exponentiation using .ˆas in

>> A = [1 2; 3 4];

>> B = [1 2; 3 4];

>> A.ˆB ans =

1 4

27 256

Of course in using dot arithmetic in MATLAB we must ensure the matrices are of the same size for the operation to be defined. We note either of the arguments can be scalars:

>> A = [1 2; 3 4];

>> B = A.ˆ2; % square all entries

>> C = 2.ˆA; % [2 4; 8 16]

We note these are effectively new binary operations and the addition of the dot should be thought of as changing the variables. It might help to think of them as separate operations: that is C = A*B performs the mathematical multiplication A× B whereas C = A.*B gives ci,j= ai,jbi,j. With practice you will come to appreciate the difference between these two operations.

It is not necessary to have a .+ or a .- command since using the definitions above these give the same results, since the elements of C = A + B are exactly ci,j= ai,j+ bi,j.

Example 6.5 Given the matrices A = I and B =

 1 2 3 4

 ,

compare the results of the MATLAB calculations A*B, A.*B, A/B and A./B.

For the multiplications we have the code:

>>A = eye(2); B = [1 2; 3 4];

>>C = A*B C =

1 2

3 4

>> C = A.*B C =

1 0

0 4

Here we have introduced the command eye(n) which gives an n-by-n identity matrix4. For the division operations we have

>> C = A/B C =

-2.0000 1.0000 1.5000 -0.5000

>> C = A./B C =

1.0000 0

0 0.2500

4 This could also have been obtained using the command diag.

Hopefully this example makes it clear that the straightforward asterisk opera-tion performs the mathematical operaopera-tion of multiplicaopera-tion, with multiplicaopera-tion by the identity matrix A leaving B unchanged, whereas multiplying using .*

performs the operation element-wise, so that C = A.∗ B =

We have previously skirted the issue of division of matrices, but now that we have introduced the element-wise operation this can be dealt with naturally and it works in the same way as multiplication. Above we have the results of the calculation

which is AB−1 and the other calculation is A./B =

We note when using the division operator / without the dot we are, in fact, constructing the inverse of the matrix B.

In order to calculate the transpose of a matrix we can use the command transpose or we can use an apostrophe, A’. If the elements of the matrix are complex numbers, then this operation constructs the complex conjugate of the transpose of A. If the elements of the matrix are real this is then equal to the transpose of the matrix. To obtain the transpose without conjugation, in the case of a complex matrix, we use the construction A.’.

Nel documento An Introduction to Programming and Numerical Methods in MATLAB (pagine 184-190)