1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ]...
-
Upload
maximillian-gilbert -
Category
Documents
-
view
216 -
download
0
Transcript of 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ]...
![Page 1: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/1.jpg)
1
4.1 BASICS
![Page 2: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/2.jpg)
2
Setting Up Matrices Assignment a matrix
A = [ {row 1}; {row 2}; ……; {row m} ]
Ex: To set up a 4x4 matrix A ,like
A = [1 1 1 1;1 2 3 4;1 3 6 10;1 4 10 20]
It’s also legal to make input ‘look like’ output.A = [1 1 1 1
1 2 3 41 3 6 101 4 10 20 ]
201041
10631
4321
1111
![Page 3: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/3.jpg)
3
Simple Output If you enter A = [1 2;3 4] without a
semicolon ,then the system responds with
If you already assignment A wit a semicolon, but now you want to display it .You could only type A to display.
43
21A
![Page 4: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/4.jpg)
4
Types and Dimension Complex syntax
c = complex(a,b), it means c = a + bi
Dimension is handled automatically in MATLAB.Suppose you set B = [1 2 3; 4 5 6 ], and then set B = [1 0; 0 1].The system ‘knows enough’ to recognize that B has changed from 2-by-3 matrix to a 2-by-2 matrix.
![Page 5: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/5.jpg)
5
Subscripts The subscripts in MATLAB must be positive. If you type A(1.2 , 3.5) = 10, you will get a error
message . ?? Subscript indices must either be real positive integers or logicals
![Page 6: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/6.jpg)
6
Vectors and Scalars Vector and scalar are ‘special matrices’.
The commands, x = [ 1 ; 2 ; 3 ] ; or x = [1 2 3]’ ; establish x as a column vector while
Assignment an 1-by-1 matrix with c = [3], and it equals to c = 3 .
3
2
1
x
![Page 7: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/7.jpg)
7
Size and LengthIf we declare A = [ 1 2 ; 3 4 ; 5 6 ], v = [7 8 9 10]
Size & Length syntaxsize(A)
ans = [3 2]length(v)
ans = 4
![Page 8: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/8.jpg)
8
Continuation Sometimes it is not possible to put an entire
MATLAB instruction on a single line. We can break the instruction in a ‘natural’ place.
There are some ways to express a instruction, and they are the same.
A = [1 2 3; 4 5 6 ;....... 7 8 9]
A = [1 2 3; 4 5 6 ; 7 8 9]
A = [1 2 3; 4 5 6 ; 7 8 9]
![Page 9: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/9.jpg)
9
Variable Names Variable names in MATLAB must consist of
nineteen or fewer characters.
The name must begin with a letter.Any letter, number, underscore may follow, e.g., sym_part1.
Upper and lower cases are distinguished in MATLAB.
![Page 10: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/10.jpg)
10
Addition, Multiplication, Exponentiation
If A and B are matrices the C = A+B sets C to be the sum while C = A*B is the product.
To raise a square A to power r enterC = A^r
C = A^(-1) assigns the inverse of A to C.
f = c1Ab + c2A2b + c3A3b
f = A*(A*(c(3)*A*b+c(2)*b)c(1)*b)
f = ( c(3)*A^3+ c(2)*A^2+c(1)*A )*b
![Page 11: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/11.jpg)
11
Transpose The conjugate transpose of a matrix can be
obtained using a single quote:Set B = At
B = A’
![Page 12: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/12.jpg)
12
The “Colon Method” for Setting Up Vectors
Colons can be used to prescribe vectors whose components differ by a fixed increment.
v = 1:3 v = [1 2 3] v = 3:-1:0 v = [3 2 1 0] v = 1:2:10 v = [1 3 5 7 9]
![Page 13: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/13.jpg)
13
Logarithmically Spaced Vectors
LOGSPACElogspace(a,b,n) generates n points between decades 10^a and 10^b.
If w = logspace(d1,d2,n), thenw(i) = 10 ^ [ d1 +(i-1)(d2-d1)/(n-1) ]
w = logspace(0,3,4) w = [1 10 100 1000]
![Page 14: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/14.jpg)
14
Generation of Special Matrices
MATLAB has a number of build-in functions that can be used to generate frequently occurring matrices:
A = eye(m , n ) A m-by-n, ones on diagonal, zeros
elsewhere.A = zeros(m , n )
A m-by-n, zeros everywhere.A = ones(m , n )
A m-by-n, ones everywhere.
![Page 15: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/15.jpg)
15
Random Matrices It’s possible to generate matrices and vectors
of random numbers:
A = rand(m , n ) A m-by-n, random element between 0
and 1.
![Page 16: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/16.jpg)
16
Complex Matrices If A and B are matrices with the same
dimension and i2=-1, then the complex matrix C = A+iB can be generated as follows:
C = A + sqrt(-1) * B ; or C = A + i * B ;
![Page 17: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/17.jpg)
17
Pointwise Operations Element operations between two matrices of
the same size are also possible:
C = A.*B cij = aij * bij
C = A.\B cij = bij / aij
C = A./B cij = aij / bij
C = A.^B cij = aijbij
C = A.’ cij = aji
If we have a=[1 2], then type a.*[3 4]We get the answer = [1*3 2*4 ]= [3 8].
![Page 18: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/18.jpg)
18
More On Input/Output I The format for printed output can be controlled
using the following commands:format short 5-digit fixed point
styleformat short e 5-digit floating point
styleformat long 15-digit fixed point
styleformat long e 15-digit floating
point styleformat hex hexadecimal
![Page 19: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/19.jpg)
19
More On Input/Output II format hex hexadecimal
To represent a number into double precise floating point(with hexadecimal form).
Ex. To show 1 in format ‘ hex ’ in matlab1 = + 1.0 * 2^0
sign exponent+1023 mantissa
0011 1111 1111 0000 0000 0000 ..... 0000 3 f f 0 0 0 ..... 0
sign(1 bit)
exponent(11 bit)
mantissa(52 bit)
0 01111111111 000000000.....00+1023
![Page 20: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/20.jpg)
20
Machine Precision At the start of MATLAB session, the variable eps
contains the effective machine precision,i.e.,the smallest floating point number such that if x = 1 + eps then x>1 .
eps = 2.220446049250313e-016
![Page 21: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/21.jpg)
21
Flops This is an obsolete function.
It can estimate the count of the program.
For complex operands, addition and subtraction count as two flops while multiplications and divisions each count as six flops.
![Page 22: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/22.jpg)
22
4.2 LOOPS AND CONDITIONALS
![Page 23: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/23.jpg)
23
For-Loops Syntax
for {var} = {row vector of counter values}{statements}
end
Ex: for i = 1:3 x(i) = i;end
is equivalent to x = [1;2;3];
![Page 24: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/24.jpg)
24
Relations Relation operators
== equals
~= not equals< less than<= less than or equal to> greater than>= greater than or equal to
Ex: A=[1 2;3 4]; B=[1 2;2 4];T = A==BThen, the output is T = 1 1
0 1
![Page 25: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/25.jpg)
25
‘and’, ’or’, ’not’ And(&), or(|), not(~)
The operations are possible between 0-1 matrices of equal dimension.
If T1 = [1 1 ; 0 1] ,T2 = [1 0 ; 0 0]T=T1&T2, T= 1 0
0 0T=T1|T2, T= 1 1
0 1T=~T1, T= 0 0
1 0
![Page 26: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/26.jpg)
26
‘if’ Constructions Syntax
if {relation} {statements}end
Ex:Set a upper triangular matrix that has 1’s on the diagonal and –1’s above the diagonal.for i = 1 : n for j = 1 : n
if i < j A(i,j) = -1; elseif i > j A(i,j) = 0; else A(i,j) = 1; end
end end
![Page 27: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/27.jpg)
27
The ‘any’ and ‘all’ Functions Any
If any component of the vector is nonzero, it returns a “1”.
AllIf all the entries are nonzero, it returns a “1”.
a = [ 0 1 1; 0 0 1; 0 0 1]any(a) ans =[0 1 1]all(a) ans =[0 0 1]
![Page 28: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/28.jpg)
28
While-Loops Syntax
while {relation} {statements}
end
Print a sequence{Ai} of random 2-by-2 with the property that Ai+1>Ai:
A = zeros(2)
B = rand(2,2)
while(B>A)
A=B
B=rand(2,2)
end
![Page 29: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/29.jpg)
29
The ‘break’ Command Break
It can be used to terminate a loop.
Print all Fibonnaci number less than 1000:fib(1) = 1
fib(2) = 1
for j = 3:1000
z = fib(j-1) + fib(j-2)
if z >= 1000
break
end
fib(j) = z
end
![Page 30: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/30.jpg)
30
4.3 WORKING WITH SUBMATRICES
![Page 31: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/31.jpg)
31
Setting Up Block Matrices We can set up the block matrix as follows:
A=[A11 A12 ; A21 A22 ; A31 A32 ]
If we want to make a 6-by-6 matrix with random 2-by-2 block diagonal, like
A = rand(2,2)
for i = 2:3
[m,m] = size(A);
A = [ A zeros(m,2) ; zeros(2,m) rand(2,2) ]
end
lk0000
ji0000
00hg00
00fe00
0000dc
0000ba
![Page 32: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/32.jpg)
32
The Empty Matrix The empty matrix [] is often useful for
initialization of certain matrix computations.B=[] B is empty
To set up a column-reversed version of above example:
B = []
for j = 6:-1:1
B = [ B A(:,j) ];
end
0000kl
0000ij
00gh00
00ef00
cd0000
ab0000
![Page 33: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/33.jpg)
33
Designating Submatrices If A is m-by-n matrix and i,j,p,q are integers.
Then, B= A( i:j , p:q ) B=
B = A ( : , p:q ), the range ( : ) means from 1:n, if the dimension of matrix is n.
jqjp
iqip
aa
aa
![Page 34: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/34.jpg)
34
Assignment to Submatrix
If A = , we can assign the column
3 to [3 3 3]’ by using A(:,3)=3 or A ([7 8 9])=[3 3 3].
Then A =
021
021
021
321
321
321
![Page 35: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/35.jpg)
35
Kronecker Product The Kronecker product of two matrices A and B can
be calculated using the build-in function kron: C = kron(A,B) C = (aijB)
C = []; [m,n] = size(A); for j = 1:n ccol = []; for i = 1:m ccol = [ ccol ; A(i,j)*B ]; end
C = [C ccol] end If C = kron( [ 1 2 ] , [ 1 2 ; 3 4 ] ) then C = 1 2 2 4 3 4 6 8
![Page 36: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/36.jpg)
36
Turning Matrices into Vectors and Vice Versa
If A is a m-by-n matrix and we want to set v is an mn-by-1 vector.
v = A(:)
Set a transpose of a 2-by-2 matrix A = [ 1 2 ; 3 4 ]x = A(:);
x( [2 3] ) = x( [3 2] );
A(:) = x;
![Page 37: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/37.jpg)
37
4.4 BUILD-IN FUNCTIONS
![Page 38: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/38.jpg)
38
‘abs’, ‘real’, ‘imag’, ‘conj’ B = abs(A) B = ( |aij| )
B = real(A) B = ( real(aij) )
B = imag(A) B = ( imag(aij) )
B = conj(A) B = real(A)-i*imag(A), i2=-1
If A=[1-2i 2-i;3-4i 4-3i]; conj(A);Then, ans = 1+2i 2+i
3+4i 4+3i MATLAB’s build-in functions are in lower case,
unless user define.Otherwise, it’s will show ‘Capitalized internal function XXXX; Caps Lock may be on.’
![Page 39: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/39.jpg)
39
Norms t = norm(A) t = || A ||2
t = norm(A,1) t = || A ||1
t = norm(A,’inf’) t = || A ||t = norm(A,'fro') t = || A ||F
If A = [1 2 3 4]norm(A)
ans = 5.47722557505166sqrt(1+4+9+16)ans = 5.47722557505166
![Page 40: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/40.jpg)
40
Largest and Smallest Entries If v = max(A) v = [ max(A(:,1)), … , max(A(:,n)) ]
[v,i] = max(A)v = [ max(A(:,1)), … , max(A(:,n)) ]i = [the row of the max value
in]
A =[ 1 2 86 9 4
7 5 3];and [v,i]=max(A);
v= 7 9 8i = 3 2 1
![Page 41: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/41.jpg)
41
Sums and Products t = sum(A) t =
t = prod(A) t =
A =[ 1 2 9 6 9 4
7 5 3]sum(A)ans =14 16 15prod(A)ans =42 90 96
) )(:,, .... ),(:, ( nA1A
))(:, , .... ),(:, ( nA1A
![Page 42: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/42.jpg)
42
‘sort’ and ’find’ I Sorts each column of A in ascending order by using
sort(A)
[v,i] = sort(A), returns in sorted v and an integer vector i which indicate the permutation of new from old matrix.
A =[ 3 5 92 6 81 4 7]
[B,i]=sort(A); B =[1 4 7 2 5 8 3 6 9]
i =[ 3 3 3 2 1 2 1 2 1]
![Page 43: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/43.jpg)
43
‘sort’ and ’find’ II The find function can be used to locate nonzero
entries.
A =[1 0 2 3 0]find(A)
ans =[1 3 4]find(A == 0)
ans =[2 5]
![Page 44: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/44.jpg)
44
Rounding, Remainders, and Signs I
There are several conversion-to-integer routines:
round(x) round x to nearest integerfix(x) round x to zerofloor(x) round x to -ceil(x) round x to +
A = [ 1.1 -2.2 -0.9 3.8 ]round(A) ans=[1 - 2 -1 4]fix(A) ans=[1 -2 0 3]floor(A) ans=[1 -3 -1 3]ceil(A) ans=[2 -2 0 4]
![Page 45: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/45.jpg)
45
Rounding, Remainders, and Signs II
t = sign(x) t =
t = rem(x,y) t = x-y*fix(x/y)If y = 0, rem(x,0) is NaN. And x and y must be the same dimension.
rem equals mod(x,y) if x and y are the same sign.Otherwise , mod(x,y) = rem(x,y)+ya = mod(-7,4)a = 1a = rem(-7,4) a = -3
0 if x 1
0 if x0
0 if x1
![Page 46: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/46.jpg)
46
Extracting Triangular and Diagonal Parts I
B = tril (A) bij=aij if i j, zero otherwise.B = triu (A) bij=aij if i j, zero otherwise.B = tril (A,k) bij=aij if i+k j, zero otherwise.
B = triu (A,k) bij=aij if i+k j, zero otherwise.
diag(v) puts v on the main diagonal.v=diag(A) return a vector v with A’s diagonal.
![Page 47: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/47.jpg)
47
Extracting Triangular and Diagonal Parts II
T = toeplitz(c,r) is equivalent to
T = zeros(n);
for k = 1:n
j = n-k;
if k<n
T = T + diag( r(k+1)*ones(j,1) , k);
end
T = T + diag( c(k)*ones(j+1,1) , -k+1);
end
![Page 48: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/48.jpg)
48
Extracting Triangular and Diagonal Parts III
Take c=[6 7 8 9 10];r=[1 2 3 4 5];n=5;
into above two function. We can show
T =[ 6 2 3 4 5 7 6 2 3 4 8 7 6 2 3 9 8 7 6 2 10 9 8 7 6]
![Page 49: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/49.jpg)
49
‘rank’ If A is a m-by-n matrix, rank(A)= min{m,n}.
It Is also possible to base the rank decision on an arbitrary tolerance.If tol is a nonnegative scalar then
r = rank(A , tol)
A = [ 1 2 ; 3 4 ; 5 6 ]rank(A)=min{3,2}=2
A = [1 0.1 ; 3 0.2 ; 5 0.3 ]rank(A,1)=1
![Page 50: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/50.jpg)
50
Condition cond(A) can be computed the 2-norm
condition (the ratio of the largest
singular value of X to the smallest) of a matrix A.
min/ 1
![Page 51: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/51.jpg)
51
Determinant The determinant of a square matrix A can be
computed as follows:det(A)
A =[1 2;3 4];det(A)=1*4-2*3
=-2
![Page 52: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/52.jpg)
52
Elementary Functions Trigonometric
sin() cos() tan()
Inverse trigonometricasin() acos() atan()
Exponentialexp() log() log10()
Hyperbolicsinh() cosh() tanh()
![Page 53: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/53.jpg)
53
Functions of Matrices F = expm(A) F = eA
F = logm(A) A = eF
F = sqrtm(A) F2 = A
![Page 54: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/54.jpg)
54
‘roots’ and ‘poly’ If A is an n-by-n matrix then
c = poly(A) det( ) =
The roots are returned in a column vector by root(A), if we want to get the root.
If A=[2 -1;1 4];r=poly(A); r=[1 –6 9]a=roots(r); a=[3 -3]
AI n
1
1n
2n1n cccc
![Page 55: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/55.jpg)
55
Fourier Transforms y = fft(x) y = Fourier transform of x
y = ifft(x) y = inverse Fourier transform of x
B = fft2(x) B = 2DFourier transform of xB = ifft2(x) B = inverse 2DFourier transform of x
![Page 56: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/56.jpg)
56
4.5 FUNCTIONS
![Page 57: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/57.jpg)
57
An Example I The first line in the function is of the form
function {output variable} = {function name} ({arguments})
Comments begin with the ’%’ sign.All functions often begins with ‘how-to-use’ comments.They will be displayed, as we input the help with the function name.
![Page 58: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/58.jpg)
58
An Example II Write a function return a unit 2-norm vector
in the direction of Ax
function y = proAx(A,x)%% Compute K = [ v , Av , A^2 v , . . . , A^(p-1) v ] D,% where A is n-by-n, v is n-by-1, and D is diagonal% chosen so that the column of K have unit 2-norm.%y = A*x;c = norm(y);if c ~= 0
y = y/c;else
y = [ 1 ; zero( length( x-1,1 ) ) ];disp('Matrix-vector product is zero. First
column..of I returned.')end
![Page 59: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/59.jpg)
59
Functions can call other Functions
Compute K = [v,Av,A^2v,…,A^(p-1)v]D,where A is n-by-n,v is n-by-1,and D is diagonal.
function K = kry(A,v,p) [ m,n ] = size(A); nv = length(v) if m ~= n | n ~= nv disp('Dimension do not agree.') else K = v/norm(v) for j = 2:p-1 K = [ K prodAx(A,K(:,j-1) )]; end end return
![Page 60: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/60.jpg)
60
Multiple Input and Output Arguments I
Computes nonnegative matrices P and N, where A(i,j) > EPS , implies P(i,j) = A(i.j) , N(i,j) = 0 A(i,j) < EPS , implies P(i,j) = 0 , N(i,j) = -A(i.j)
| A(i,j) | <= EPS , implies P(i,j) = N(i,j) = 0
function [P N] = pn(A)E = EPS*ones(A);
P = A.*( A > E );
N = -A.*( -A > E );
end
![Page 61: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/61.jpg)
61
Multiple Input and Output Arguments II
If one input argument(decided by nargin), tol = EPS.If one output argument (decided by nargout), P only is returned.
function [P N] = pn(A,tol)
if nargin == 1
tol = EPS;
end
E = tol*ones(A);
P = A.*( A > E );
if nargin == 2
N = -A.*( -A > E );
End
Statement Tolerance
Output
P = pn(A)[P,N]=pn(A)P = pn(A,tol)[P,N]=pn(A,tol)
epsepstoltol
Just PP and
NJust PP and
N
![Page 62: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/62.jpg)
62
Passing Function Names It’s possible to pass the function name of another
function,but it requires some text processing with the eval function.
function F = afun(A,f)
[m,n] = size(A);
for j = 1:m
for i = 1:n
F(i,j) = eval([ f ,'(A(i,j))' ]);
end
end F(i,j) = eval([ f ,‘A(i,j)’ ]);
assign f(A(i,j)) to F(i,j). Ex: afun(A,'cos');
[0.54030230586814 -0.41614683654714 -0.98999249660045 -0.65364362086361]
![Page 63: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/63.jpg)
63
Global Variables The values in all global variables can be
accessed whenever inside a function.
Like the common in Fortran, global should be used sparingly.
![Page 64: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/64.jpg)
64
Recursion I Compute the 1-norm of an n-vector x function normL1 = f(x)
n = length(x);
if n == 1
normL1 = abs(x);
else
normL1 = f( x(1:n-1) ) + abs(x(n));
end
Ex: f([1 2 -3 4]) = 10
![Page 65: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/65.jpg)
65
Recursion II Compute A*B with recursion method(Strassen
matrix), where A,B are square matrices.function C = strass(A,B,nmin)
[ n n ] = size(A); if n <= nmin % n small, get C conventionally C = A*B;
else m = n/2; u = 1:m; v = m+1:n; P1 = strass(A(u,u)+A(v,v), B(u,u)+B(v,v), nmin);
P2 = strass(A(v,u)+A(v,v), B(u,u), nmin); P3 = strass(A(u,u), B(u,v)-B(v,v), nmin);
P4 = strass(A(v,v), B(v,u)-B(u,u), nmin); P5 = strass(A(u,u)+A(u,v), B(v,v), nmin); P6 = strass(A(v,u)-A(u,u), B(u,u)+B(u,v), nmin); P7 = strass(A(u,v)-A(v,v), B(v,u)+B(v,v), nmin); C = [ P1+P4-P5+P7 P3+P5 ; P2+P4 P1+P3-P2+P5 ];end
![Page 66: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/66.jpg)
66
4.6 FACTORIZATION
![Page 67: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/67.jpg)
67
Solving Linear Systems It can be computed the solution to the multiple
tight-hand side linear system AX=B.X = A \ B
A less preferable way to solve AX = B isX = inv(A) * B
![Page 68: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/68.jpg)
68
The LU Factorization The factorization A =LU, where U is upper
triangular and L is a row-permuted unit lower triangular matrix,can be computed as follows:
[ L , U ] = lu(A) Another less efficient method for solving AX =
b.[ L , U ] = lu(A)
y = L \ b;
x = U \ y;
![Page 69: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/69.jpg)
69
The Cholesky Factorization It can be computed an upper triangular R
such that A =RHR, where A is an m-by-n Hermitian, positive definite matrix.
R = chol(A)
There is no build-in function for computing the factorization A = LDLH, where L is unit lower triangular and D is diagonal. How to do it?L = chol(A);d = diag(L);L=L';for k =1:n
L(:,k) = L(:,k)/d(k);endD = diag(d.*d);
![Page 70: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/70.jpg)
70
QR Factorization Compute unitary Q(m-by-m) and upper
triangular R(m-by-n) such that A =QR.[Q , R] = qr(A)
Compute the minimizer of ||Ax - b||2.
[Q , R] = qr(A);
[m , n] = size(A);
b = Q'*b;
x = R(1:n,1:n)\b(1:n);
![Page 71: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/71.jpg)
71
Householder and Givens Transformations
Compute a Householder matrix P such that Px is zero below the first component:
[P , R] = qr(x)
![Page 72: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/72.jpg)
72
QR Factorization with Column Pivoting
Syntax[Q,R,P] = qr(A)
where Q is m-by-m unitary, p is n-by-n permutation, and upper triangular m-by-n matrix R.So, QTAP = R =
Compute the solution of Ax = b.[Q,R,P] = qr(A);
[m,n] = size(A);
y= R(1:m , 1:m) \Q'*b;
x= P* [y; zeros(n-m,1)];
00
RR 1211
![Page 73: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/73.jpg)
73
Range and Null Space Compute an orthonormal basis for the range
space.Q = orth(A)
or[m,n]=size(A);[Q,R]=qr(A);Q=Q(:, 1:n);
A null space of a matrix AQ=null(A)
![Page 74: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/74.jpg)
74
The Singular Value Decomposition I
If A is a m-by-n matrix ,we can compute unitary U(m-by-m), unitary V (n-by-n), and diagonal S(m-by-n):
[ U , S , V ] = svd(A)so, UHAV = S.
If m n then the ‘compact’ SVD in which U is m-by-n and S is n-by-n, use a two-argument version of svd:
[ U , S , V ] = svd(A, 0)
![Page 75: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/75.jpg)
75
The Singular Value Decomposition II
A = [1 2 ;4 5;3 4];[ U , S , V ] = svd(A,0)[ U1 , S1 , V1 ] = svd(A,0)
U = [-0.26118 0.92755 -0.26726 -0.76072 -0.36822 -0.53452 -0.59420 0.06370 0.80178]S = [8.41440 0 0 0.444672 0 0 ]V = [-0.60452 -0.796587 -0.79658 0.60452]
U1 = [-0.26118 0.92755
-0.76072 -0.36822
-0.59420 0.06370]
S1 = [8.41440 0
0 0.444672]
V1 =[-0.60452 -0.796587
-0.79658 0.60452]
![Page 76: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/76.jpg)
76
Pseudoinverse and Least Squares
If A is initialized then the pseudoinverse X can be found using pinv:
X = pinv(A)Then, multiple right-hand side least squares problem can be solved with either of the commands
x=A\B;or
X = pinv(A)*B;
![Page 77: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/77.jpg)
77
The Hessenberg Decomposition
If A is a m-by-n matrix ,we can compute unitary Q(n-by-n), and upper Hessenberg H such that QHAQ = H.
H = hess(A)
A =[1 2 4;3 4 2;1 2 4];hess(A);
[1 -3.16227 3.162277 -3.16227 5.2 -1.6 0 -1.6 2.8 ]
![Page 78: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/78.jpg)
78
The Schur Decomposition I Real SCHUR form
If A is real then Q is orthogonal and T = QTAQ is upper quasitriangular.
Imaginary SCHUR formIf the imaginary part of A is nonzero, then Q is unitary and T = QTAQ is upper triangular.
Syntax[Q,T] = schur(A);
It can be computed the complex Schur form from the real Schur form.[Q,T] = schur(A);[Q,T] = rsf2csf(Q,T);
![Page 79: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/79.jpg)
79
The Schur Decomposition II If A=[7 -4 0
8 -5 0 6 -6 3 ];
T = schur(A);T= [3 13.2 6.18448
0 -1 -1.87408 0 0 3 ] A=[3 4;-2 -1];
[Q,T] = schur(A);[Q,T] = rsf2csf(Q,T);T=[ 1+2i -4.47213; 0 1-2i ]
![Page 80: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/80.jpg)
80
The Eigenvector Decomposition
It can be compute a nonsingular X and diagonal D such that X-1AX = D is diagonal.
[X,D] = eig(A);
If A=[7 -4 0 8 -5 0 6 -6 3 ];
e = eig(A);e=[3 1 3]’
![Page 81: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/81.jpg)
81
Generalized Eigenvalues A two-argument version of eig can be used to
solve the generalized eigenvalue problem Ax = Bx, where A and B are square and of the same dimension.
It can be compute a matrix X and a diagonal matrixD such that AX = BXD.
[X,D] = eig( A, B )
![Page 82: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/82.jpg)
82
4.7 MISCELLANEOUS
![Page 83: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/83.jpg)
83
IEEE Arithmetic In MATLAB, the result of a zero divide is a
special value called “inf”.Example, 1/0.
NaN means not a number.Example, 0/0.
Thus, x = inf and y = NaNa=x+z a = infa=1/x a = 0a=y*z a = NaNa=x/y a = NaNa=[x;y] a(1)=inf, a(2)=NaN
![Page 84: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/84.jpg)
84
Timings We can execute the time which we run a
program.The function clock returns the current time in a row vector(year,month,day,hour,minute,and second).The function etime(t1,t2) returns the time in seconds between vectors t1 and t2.
for k =1:500n=k;A=rand(n);x=rand(n,1);t1=clock;y=fft(x);t2=clocktime=time+etime(t2,t1);end
![Page 85: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/85.jpg)
85
Timed Displays A delay can be build into a MATLAB segment
with the pause statement.
Display the n-by-n magic square matrix on the screen for approximately n seconds for n=1:5.
for n=1:5
A=magic(n)
pause(n)
end
![Page 86: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/86.jpg)
86
Getting Input It can be input a value during execution of a
MATLAB segment.
Write a function which displays magic square of chosen dimension by user. while(n>=1)
A=magic(n)
n=input('Enter dimension')
end
![Page 87: 1 4.1 BASICS. 2 Setting Up Matrices Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex:To set up a 4x4 matrix A,like A = [1 1 1 1;1 2 3 4;1.](https://reader035.fdocuments.in/reader035/viewer/2022070402/56649f265503460f94c3cee1/html5/thumbnails/87.jpg)
87
Converting Numbers to Strings
Num2str can be used to convert a number to its string format.
s = num2str(2) s = ‘2.0000’s = num2str(2) s = ‘-1.000E+2’s = num2str(2) s = ‘1.2345E+7’