clear all; echo on;clc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab Primer % Author: Paul Dostert % Description: This is a series of Matlab commands that should hep a new % user learn many of the basics of Matlab. This script is meant to be run % once, which will walk the user through each function. It can also be % used as a reference by simply reading through this .m file. % % This primer is split into 9 parts. This primer simply walks through some % commands part by part. It is not interactive. % 1. Basic Matlab Commands % 2. Basic Calculator Type Commands % 3. Vector Assignment % 4. Vector Operations % 5. Matrix Assignment % 6. Matrix Operations % 7. Matrix Functions % 8. Plotting in 2D % 9. Plotting in 3D % Press any key to start with part 1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %BASIC MATLAB COMMANDS: "help XXX" on any command displays more %information about a function (ie "help clear" tells how to use clear). %"doc" loads up the help menu, or "doc XXX" loads a very detailed %description of the function XXX. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Obviously, the '%' comments everything that comes on the line after it % % "echo on" makes it so every command in this .m file is displayed when % the file is run. "echo off" does the opposite. echo on; % Assign a variable: x = 5; % The who command displays all variables currently being used who % To clear one variable (such as x) do: "clear x". To clear all variables % type "clear all" clear x; % Show only 5 digits of precision. "format long" shows more if needed format short; % An important command to remember (but not use now) is "Ctrl-c". This % stops any computation or script. If you hit "Ctrl-c" on your keyboard % right now it will stop this primer. % % The pause command stops the script until the user hits a key. The clc % command clears the screen (it will clear once you hit a key) pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % BASIC CALCULATOR COMMANDS: Matlab works scalars (real or complex) just % like a regular calculator, where all your usual commands (+-/*^) work as % usual. Most everything else on a calculator (sin,cos,abs,etc) works as % well. Type: "help arith" to find out more about arithmetic commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ((3+3)-3)*2 / 6 2^3 1-2*i sin(pi) abs(-2.4) % These are exponential and natural log, log base 10 is log10 exp(1) log(2) % This is arccosine (same idea for other arcs) acos(0) pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % VECTOR ASSIGNMENT: To create vectors we use % the square brackets. A comma (or space) between entries indicates a new % column, while a semicolon indicates a new row. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Assign v the row vector from 2 to 7. There are two ways to do this: v = [2, 3, 4, 5, 6, 7] % This means 'go from 2 by 1 to 7' v = [2:1:7] % Assign w the column vector from 10 to 7 (going down) w = [10 ; 9 ; 8 ; 7] % This means 'go from 10 by -1 to 7'. This creates a row vector. To make % it a column vector, we use the TRANSPOSE operator, which is ' w = [10:-1:7]' pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % VECTOR OPERATIONS: Many usual vector operations are defined such as dot % (for the dot product), cross (cross product), norm, & scalar mult % are handled easily. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% v = [1 0 1]; w = [1 1 1]; dot(v,w) cross(v,w) norm(w) 2*v pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATRIX ASSIGNMENT: Just like vector assignment, but you have % rows/cols. There are a handful of nice commands to create new matrices % with standard entries %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Create a 2x3 matrix: A = [2 3 4; 5 6 7] % Create a 4x2 matrix: B = [2 3; 4 5; 6 7; 8 9] % Create a random 2 x 2 matrix, and a random 3x4 matrix (rand chooses % values between 0 and 1 randomly) A = rand(2); B = rand(3,4) % Create a 2x2 matrix of all ones, then all zeros, then the 2x2 identity A1 = ones(2) A2 = zeros(2) A3 = eye(2) pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATRIX OPERATIONS: Matrix operations behave just as expected. Here is an % interesting quirk to Matlab. You can have Matrix-Matrix (or % Matrix-Vector) operations, or you can have element-wise operations. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% A = [1 2; 2 1]; B = [ 1 1; -1 -1]; % AB (A times B, using standard Matrix-Matrix multiplication) A*B % A.*B (This take each element of A and multiplies it by the corresponding % element of B). Note that these are NOT the same. A.*B % Scalar multiplication 3*A % Change ONE element of A A(1,2) = 9; A % Reference only the 1st row of the new A A(1,:) % Matrix-Vector multiplication (we need x to be a column vector) x = [1,2]; A*x' % Matrix solve( the \ command, such as x=A\b means "solve Ax=b") b = [3; 1]; x = A\b pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATRIX FUNCTIONS: There are MANY matrix functions. Some are inv (inverse % of a matrix), norm (norm of a matrix), det (determinant of a matrix) % eig (eigenvalues/eigenvectors), size (matrix size), rank (matrix rank) % and ' (matrix transpose). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% A = [1 2; 3 4]; % Assign "Ainv" the inverse of A. If no inverse exists, this returns infs Ainv = inv(A) % By default, this uses the L2 norm (norm(A,N) uses the "N-norm"); norm(A) % Determinant of a matrix det(A) % The cols of V are evecs, the diagonal of D are the evals [V,D] = eig(A) % Assigns n to be the number of rows of A and m the number of cols of A [n,m] = size(A) % Returns the approx rank of A (This is not 100% accurate for each case) rank(A) % This returns the transpose of A A' pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PLOTTING IN 2D: There is really no such thing as plotting a function % "y=f(x)" for some unknown (x,y) in Matlab (unless you use some special % packages which not everyone has). The idea for doing any type of plotting % is that you want to plot "y=f(x)" as a set of points (x,f(x)). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % First, we set up points for "x". The "linspace" command will create a % vector containing 100 points from the starting to the ending value. So % the following sets x to be values from 0 to 1 by 1/99 (ie, 0,1/99,...,1) % You can also specify exactly how many points you want to use. For % example, x=linspace(0,1,1000) creates 1000 points. x = linspace(0,1); % Now, we plot two functions. y=2*x and y=x^2. Recall that x is a vector, so % x^2 doesn't really make any sense. What we want is x.^2 since we want to % square each value of x. % This command means plot x vs 2*x in blue (it uses a solid line by % default). THEN also plot x vs x.^2 in red, with a dotted line (the :) plot(x,2*x,'b--',x,x.^2,'r:') % These commands label the x and y axis, and create a title on the previous % plot: xlabel('x'); ylabel('y'); title('Plot of 2x and x^2'); % Note that we could also have done: y1 = 2*x; y2 = x.^2; plot(x,y1,'b--',x,y2,'r:'); pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PLOTTING IN 3D: This is a bit trickier than 2D, but the same idea % applies. We want to plot points in 2D, but now what we want to do is % create a mesh in 2D, then determine where these points on the mesh are in % 3D. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % We set up points to begin with. This makes x and y go from 0 to 1. x = linspace(0,1); y=x; % Now we set up a mesh of points from 0 to 1 in 2D (each combination of x,y % in 2d) [X Y] = meshgrid(x,y); % If we want to plot a surface that is given Z=F(X,Y), we can now. We have % many options. We can use the "mesh" command, which makes a 3d Mesh plot. % We can use the "surf" command, which is similar but sometimes gives % more detail. We can use the "contour" plot, which gives a 2D plot with % contour lines. We'll use all three to plot Z=X.^2 + Y.^2; mesh(X,Y,X.^2+Y.^2); title('Mesh Plot'); xlabel('x'); ylabel('y'); zlabel('z'); pause(2); surf(X,Y,X.^2+Y.^2); title('Surf Plot'); xlabel('x'); ylabel('y'); zlabel('z'); pause(2); contour(X,Y,X.^2+Y.^2); colorbar; title('Contour Plot'); xlabel('x'); ylabel('y'); zlabel('z'); pause(2); pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MORE HELP: To find any more information, the best way to do this is to % type "doc". The use the "search" tab to find the functions you're % looking for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%