MATLAB Learning Linear Algebra MATLAB Vector Products Dot
MATLAB 程式設計 Learning Linear Algebra 方煒 台大生機系
MATLAB 程式設計 Vector Products, Dot and Cross n n n a=[1, 2, 3]; b=[3, 2, 1]; C=a*b D=a. *b E=dot(a, b) F=cross(a, b)
MATLAB 程式設計 Ex 7_1 Solve a Linear System n n n A=[ 1, 0, 1; -1, 1, 1; 1, -1, 1 ]; b=[4; 4; 2]; % now solve for x x=Ab %we obtain [1; 2; 3]
MATLAB 程式設計 Ex 7_2 Max and Min n n x=0: . 01: 5; y=x. *exp(-x. ^2); % take a look at the function so we know what it looks like plot(x, y) % find the max and min ymin=min(y) ymax=max(y) % find the max and min along with the array indices imax and imin % where they occur [ymin, imin]=min(y) [ymax, imax]=max(y)
MATLAB 程式設計 Ex 7_3 Matrix Inverse n n n A=[1, 0, -1; -1, 1, 1; 1, -1, 1] % load C with the inverse of A C=inv(A) % verify by matrix multiplication % that A*C is the identity matrix A*C
MATLAB 程式設計 Ex 7_4 Transpose and Hermitian Conjugate n If your matrices are real, then there is no difference between these two commands n n n n %find the transpose of the matrix A A. ’ %find the Hermitian conjugate of matrix A A’ [1, 2, 3]’ [4; 5; 6]’
MATLAB 程式設計 Ex 7_5 a Special Matrices n n n n n % eye: % load I with the 4 x 4 identity matrix (the programmer who invented this % syntax must have been drunk) I=eye(4, 4) % zeros: % load Z with a 5 x 5 matrix full of zeros Z=zeros(5, 5) % ones: % load X with a 3 x 3 matrix full of ones X=ones(3, 3) % rand: % load Y with a 4 x 6 matrix full of random numbers between 0 and 1 % The random numbers are uniformly distributed on [0, 1] Y=rand(4, 6) % And to load a single random number just use r=rand % randn: % load Y with a 4 x 6 matrix full of random numbers with a Gaussian % distribution with zero mean and a variance of 1 Y=randn(4, 6)
MATLAB 程式設計 Ex 7_6 Determinant and Ex 7_7 Norm n n n Ex 7_6 Determinant %Find the determinant of a square matrix this way det(A) n Ex 7_7 Norm of Vector (Magnitude) %Matlab will compute the magnitude of a vector a n %(the square root of the sum of the squares of its components) n n %with the norm command a=[1, 2, 3] norm(a)
MATLAB 程式設計 Ex 7_8 Sum the Elements n n n %For arrays the command sum adds up the elements of the array: % calculate the sum of the squares of the reciprocals of the % integers from 1 to 10, 000 n=1: 10000; sum(1. /n. ^2) % compare this answer with the sum to infinity, which is pi^2/6 ans-pi^2/6 For matrices the sum command produces a row vector which is made up of the sum of the columns of the matrix. A=[1, 2, 3; 4, 5, 6; 7, 8, 9] sum(A)
MATLAB 程式設計 Ex 7_9 Eigenvalues and Eigenvectors n n %build a column vector containing eigenvalues of the matrix A in previous section use E=eig(A) n %To build a matrix V whose columns are the eigenvectors of the matrix A %and another matrix D whose diagonal elements are the eigenvalues %corresponding to the eigenvectors in V use n [V, D]=eig(A) n % to select the 3 rd eigenvector and load it into a column vector use n v 3=V(: , 3) n % i. e. , select all of the rows (: ) in column 3 n n
MATLAB 程式設計 more stuff n n n help help svd QR LU rref rcond
- Slides: 11