An Introduction to Programming and Numerical Methods in MATLAB
468 pág.

An Introduction to Programming and Numerical Methods in MATLAB


DisciplinaMatlab498 materiais2.121 seguidores
Pré-visualização50 páginas
the \ufb01rst row of A to be equal to the vector r
and to print out the resulting matrix A which, as we see, is precisely the matrix
we required. We could have entered the matrix in many di\ufb00erent ways. One
way would be to simply type the matrix in element by element:
>> A = [1 2 3 4;
2 0 0 0;
3 0 0 0;
4 0 0 0];
This is \ufb01ne for small matrices but not practical for when we need to set up
larger matrices. Another variant which can be used to set up the matrix is
>> A = [1 2 3 4; 2 zeros(1,3); 3 zeros(1,3); 4 zeros(1,3)];
Here the command zeros(1,3) sets up a row vector (one-by-three) full of zeros.
This example serves to emphasise there is no unique way to go about setting up
a particular matrix; some ways are more elegant (and sometimes less readable)
than others.
174 6. Matrices
6.1.2 Matrix Operations
We shall now consider how matrix operations are performed within MATLAB,
referring to the mathematical de\ufb01nitions 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 \u2212 B) of the matrices
A =
(
1 2
3 4
)
and B =
(
4 3
2 1
)
.
We shall start by working through these by hand and then proceed to give the
MATLAB code. Firstly the addition
C =
(
1 2
3 4
)
+
(
4 3
2 1
)
,
=
(
1 + 4 2 + 3
3 + 2 4 + 1
)
=
(
5 5
5 5
)
;
and now the subtraction
D =
(
1 2
3 4
)
\u2212
(
4 3
2 1
)
,
=
(
1 \u2212 4 2 \u2212 3
3 \u2212 2 4 \u2212 1
)
=
( \u22123 \u22121
1 3
)
.
The MATLAB code to achieve these operations is
>>A = [1 2; 3 4]; B = [4 3; 2 1];
>>C = A+B
C =
5 5
5 5
>>D = A-B
D =
-3 -1
1 3
6.1 Introduction 175
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 \u2212 3BT where
A =
(
3 8 \u22121
5 2 0
)
and B =
\u239b
\u239d \u22123 22 2
\u22121 3
\u239e
\u23a0 .
The MATLAB code is:
\ufffd
\ufffd
\ufffd
A = [3 8 -1; 5 2 0];
B = [-3 2; 2 2; -1 3];
C = A-3*transpose(B);
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 de\ufb01ned). For instance if we try to add a three-by-three
matrix to a two-by-two one (both full of 1\u2019s)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.
176 6. Matrices
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 = \u3bb + bi,j , i = 1, · · · ,m
and j = 1, · · · , n;
where C = \u3bb1 + 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-de\ufb01ned operation within MATLAB. Similarly if a matrix is multiplied
by a scalar then each element is multiplied by that scalar (that is ci,j = \u3bbai,j
where C = \u3bbA 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]
A =
0.7854 1.5708
3.1416 1.0472
>> B = sin(A)
B =
0.7071 1.0000
0.0000 0.8660
Here we have set up the matrix3
A =
(
\u3c0
4
\u3c0
2
\u3c0 \u3c03
)
and it returns the matrix B as
B =
(
sin \u3c04 sin
\u3c0
2
sin\u3c0 sin \u3c03
)
=
(
1\u221a
2
1
0
\u221a
3
2
)
.
3 Notice that the variable pi is prede\ufb01ned in MATLAB and returns the value of
\u3c0 = 3.1415926535897 · · · .
6.1 Introduction 177
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 \u201cnormal\u201d 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
de\ufb01ned 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 \ufb02agged 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 di\ufb00erent 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 \u2212 B and AB where
A =
\u239b
\u239d 1 5 60 2 3
\u22121 0 0
\u239e
\u23a0 and B =
\u239b
\u239d 0 \u22123 21 0 \u22122
1 0 \u22124
\u239e
\u23a0 .
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];
178 6. Matrices
>> 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 .\u2c6as in
>> A = [1 2; 3 4];
>> B = [1 2; 3 4];
>> A.\u2c6B
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 de\ufb01ned. We note either of the
arguments can be scalars:
>> A = [1 2; 3 4];
>> B = A.\u2c62; % square all entries
>> C = 2.\u2c6A; % [2 4; 8 16]
We note these are e\ufb00ectively 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 di\ufb00erence between these two operations.
It is not necessary to have a .+ or a .- command since using the de\ufb01nitions
above these give the same results, since the elements of C = A+B are exactly
ci,j = ai,j + bi,j .
6.1 Introduction 179
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