Math 4330 Sec. 1, Matlab Assignment # 1, March 24, 2006 Name 1 ...
Math 4330 Sec. 1, Matlab Assignment # 1, March 24, 2006 Name 1 ...
Math 4330 Sec. 1, Matlab Assignment # 1, March 24, 2006 Name 1 ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Math</strong> <strong>4330</strong> <strong>Sec</strong>. 1, <strong>Matlab</strong> <strong>Assignment</strong> # 1,<br />
<strong>March</strong> <strong>24</strong>, <strong>2006</strong> <strong>Name</strong><br />
1. In matlab every object is a complex matrix in which real entries are displayed as real and integer<br />
as integer.<br />
2. There are several ways to enter a matrix into <strong>Matlab</strong>s workspace:<br />
(a) You can type in the elements<br />
A=[2 4 5;2 6 3;-1 6 2]<br />
builds a 3 × 3 matrix. The entries are typed in rows (elements separated by a space or a<br />
comma) with a semicolon used to declare the beginning of a new row.<br />
(b) You could also generate a matrix using the commands rand(n) or rand(n,m) to generate a<br />
random n by n or n by m matrix whose elements are normally distributed in 0 to 1.<br />
(c) The commands<br />
a=fix(10*rand(5))<br />
b=round(10*rand(5))<br />
generate 5 × 5 matrices with integer entries.<br />
3. If A = [a ij ] and B = [b ij ] are n × m matrices and C = [c ij ] is an m × p matrix, then we have the<br />
following matrix arithmetic operations:<br />
(a) A + B = [a ij + b ij ] and A − B = [a ij − b ij ]<br />
(b) A ∗C = D where D is a n ×p matrix with entries d ij =<br />
m∑<br />
a ik c kj . For matrix multiplication<br />
the number of columns of the first matrix must be the same as the number of rows of the<br />
second.<br />
(c) For a number α, the scalar product α A = [αa ij ]<br />
For example<br />
A=[1 2 5 ;-2 1 4]<br />
B=[4 2 0 ;4 2 -7]<br />
C=[3 6 ;-2 1 ;-4 2]<br />
A+B<br />
A*C<br />
3*A<br />
4. The usual rules of positive integer exponents applies for square matrices, A 2 = A∗A, A 3 = A∗A∗A,<br />
r<br />
{ }} {<br />
A r = A ∗ A ∗ · · · ∗ A.<br />
A=[3 1;5 2]<br />
A^2, A^3<br />
k=1
5. In general, division of matrices makes no sense. But for nonsingular square matrices is it is<br />
possible to make an interpretation of matrix division. If A is nonsingular, then it has an inverse,<br />
i.e., a matrix A −1 satisfying A ∗ A −1 = A −1 ∗ A = I where I is the identity matrix with ones on<br />
the main diagonal and zeros elsewhere. The identity matrix plays the same role as the number 1<br />
does for multiplication A ∗I = I ∗A = A. In <strong>Matlab</strong> the identity matrix is given by eye(n) where<br />
n is an integer. If A is nonsingular, then the inverse in matlab is given by inv(A) or A^(-1).<br />
In this case we can think of division as B ∗ A −1 just as we do with numbers bdiva = b ∗ a −1 for<br />
numbers with a ≠ 0.<br />
6. More generally, in this case, you can compute C=A^(-r).<br />
A=[3 1;5 2]<br />
A^(-1), inv(A)<br />
A*A^(-1)<br />
A=[3 1;5 2]<br />
C=A^(-2)<br />
C*A^2<br />
7. A matrix has an inverse if and only if its determinant is not zero. Recall the determinant for<br />
a square matrix is a number. You can find the definition of the number in most college algebra<br />
books. In <strong>Matlab</strong> it is easy to compute determinants using the command det.<br />
A=[3 1;5 2]<br />
d=det(A)<br />
8. Recall for a 2 × 2 matrix<br />
[ ]<br />
a11 a<br />
det 12<br />
= a<br />
a 21 a 11 a 22 − a 12 a 21 .<br />
22<br />
9. The problem of determining when a square matrix has an inverse is not easy to answer. The answer<br />
is that precisely the nonsingular matrices have inverses. There are several other characterizations<br />
of nonsingular given below. We will consider these properties with two examples<br />
A=[3 1;5 2]<br />
B=[3 1;6 2]<br />
A matrix A is nonsingular if and only if any one of the following hold:<br />
(a) det A ≠ 0<br />
det(A)<br />
det(B)<br />
(b) The row reduced echelon form of A is the identity<br />
rref(A)<br />
rref(B)
(c) A has an inverse<br />
inv(A)<br />
inv(B)<br />
(d) The only solution of the equation Ax = 0 is x = 0, i.e., the null space is the zero vector.<br />
The <strong>Matlab</strong> command “null” computes a basis for the null space. Note It does not list the<br />
zero vector.<br />
null(A)<br />
null(B)<br />
(e) The matrix A has full rank. If A is n × n then the rank of A is n.<br />
rank(A)<br />
rank(B)<br />
10. Since we can do powers, scalar products and sums of matrices we can consider matrix polynomials.<br />
Here is an example. Suppose A is an n ×n matrix, c is a (m+1) component row vector, then the<br />
matrix polynomial expression f = c(m + 1) ∗ A m + c(m) ∗ A m−1 + · · · + c(2) ∗ A + c(1) ∗ eye(n).<br />
11. Here is an example<br />
A=[3 1;5 2]<br />
c=[-3 2 -1 5]<br />
f=c(4)*A^3+c(3)*A^2+c(2)*A+c(1)*eye(2)<br />
g=A*(A*(c(4)*A+c(3)*eye(2))+c(2)*eye(2)) +c(1)*eye(2)<br />
%Horner’s method or synthetic division<br />
12. Recall that a system of n linear equations in n unknowns has the form<br />
⎧<br />
a 11 x 1 + a 12 x 2 + · · ·a 1n x n = b 1<br />
⎪⎨ a 21 x 1 + a 22 x 2 + · · ·a 2n x n = b 2<br />
which can also be written in matrix form as<br />
⎪⎩<br />
.<br />
a n1 x 1 + a n2 x 2 + · · ·a nn x n = b n<br />
Ax = b<br />
where<br />
⎡<br />
⎤ ⎡ ⎤ ⎡ ⎤<br />
a 11 a 12 · · · a 1n x 1 b 1<br />
a 21 a 22 · · · a 2n<br />
A = ⎢<br />
⎣<br />
.<br />
. . ..<br />
⎥ . ⎦ , x = x 2<br />
⎢ ⎥<br />
⎣ . ⎦ , b = b 2<br />
⎢ ⎥<br />
⎣ . ⎦ .<br />
a n1 a n2 · · · a nn x n b n<br />
13. A system of equations in this form can be solved in <strong>Matlab</strong> several different ways. One way is to<br />
use the “backslash” syntax x = A\b.
A=[3 1;5 2]<br />
b=[2;-9]<br />
x=A\b<br />
A*x-b<br />
% setup A<br />
% setup b<br />
% solve Ax=b<br />
% check the result<br />
14. This system can also be solved by writing the augmented matrix [A b] and computing the row<br />
reduced echleon form. The last column is the solution.<br />
A=[3 1;5 2]<br />
b=[2;-9]<br />
C=[A b]<br />
rref(C)<br />
% setup A<br />
% setup b<br />
% setup augmented matrix<br />
% compute row reduced echleon form<br />
15. We now show how difficult solving a system of equations accurately can be. We do this using the<br />
Hilbert matrix.<br />
For each positive integer n the n × n Hilbert matrix is<br />
⎡<br />
1<br />
1<br />
2<br />
H n =<br />
1<br />
3<br />
.<br />
⎢<br />
⎣1<br />
n<br />
1 1 1<br />
⎤<br />
· · ·<br />
2 3 n<br />
1 1 1<br />
· · ·<br />
3 4 (n + 1)<br />
1 1 1<br />
· · ·<br />
4 5 (n + 2)<br />
.<br />
. .. . .. . .. .<br />
⎥<br />
1 1 1 ⎦<br />
· · ·<br />
(n + 1) (n + 2) (2n − 1)<br />
Let us consider a problem where we start with an answer x. Then we set b = H n x and finally try<br />
to solve for x. Thus we set ⎡ ⎤<br />
1<br />
1<br />
x =<br />
1<br />
, b = H n x,<br />
⎢ ⎥<br />
⎣.<br />
⎦<br />
1<br />
We are also interested in the effect of slightly perturbing the values in b. We would guess that<br />
this would result in a very small change in the values of x - but this is not the case.<br />
⎡ ⎤<br />
ǫ 1<br />
ǫ 2<br />
b ǫ = b +<br />
ǫ 3<br />
, |ǫ j | ≤ 10 −8 .<br />
⎢ ⎥<br />
⎣ . ⎦<br />
ǫ n
On the left we solve the resulting system H n x = b. Notice that as N increases the errors get<br />
larger. On the right we have added very small perturbations (less than 10 −8 ) to the right hand<br />
side vector b. Notice what a dramatic effect in the column error which gives the deviation from<br />
the values of x with all ones.<br />
N error cond(H n )<br />
5.000000 0.000000 5 × 10 5<br />
6.000000 0.000000 1 × 10 7<br />
7.000000 0.000000 5 × 10 8<br />
8.000000 0.000000 2 × 10 10<br />
9.000000 0.000005 5 × 10 11<br />
10.000000 0.000259 2 × 10 13<br />
11.000000 0.015063 5 × 10 14<br />
12.000000 0.236949 2 × 10 16<br />
N error b cond(H n )<br />
5.000000 0.000580 5 × 10 5<br />
6.000000 0.0<strong>24</strong>407 1 × 10 7<br />
7.000000 0.197698 5 × 10 8<br />
8.000000 22.485693 2 × 10 10<br />
9.000000 868.368857 5 × 10 11<br />
10.000000 31600.401054 2 × 10 13<br />
11.000000 697526.22<strong>24</strong>39 5 × 10 14<br />
12.000000 4766960.006120 2 × 10 16<br />
Hilbert exact<br />
Hilbert perturbed<br />
In <strong>Matlab</strong> the above calculations could be carried out as follows:<br />
n=10;<br />
A=hilb(N);<br />
x=ones(N,1);<br />
b=A*x;<br />
x_approx=A\b;<br />
error =norm(x-x_approx,inf);<br />
c=cond(A);<br />
be=b+10^(-8)*rand(N,1);<br />
x_approx_b=A\be;<br />
error_b=norm(x-x_approx_b,inf);
1. Enter the matrices<br />
and carry out the following:<br />
A =<br />
ASSIGNMENT 1<br />
[ ] 2 4<br />
, B =<br />
1 3<br />
(a) Verify that (A + B) + C = A + (B + C)<br />
[ ] −2 1<br />
, C =<br />
0 4<br />
[ ] 3 1<br />
2 1<br />
(A + B) + C = A + (B + C) =<br />
(b) Verify that (AB)C = A(BC)<br />
(AB)C = A(BC) =<br />
(c) Decide whether AB is equal to BA<br />
AB = BA =<br />
(d) Find (A + B) 2 , (A 2 + 2AB + B 2 ) and (A 2 + AB + BA + B 2 )<br />
(A + B) 2 = (A 2 + 2AB + B 2 ) =<br />
(e) Find A 2 − B 2 , (A − B)(A + B)<br />
A 2 − B 2 = (A − B)(A + B) =<br />
2. Enter<br />
and do the following:<br />
A =<br />
[ ] 1/2 1/2<br />
, B =<br />
1/2 1/2<br />
[ 1/2<br />
] −1/2<br />
−1/2 1/2<br />
(a) Compute<br />
A 2 = , A 3 = , etc.<br />
Can you say what A n will be Explain why this is true.<br />
(b) Compute B 2 =<br />
, Can you explain why this is true. What does<br />
this tell you about matrix multiplication that is different from squaring numbers
(c) Find AB = and BA = . What do<br />
you learn from this that is not true for multiplication of numbers (hint: if a and b are real<br />
numbers and ab = 0, then a = 0 or b = 0).<br />
3. Find the inverse of the matrices (if they exist) and check that the result is correct by multiplying<br />
the matrix times its inverse.<br />
[ ]<br />
−1 1<br />
(a) A = , A<br />
1 0<br />
−1 = ,<br />
(b) B =<br />
[ ] 2 5<br />
, B<br />
1 3<br />
−1 = ,<br />
⎡ ⎤<br />
2 0 5<br />
(c) C = ⎣0 3 0⎦, C −1 = ,<br />
1 0 3<br />
⎡ ⎤<br />
−1 −3 −3<br />
(d) D = ⎣ 2 6 1 ⎦, D −1 = ,<br />
3 8 3<br />
[ ]<br />
1 −1<br />
4. Consider the A δ = depending on a parameter δ. The determinant of A<br />
1 −1 + δ<br />
δ is δ so when<br />
δ is small it is clear that the close to having a zero eigenvalue. This does not necessarily mean<br />
the matrix has a large condition number but it raises a red flag. Using Maple I compute the<br />
condition number using the command ConditionNumber(A_d); to get<br />
(∣ ) ∣∣∣ −1 + δ<br />
max<br />
δ ∣ + (|δ|)−1 , 2 (|δ|) −1 max (2, 1 + |−1 + δ|)<br />
Notice here is an example where Maple is preferably since I want the answer in terms of δ.<br />
[<br />
1<br />
Set x = and set b<br />
1]<br />
δ = A d x. We consider δ = 10 −1 and δ = 10 −5 .<br />
(a) For each δ find the eigenvalues and condition numbers of A δ .<br />
(b) For each δ solve A δ x δ = b.<br />
(c) Consider the error obtained between x and x δ .<br />
Now let us perturb the right hand side a little bit.<br />
,<br />
,<br />
,
(a) For each δ (above) set b_e= 10^(-4)*rand(2,1); then solve A δ x δ,ǫ = b e .<br />
(b) Consider the error obtained between x and x δǫ .<br />
,<br />
(c) Repeat parts (a) and (b) above withb_e= 10^(-5)*rand(2,1);<br />
,<br />
5. Given the matrices<br />
solve the matrix equations:<br />
(a) AX + B = C,<br />
A =<br />
[ ]<br />
5 3<br />
, B =<br />
3 2<br />
[ ]<br />
6 2<br />
, C =<br />
2 4<br />
[ ]<br />
4 −2<br />
,<br />
−6 3<br />
(b) AX + B = X,<br />
(c) XA + B = C,<br />
(d) XA + C = X.<br />
6. Let A = round(10 ∗ rand(6)). Change the sixth column as follows. Set<br />
B=A’<br />
% (take the transpose of $A$)<br />
now type<br />
A(:,6)=-sum(B(1:5,:))’<br />
(a) Explain what this last command does
(b) Compute det(A)<br />
(c) Compute rref(A)<br />
(d) Compute rank(A)<br />
7. Explain why A is singular<br />
8. Let A = round(10 ∗ rand(5)) and B = round(10 ∗ rand(5)). Compare the following pairs of<br />
numbers.<br />
(a) det(A) and det(A ′ ).<br />
(b) det(A + B) and det(A) + det(B).<br />
(c) det(AB) and det(A) det(B).<br />
(d) det(A −1 ) and 1/ det(A).<br />
9. Look at help on magic and then compute det(magic(n)) for n = 3, 4, 5, · · · , 10. What seems to<br />
be happening Check n = <strong>24</strong> and 25 to see if the patterns still holds. By pattern I mean try to<br />
describe in words what seems to be happening to these determinants.