Beruflich Dokumente
Kultur Dokumente
4
0
6
.
(e) Compute v +w by typing v + w .
(f) Compute the vector formed by cubing each element of w (type w.^3). The . before
the operator causes each element of the vector to undergo the operation (see what
happens if you type w^2).
(g) Compute the vector formed by inverting each element of v (type 1./v).
(h) Compute the product v
T
w (type v*w).
(i) Compute the vector u where [u
j
] = [v
j
w
j
] (type u = v.*w)
(j) Sum all the elements in v (type sum(v)).
(k) Create a zero column vector x R
4
(type x = zeros(4,1)).
(l) Create a column vector x R
4
of all ones (type x = ones(4,1)).
(m) Assign the values 0, 0.1, 0.2, . . . , 1 to the vector x (type x = 0:0.1:1).
(n) Make x a column vector.
2
Matrix Operations: Transposes and Inverses.
(a) Enter the matrix A =
2 3 5
5 1 8
12 5 21
2 5 1
3 1 5
5 8 5
2 3 5
5 1 8
12 5 21
(b) Let B = A
T
(type B = A).
(c) Store a copy of B in the matrix H (type H = B).
(d) Reduce B to an echelon form by performing the following operations:
(1) R
1
R
2
R
1
(type B(1,:) = B(2,:) - B(1,:) ).
(2) R
2
R
2
3R
1
(3) R
3
R
3
5R
1
(4) R
2
R
2
/13
(5) R
3
R
3
28R
2
(e) Transform the result of the above calculations to reduced row echelon form by performing
the following additional operation:
(6) R
1
R
1
+ 4R
2
(f) Type help rref
(g) Use the command rref to nd the reduced row echelon form for B (set B back to its
original value by typing B = H before using the rref command).
(h) Enter the matrix C =
2 5 1
3 1 5
5 8 5
2 3 5
5 1 8
12 5 1
.
(r) Save the inverse of A in the matrix B.
(s) We are going to compute the adjoint of A using the M-le we have created. Type C =
find adjoint(A).
(t) Type D = C/det(A).
(u) Type D*A. Is the answer what you expected (within computer roundo error)?
(v) Type D-B. Is the answer what you expected (within computer roundo error)?
7
Cramers Rule.
(a) We are going to create a new M-le. If the MATLAB editor is still open, select the
New command from the File menu; otherwise, in the MATLAB command window,
type edit.
(b) Type the following commands into the MATLAB editor window:
function x = cramers rule(A,b)
% this function solves the system Ax = b
% when A is an nxn matrix invertible matrix
n = size(A,1);
m = size(A,2);
if n = m
fprintf(1,\n The matrix is not square!\n);
x = [];
else
detA = det(A);
if det(A) = 0
x = zeros(n,1);
for j = 1:n
if j= 1 & j= n
Ab = [A(:,1:j-1) b A(:,j+1:n)];
elseif j==1
Ab = [b A(:,2:n)];
elseif j==n
Ab = [A(:,1:n-1) b];
end
x(j) = det(Ab)/detA;
end % for j=1:n
else
fprintf(1, \n The matrix A has a zero determinant \n);
x = [ ];
end %if det(A) = 0
end %if n=m
(c) Save your work.
(d) The command fprintf is used to write information to the screen (or to a le). Provide
a line-by-line interpretation of the code cramers rule.m.
(e) Return to the MATLAB command window. Enter the matrix A =
1 4 5
4 2 5
3 3 1
1 3 2
3 0 5
2 5 4
and S =
0 2 1
2 0 4
1 4 0
T =
3 1 5
1 0 7
5 7 9
and B =
0 9 12
9 0 20
12 20 0
C =
1 0 0
0 1 0
0 0 1
3 12 0
1 5 1
9 1 8
and V =
1 0 1
2 1 0
4 0 6
10
Vector Spaces.
(a) Enter the matrix A =
4 0 4 3
4 1 1 1
2 3 5
5 1 8
12 5 21
min
= min
x=0
x
T
Ax
x
T
x
= min
x
T
x=1
x
T
Ax (1)
max
= max
x=0
x
T
Ax
x
T
x
= max
x
T
x=1
x
T
Ax (2)
(a) Use the MATLAB program editor to create the following m-le:
function [S, D] = evalues(A,num iter)
% function [S, D] = evalues(A,num iter)
%
% This function prints an estimate of the maximum
% and minimum eigenvalues for the square matrix A
% provided that A is symmetric; it also prints the
% true eigenvalues in the diagonal matrix D
% and corresponding eigenvectors in the matrix S
%
% The two input parameters are the matrix A and
% the number of iterations, num iter, to run
Asize = size(A);
if Asize(1) = Asize(2)
fprintf(1,\nError! The matrix is not square!\n);
14
S = [ ];
D = [ ];
else
if (isequal(A,A))
fprintf(1,\nWarning! The matrix is not symmetric!\n);
end
n = Asize(1);
x = zeros(n,num iter);
l = zeros(num iter,1);
min iter = 0;
max iter = 0;
lmin = inf;
lmax = -inf;
for i = 1:num iter
y = 2*rand(n,1)-1;
if y*y = 0
x(:,i) = y /(y*y)^0.5;
l(i) = x(:,i)*A*x(:,i);
if l(i) < lmin
lmin = l(i);
min iter = i;
end
if l(i) > lmax
lmax = l(i);
max iter = i;
end
end
end
fprintf(1,\n Smallest eigenvalue (estimated): %f\n, lmin);
fprintf(1,\n Largest eigenvalue (estimated): %f\n, lmax);
fprintf(1,\n The true eigenvalues and eigenvectors are\n);
[S, D] = eig(A);
end % Asize(1) = Asize(2)
% end function
(b) Provide an explanation for the code. (If necessary, use the MATLAB help to lookup
any functions with which you are not familiar.)
15
(c) Test the code on the following matrices
A =
8 1 1
1 8 1
1 1 8
B =
1 2 3
2 4 5
3 5 6
C =
3 0 0
4 6 2
16 15 5