LAB 1, 01/29/14 _______________ 1. Customize MATLAB workspace: DESKTOP menu COMMAND (window) - absolutely necessary COMMAND HISTORY - useful when repeating commands WORKSPACE - useful for keeping track of variables CURRENT DIRECTORY - listing of files EDITOR - for creating .M files (script and function) HELP - documentation All windows can be popped out. 2. Assigning values to scalar variables; number formats x = 2; % semicolon suppresses output x = 3 % note the output x^2 % again, note output ans % most recent numerical output that % was not assigned to a variable; now ans y = pi*ans format long; y % semicolon also implies new line format short e; y format rat; y format; y % returns to default format (see HELP) Question: What variables appear in the Workspace window? 3. Array variables (i.e., mxn matrices) are fundamental to all MATLAB functions. Scalar variables (introduced above) are special (1x1) cases of arrays. The elements (entries) of an array - are enclosed in [ ] - are separated horizontally using spaces or commas - are separated vertically using newlines or ; Transposition (rows <--> columns) is accomplished using .' or simply ' if all entries are real. For example: vrow = [1 2 4 8] % row vector vcol = [1 3 9 27] % column vector vcol = [1 ; 3 ; 9 ; 27] % alternatively A = [1 2 4 8 1 3 9 27] A = [1 2 4 8 ; 1 3 9 27] % alternatively vcol' % note use of ' [vrow ; vcol'] % alternative for A A' A.' % same as ' for real A [vrow' ; vcol] % same as A' ? 4. Array indexing is simple: - v(k) is kth element of a row or column vector - A(k,l) is the (k,l)th element of a 2-dimensional matrix k and l can be replaced by vectors of indices (K and L), in which case v(K) and A(K,L) are sub-arrays of v and A. More on that later. Vectors with regularly spaced entries on the real line are created using an expression of the form FIRST VALUE : INCREMENT : LAST VALUE For example: v1 = 0 : 0.1 : 2 % what is the size of v1? v2 = (1 : 0.01 : 2).' % what is the size of v2? v3 = 2 : -0.1 : 0 % relationship to v1? v4 = 0 : 0.1 : 2.05 % relationship to v1? v5 = 0 : 10 % see below If INCREMENT is omitted, then it equals 1 by default. 5. MATLAB has powerful graphing capabilities. Two common graphing commands are PLOT (generally used for continuous graphs) STEM (suited for functions of a discrete variable) There are several optional parameters for both commands. In their simplest form, PLOT(X) or STEM(X) plots the vector X against the indices 1:length(X). Also, PLOT(X,Y) where X and Y are vectors of the same length, plots Y (vertical axis) against X (horizontal axis). plot(v1) % continuous graph stem(v1) % discrete graph plot(v1,3*v1) % explain the result 6. You can use either i or j (for the imaginary unit) to enter complex numbers - provided i and j have not been previously assigned other values. 3+4*i 3+i*4 3+j*4 3 + j*4 3 + 4i 3 + 4j 3 + j4 % unassigned variable j4 [3 + 4j] % 1x1 array [3 +4j] % 1x2 array 7. Naturally, if x and y are real vectors of the same size and orientation, z = x+j*y will produce a complex vector. The command PLOT(Z), where z is a complex vector, plots the imaginary part of Z against the real part of Z. Transposition of complex vectors is achieved using .' Use of ' (without the period) will result in complex conjugation, as well. z = v1 + j*3*v1 ; plot(z) % same plot as before v1 + j*v2 ; % v1 and v2 not of the same size (3+4j).' % 1x1, so transpose has no effect (3+4j)' % complex conjugation! plot(z.') % same plot as before plot(z') % explain the results