% matlabeg.m %This script shows some basic commands of Matlab. home disp('This script shows some of the basic commands of Matlab. Whenever ') disp('there is a pause, simply hit a key to continue to the next') disp('command. You can see this script file by going to File, then') disp('Open, and then selecting matlabeg.') disp(' ') disp('In the following, the command which is used to give') disp('the result is shown on a line by itself after the explanation.') disp(' ') pause disp('To define a matrix A, you would type') disp(' ') disp('A=[1 2 3;4 8 6; 7 2 9]') disp(' ') disp('and the output will be') disp('') A=[1 2 3;4 8 6; 7 2 9] disp('') disp('Note that the columns are separated by spaces (or commas) and the rows') disp('are separated by semicolons or returns.') pause disp(' ') disp('To change an entry, type') disp('A(1,1)=-1') A(1,1)=-1 pause disp('The transpose of A is obtained by typing ') disp('A'' ') A' disp('Note that when you do not use a variable name, the default') disp('name is ans.') pause disp(' ') disp('To square a matrix (and give it the name Asq), type') disp('Asq=A^2') Asq=A^2 pause disp('Matlab can also do coordinate-wise calculations. For example,') disp('to square each entry of a matrix, type') disp('A.^2') A.^2 disp('In general, coordinate-wise calculations will use the period along') disp('the operation.') disp(' '); pause disp('To multiply matrices use *. For example, type') disp('B=[5,1,0;4,10 -1;1 2 3]; A*B') B=[5,1,0;4,10 -1;1 2 3];A*B disp('Note that since a semicolon was used after the definition of B,') disp('the matrix B was not shown. That is, a semicolon will suppress the output.') pause disp(' ') disp('To multiply by the constant 3, type') disp('3*A') 3*A pause disp(' ') disp('Other arithmetical operations are defined in obvious ways.') disp('A+B') disp('A-3*B'); A+B A-3*B pause disp(' ') disp('To add the constant 2 to all entries, type ') disp('A+2') A+2 disp('Better notation for the above would be ') disp('A+2*ones(3,3)') A+2*ones(3,3) pause disp('To add the constant 2 to the diagonal entries, type') disp('A+2*eye(3,3)') A+2*eye(3,3) pause disp('The determinant of A is obtained by typing') disp('det(A)') det(A) pause disp('The rank of A is obtained by typing') disp('rank(A)') rank(A) pause disp(' ') disp('The inverse of a matrix (if it exists) can be obtained by typing') disp('inv(A)') inv(A) pause disp(' ') disp('The eigenvalues of A are listed in a vector when the following command is typed') disp('eig(A)') eig(A) pause disp('To see the eigenvalues and eigenvectors, type the following command') disp('[u,d]= eig(A)') [u,d]=eig(A) disp('The d matrix is a diagonal matrix with the eigenvalues on the diagonal. The ') disp('corresponding columns in the matrix u are eigenvectors.') disp('If the matrix is diagonalizable, then you should be able to recover A by typing') disp(' ') disp('u*d*inv(u)') u*d*inv(u) pause disp('The usual norm of a matrix can by found by typing') disp('norm(A)') norm(A) pause disp('A basis for the null space of A is given by typing') disp('null(A)') null(A) disp('In this case, A is invertible so its null space is trivial.') disp('To see a non-trivial example, define') disp('c=[1 2;2 4]') c=[1 2;2 4] disp('and now type') disp('null(c)') null(c) disp('Note that Matlab''s output of vectors is usually given as unit vectors') disp('and whenever possible, output vectors will be orthogonal.') pause disp(' ') disp('A basis for the range of A is given by typing') disp('orth(A)') orth(A) pause disp(' ') disp('To see a basis for the range of the above matrix c, type') disp('orth(c)') orth(c) pause disp(' ') disp('To multiply a matrix by a vector, the dimensions must match. That is, Matlab') disp('considers vectors as matrices with one row or one column.') disp(' ') disp('So first define a vector x. Either use x=[1;1;1] or x=[1 1 1]'' ') disp('x=[1;1;1]') x=[1;1;1] disp('To multiply A times x, type') disp('b=A*x') b=A*x pause disp('To solve a system of equations (in this case Ax=b for x), type') disp('A\b') A\b pause disp(' ') disp('Larger matrices can be defined by using loops. For example') n=10; disp('n=10;A=zeros(n,n)') A=zeros(n,n) pause disp('Define a loop to put ones on the super diagonal') disp('for i=1:n-1; A(i,i+1)=1; end') for i=1:n-1 A(i,i+1)=1; end A pause disp('A=A+A'' ') A=A+A' pause disp('A=A+3*eye(n)') A=A+3*eye(n) pause disp('Define corners to be 5.') disp('A(n,1)=5') disp('A(1,n)=5') A(n,1)=5; A(1,n)=5; A pause disp('To get a geometrical picture of A, use the command') disp('mesh(A)') mesh(A) pause disp(' ') disp('The same idea is used to graph functions. For example, if you type') disp('peaks, you will run a built-in script which produces the graph of a surface.') disp('If you want to see the script, type type peaks after this demo finishes.') disp('peaks') peaks