Massachusetts Institute of Technology 6.003:Signals and Systems -Fall 1998 Matlab Picocourse exerpts from the " picocrse demo" %_____________________________________________________________________________ %_____________________________________________________________________________ %%%%%%% Introduction %_____________________________________________________________________________ %_____________________________________________________________________________ MATLAB is a specialized software system for numerical computations with matrices. We will see that very sophisticated functions and computation routines can be built from the basic matrix operations within MATLAB. This 2-hour "Pico"-Course consists of a series of brief lessons intended to introduce the syntax of MATLAB and especially to present some commonly used functions and techniques. In addition, some very specific topics useful for 6.003 are discussed in more detail. We *strongly advise* that the student becomes accustomed to plotting capabilities, Help features, and defining one's own specialized functions using M-files in MATLAB. With this goal in mind, we close our presentation by using the power of MATLAB to analyze the step response for the parallel RLC circuit. The overall presentation consists of five lessons: Lesson 1: Generating Matrices Lesson 2: Basic Operations: Matrix and Element-wise Lesson 3: Working with Complex Numbers and Polynomials Lesson 4: Plotting and Printing Lesson 5: Illustrative Example -- Parallel RLC Circuit (Using MATLAB Help features and defining one's own Scripts/Functions using M-files) %_____________________________________________________________________________ %%%%%%% Lesson 1: Generating Matrices %_____________________________________________________________________________ % 1) Basic syntax for defining matrices, vectors, and scalars % 2) Some special functions: "colon" operator,"linspace","eye","zeros","ones" % 3) Extracting elements, vectors, and blocks from existing matrices % % At every keypress, one or more MATLAB commands is executed and an % explanation will follow the format below: (Actual MATLAB syntax) %(brief explanation of the command) (MATLAB's response to the issued command) %_____________________________________________________________________________ A = [1 7; -4 8] %2x2 matrix A= %___________________________________________ b = [9; 3] %2x1 column vector b = %___________________________________________ c1 = [9 3] %1x2 row vector c1 = c2 = b' %1x2 row vector -- via transpose c2 = %___________________________________________ alpha = 1 %scalar alpha = %___________________________________________ H = [A b] %2x3 matrix -- via block form (DIMENSIONS!) H = %___________________________________________ t1 = [0:4] %1x5 row vector -- via "colon" t1 = %___________________________________________ t2 = linspace(0,4,5) %1x5 row vector -- via "linspace" t2 = %___________________________________________ t3 = [-1:0.2:0] %6x1 row vector -- via "colon" t3 = t4 = linspace(-1,0,6) %6x1 row vector -- via linspace t4 = %CAn also DO indescending order : [0:0.2:-1] and linspace(0,-1,6) %___________________________________________ I1 = [1 0 0; 0 1 0 ; ... 0 0 1] I1 = I2 = eye(3) %3x3 Identity Matrix -- via "eye" I2 = %___________________________________________ Z1 = [0 0 0; 0 0 0] %2x3 zero matrix Z1 = Z2 = zeros(2,3) %2x3 zero matrix -- via "zeros" Z2 = %___________________________________________ O2 = ones(3,2) %3x2 ones matrix -- via "ones" O2 = %___________________________________________ txt = 'string' %Can also generate matrices/vectors of characters txt = txt = ['s' 't' 'r'; 'i', 'n', 'g'] %Character-by-character example, txt = %___________________________________________ [numrows,numcols] = size(H) %Getting dimensions of existing variables numrows = numcols = numrows = size(H,1) %Getting number of rows only numrows = numcols = size(H,2) %Getting number of columns only numcols = %___________________________________________ A %Extracting elements from existing matrices A = 1 7 -4 8 alpha = A(1,1) %scalar, upper-left element of A alpha = %___________________________________________ H %can use the "colon" operator H = 1 7 9 -4 8 3 A = H(1:2,1:2) %2x2 matrix block, upper-left block of A A = 1 7 -4 8 b = H(1:2,3) %2x1 column vector, top of 3rd column of A b = %___________________________________________ A= H(:,1:2) %2x2 matrix block, all rows of left twp columns of H A = F = H(:,[1 3]) %2x2 matrix block, 1st & 3rd columns of A F = %___________________________________________ %can use the "size" function A = H(1:size(H,1),1:2) %2x2 matrix block, all rows of left twp columns of H A = %___________________________________________