Beruflich Dokumente
Kultur Dokumente
CONTENTS
1.1 MATLAB Manuals and Literature .............................................................................................. 1
1.1.1 Manuals and Tutorials Available at CIRCA ..................................... 1 1.2 Introduction to MATLAB ............................................................................. 2 1.2.1 Modes of Using MATLAB .............................................................. 2 1.2.2 Executing the MATLAB Program.................................................... 2 1.2.3 Exiting from the MATLAB Program ............................................... 2 1.2.4 Obtaining On-Line Help .................................................................. 2 1.2.5 Useful Keyboard Commands ........................................................... 3 1.2.6 Adjusting the Format of Numbers Displayed.................................... 3 List of Essential Commands and Operators ................................................. 4 Overview of MATLAB Operations ............................................................... 5 1.4.1 Defining Vectors, Matrices and Polynomials.................................... 5 1.4.2 Defining Three Useful Functions ones, zeros, eye ........................ 6 1.4.3 Defining Transcendental Functions sin, cos, log, exp ................... 7 1.4.4 Defining Basic Operations det, eig, conv, poly ............................. 8 Advanced MATLAB Commands .................................................................. 10 1.5.1 Plotting ............................................................................................ 10 1.5.2 Displaying to the Screen .................................................................. 11 1.5.3 Operating with Strings of Characters................................................ 12 1.5.4 Appending Results to a Vector or a Matrix....................................... 13 1.5.5 Taking Input from the Keyboard ...................................................... 14 1.5.6 Creating a Diary File........................................................................ 15 1.5.7. Creating an M-File.......................................................................... 15 1.5.8 If Structure....................................................................................... 16 1.5.9 While Structure................................................................................ 16 1.5.10 For Structure.................................................................................. 17 1.5.11 Break Command ............................................................................ 17 1.5.12 M-Functions................................................................................... 18 1.5.13 Quadrature/Numerical Integration.................................................. 20 1.5.14 ODE23/ODE45.............................................................................. 21 File Name: Matlab_Miniguide_Rev_7.3.doc
1.3 1.4
1.5
1.1 MATLAB MANUALS AND LITERATURE Program name: MATLAB PRO-MATLAB Note: (personal computers) (workstations)
Basic manuals an documentation: a. b. c. d. 1.1.1 MATLAB user's guide (tutorial and reference) Control Toolbox manual SIMULINK Manual Various manuals for each toolbox Manuals and Tutorials Available at CIRCA a. CIRCA Lab: Room: CSE 211 - Phone: 392-2446 Hours: M-F 8:00 AM - midnight Sat. Noon - Midnight b. CIRCA consultants office Room: CSE 520 - Phone: 2-HELP (2-4357) Hours: M-F 9:00 AM - 5:00 PM
-1-
MATLAB Miniguide
1.2 INTRODUCTION TO MATLAB MATLAB is a high-level computer language with powerful commands that emphasize numerical operations with matrices. The word MATLAB itself is derived from the words MATrix LABoratory. Originally MATLAB carried out only linear algebra calculations (matrix operations) but has now been extended to include operations useful for signal processing, control analysis and design, nonlinear differential equations, and many others. These extensions are done by a number of routines collected under directories called Toolboxes, such as the following: Control toolbox Robust control toolbox Identification toolbox MFD (multivariable frequency design) toolbox Optimization toolbox Splines toolbox The related program SIMULINK includes MATLAB plus additional capabilities for simulating nonlinear dynamic systems. MATLAB is available in almost all computer platforms (IBM, Macintosh, HP, Sun, DEC). All commands remain unchanged from one computer to another. 1.2.1 a. b. Modes of Using MATLAB Interactive (commands issued using a keyboard). M-files (programs) , which can be of three types: (1) M-scripts (also called M-files), (2) M-functions, and (3) SIMULINK M-files. Executing the MATLAB Program
1.2.2
Type: matlab The prompt commands. 1.2.3 >> appears, indicating that MATLAB is ready to receive interactive
Type: >> quit Note that you only need to type the word quit; the symbol >> is the MATLAB prompt. 1.2.4 Obtaining On-Line Help
-2-
MATLAB Miniguide
Example: to get help on the command dir type: >> help dir Type: >> demo to get a demonstration of some of MATLAB's features. 1.2.5 Useful Keyboard Commands
^a ^e ^d Backspace 1.2.6
Repeats previous command Advances to next command Move left on command line Move right on command line Move to beginning of command line Move to end of command line Delete next character Delete previous character
format short format short e format long format long e format compact
shows 5 digits shows 5 digits in exponential form shows 15 digits shows 15 digits in exponential form does not display extra blank line after each answer is echoed
-3-
MATLAB Miniguide
1.3 LIST OF ESSENTIAL COMMANDS AND OPERATORS The following commands must be thoroughly studied from the MATLAB manual (or tutorial) so you may carry out the essential operations available. diary save, load help who, whos, size, dir, clear inv eig zeros, ones, eye conv roots format short, format compact, format short e plot, hold, axis, title, text, shg, clg, semilog, loglog, subplot % ' : ; , (apostrophe) (colon) (semicolon) (comma)
All of these commands must be typed in lowercase (MATLAB is case-sensitive). To learn more about a command, type help. Example: >> help whos
-4-
MATLAB Miniguide
1.4 OVERVIEW OF MATLAB OPERATIONS 1.4.1 Defining Vectors, Matrices and Polynomials
!1 $ Defines column vector x = # 2 & # & # 3& " %
x = 1 2 3 >>x = [1 2 3] x = 2 3 >> x = [1, 2, 3] x = 1 2 3 >> A = [5 6 7 8] A = 5 6 7 8 >> A = [5, 6; 7, 8] A = 5 7 >> b = A(2, 2) b = 8 >> b = A(2, :) b = 7 8 >> b = A(:, 2) b = 6 8 6 8 1
!1 $ Defines column vector x = # 2 & # & # 3& " % The semicolon indicates that the next element is on a new row (the semicolon is an end-of-row marker)
Defines row vector x = [1 2 3] Echo printed on screen Defines row vector x = [1 2 3 ] The comma indicates that the next element is on a new column ( the comma is an end-of-column marker) Defines matrix A
Defines matrix A The comma separates elements on the same row, and the semicolon separates elements on different rows Denotes the element (2, 2) of matrix A Denotes row 2 of A and all columns of matrix A
-5-
MATLAB Miniguide
>> b = A(1, 1) + A(2, 2) b = 13 >> A(1, 1) + A(2, 2) ans = 13 >> p = [1 0.9 0.3] p = 1 >> size(A) ans = 2 2 >> poly2sym([1 0.9 0.3], s) ans = s2 + 0.9s + 0.3 >> size(p) ans = 1 >> who Your variables are: A ans b p x 3 0.9 0.3
Sum of elements (1, 1) and (2, 2) of matrix A The result is written to the default variable ans Denotes the polynomial p(s) = s2 + 0.9s + 0.3 Polynomials are represented as row vectors. Finds the number of rows and columns of matrix A Transforms a row-vector polynomial representation into a standard symbolic representation shown the powers of the variable s and all the terms in the polynomial (requires that the Symbolic Toolbox be installed) Finds the number of rows and columns of vector p List all variables currently defined
1.4.2
>> B = ones(2, 2) B = 1 1 1 1 >>B = ones(2, 1) B = 1 1 >> A = [5 6 9; 7 8 10], ... B = ones(size(A)) A = 5 7 B = 1 1 1 1 >> B = ones(3) 1 1 6 8 9 10
Create a matrix consisting of 2 rows and 2 columns, and whose entries are all equal to 1
Create a matrix consisting of 2 rows and 1 column, and whose entries are all equal to 1
More than one command can be given on a line, by separating each individual command with a comma. Commands can also be embedded into other commands. The ellipsis (...) indicates that the command continues on the next line When only one number is given, the function
-6-
MATLAB Miniguide
ones B = 1 1 1 1 1 1 >> C = zeros(2, 2) C = 0 0 0 0 >> C = zeros(2, 1) C = 0 0 >> C = zeros(size(A)) C = 0 0 0 0 >> C = zeros(3) C = 0 0 0 0 0 0 >> D = eye(2, 2) D = 1 0 0 1 >> D = eye(2, 3) D = 1 0 0 0 1 0 >> D = eye(size(A)) D = 1 0 >> D = eye(3) D = 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1
1.4.3
Defines = 4 arctan(1).
-7-
MATLAB Miniguide
cosine function
Logarithm to base 10
1.4.4
>> A A = 5 6 7 8 >> B = A' B = 5 7 6 8 >> C = A + B C = 10 13 13 16 >> det(A) ans = -2 >> C = inv(A) C = -4 3.5 >> C * A ans = 1 0 0 1 >> D = abs( [-3, 4] ) D = 3 4 >> C = A^2 C = 67 91 >> eig(A) 78 106 3 -2.5
Displays matrix A
Transpose of matrix A. The apostrophe is used to denote transposition, since it is common in mathematics to use the nomenclature A to denote AT Sum of two matrices
Determinant of matrix
Matrix inversion
Matrix multiplication
ans = -0.1521 13.1521 >> [H, L] = eig(A) H = -0.7587 0.6515 L = -0.1521 0 0 13.1521 >> b = [10; 20], C = [A, b] b = 10 20 C = 5 6 10 7 8 20 >> i = sqrt(-1) i = 0 + 1i >> z = 3 + 4 * i z = 3 +4i >> z + (1 + 10 * i ) ans = 4 + 14i >> p1 = [1, 0.3], ... p2 = [1, 0.1] p1 = 1 p2 = 1 0.1 >> p3 = conv(p1, p2) p3 = 1 0.4 0.03 >> p = [1 -6 11 -6], ... r = roots(p) p = 1 r = 3 2 1 >> q = poly(r) q = 1 -6 11 -6 -6 11 6 0.3 -0.5928 0.8054
Eigenvalues of A are written in diagonal matrix written in the columns of matrix H = [v1 v 2 ] , where v1 = $
!-0.5928$ "!0.7587% ' and v 2 = #-0.8054& # 0.6515 & " %
"!1 L=$ #0 0% !2 ' &
!1
Defines polynomials p1(s) = s + 0.3 and p2(s) = s + 0.1 The ellipsis (...) indicates that the command continues on the next line Product of two polynomials (convolution) p3(s) = p1(s) p2(s) = s2 + 0.4s + 0.03 Define p(s) = s3 - 6s2 + 11s - 6 and find its roots (s1 = 3, s2 = 2, and s3 = 1). The roots are stored in column-vector r.
Creates polynomial q(s) which has roots indicated by the entries of vector r = [3 2 1]T. Here q(s) = s3 6s2 + 11s - 6
-9-
MATLAB Miniguide
x = [0:0.1:10];
Generates vector x with first component equal to 0 and last component equal to 10. Intermediate components are spaced at intervals equal to 0.1 Calculates vector y Generates a plot of x versus y
1
0.5
-0.5
-1
10
plot(x, y, '--')
0.5
-0.5
-1
10
- 10 -
MATLAB Miniguide
sine plot
ylabel('sin(x)') grid
0.5
sin(x)
-0.5
-1
10
help plot
Provides help on the "plot" command. See also: loglog, semilog, polar, text Saves the figure that is currently displayed as a PDF (portable document format) file with name FileName.pdf Saves the figure that is currently displayed as a color EPS (encapsulated PostScript) file with name FileName.eps that will print in a PostScript printer with a resolution of 300 dpi. The default resolution is 150 dpi. The option -tiff creaters a 72 dpi preview that can be displayed in a computer screen by many word processors that import the file. Same as above, except that the file is saved in a color EPS format. Saves the figure that is currently displayed to a file with the name FileName.ai that is compatible with the Adobe Illustrator graphical software package.
1.5.2
disp('Result')
- 11 -
MATLAB Miniguide
disp(A) 1 2 3 4
Displays the entries of matrix A but does not display the header line "A ="
1.5.3
s = 'This is a string' s = This is a string disp(s) This is a string s1 = 'This is'; s2 = ' a string'; s3 = [s1 s2] s3 = This is a string s1 = 'x is equal to '; x = 17; s2 = num2str(x);
s3 = [s1 s2] s3 =
Defines string s1 Assigns a value to variable x Changes the value numeric variable x to a string of characters. NOTE: num2str only converts only to four decimal places
- 12 -
MATLAB Miniguide
1.5.4
Defines vector x1
Defines vector x2
Appends columns of x1 to x
10 20 x = [x;x1] x = 10 20 10 20 x = [x;x2;x2] x = 10 20
- 13 -
MATLAB Miniguide
10 20 100 200 100 200 z = [1;10;100;1000] z = 1 10 100 1000 y = log10(z); y = 0 1 2 3 t = [z, y] t = 1 10 100 1000 disp(' t disp('---t ---1 10 100 1000 y --0 1 2 3 0 1 2 3 y'); ... ---'); disp(t)
1.5.5
Asks the user to give a value for n If user enters "3" followed by return, the value of n is displayed on the screen
- 14 -
MATLAB Miniguide
1.5.6
Creates a file named 'exercise1' with the following contents that contains all the lines displayed to the screen following the command "diary". The file exercise1 has the following contents:
x = [1 10 100] x =
y = log10(x) y = 0 1 2
10 100
diary off
y = log10(x) y = 0 1 2
diary off
1.5.7.
Creating an M-File
An M-file (or M-script) is a text file that contains MATLAB commands, and has the extension .m. Create this text file using a text editor. Common text editors are: xedit, vi, and emacs.
x = [1 10 100] y = log10(x)
Use a text editor to create and save a file (M-file) containing the two lines shown on the left column. Save the file under the name 'test1.m'
test1 x = 1 y = 0 1 2 10 100
- 15 -
MATLAB Miniguide
1.5.8
If Structure
x = input('value of x ') if x < 0 disp('x negative') elseif x == 0 disp('s is equal to 0') else disp('s is positive') end
Create an M-file entitled 'test2.m' containing the lines shown on the left column. The relational operators are: == < > | equal less than greater than logic or ~= <= >= & not equal less than or equal greater than or equal logic and
Execute 'test2.m' from the MATLAB command window and input the value "3" in response to the query.
1.5.9
While Structure
Create an M-file entitled 'test3.m' containing the lines shown on the left column.
- 16 -
MATLAB Miniguide
1.5.10
For Structure
Create an M-file entitled 'test4.m' containing the lines shown on the left column.
1.5.11
Break Command
The break command forces the termination of a for loop or a while loop.
x = [0:0.001:10]'; y = sin(x); n = length(y); for i = 1 : n if y(i) >= 0.95 x1 = x(i); break end end disp(['sine is equal to 0.95 at x = ' num2str(x1)]) >>test5 sine is equal to 0.95 at x = 1.254
Create an M-file entitled 'test5.m' containing the lines shown on the left column. The break command exits the loop whenever the line containing the word "break" is executed.
- 17 -
MATLAB Miniguide
1.5.12 M-Functions M-functions are M-files that take inputs and return outputs.
Create an M-file entitled 'sqlog.m' containing the lines shown on the left column. The variable "f" appearing to the left of the equal sign of the first line is the output, while the variable "x" appearing on the right hand side is the input to the function. Assign the value of 1 to variable x. Execute 'sqlog.m' from the MATLAB command window.
>>x = 1; >>sqlog(x) ans = 0 >>x=10; sqlog(x) ans = 100 function f = xsqylog(x, y) ; f = x^2 * log10(y) return
Create an M-file entitled 'xsqylog.m' containing the lines shown on the left column. M-functions can have multiple inputs.
>>xsqylog(3,100) ans = 18 function [f, g] = fsqglog(x) f = x^2; g = log10(x); return >>[f, g] = fsqglog(10) f = 100 g =
Create an M-file entitled 'fsqglog.m' containing the lines shown on the left column. M-functions can have multiple outputs.
- 18 -
MATLAB Miniguide
- 19 -
MATLAB Miniguide
1.5.13
Quadrature/Numerical Integration
The integration of functions can be done using the gaussian-quadrature function QUAD. This function requires that the user define an M-function that calculates the integrand for any value of b the dummy integration variable. The example below calculates the integral !a (2x + 1)dx .
function f = integrand(x) f = 2 * x + 1; return >>a = 0; b = 1 ; >>quad('integrand', a, b) ans = 2
Create an M-file entitled 'integrand.m' containing the lines shown on the left column.
Define integration limits Call the QUAD function to integrate the function integrand.m between limits a to b See also: quad8
- 20 -
MATLAB Miniguide
1.5.14
ODE23/ODE45
ODE23/ODE45 are differential equation solvers. This section describes solvers that appeared in earlier versions of MATLAB; consequently, these commands may be obsolete. On the other hand, the newer solvers retain the basic ideas of the functions ODE23 or ODE45 described below. The differential equation solved is of the form dx/dt = f(t, x) where f(t, x) is known informally as the right-hand-side function. The functions ODE23 and ODE45 are ordinary differential equation solvers of variable orders 2-3 and 4-5, respectively. They require that the user define an M-function that calculates the values of the right-hand-side function f(x, t). The example below calculates the solution to the equation dx/dt = t2 log(x) with initial value xo = 2. The result is obtained for values of t ranging from to = 0 to tf = 4.
function f = dxdt(t,x) f = -t^2 * log(x) return >>t0 = 0; tf = 4; >>x0 = 2; >>[t,x] = ode23('dxdt',t0,tf,x0); >>plot(t,x) >>xlabel('t') >>ylabel('x')
2 1.8 1.6 1.4 1.2 1 0.8 0 1 2 t 3 4
Create an M-file entitled 'dxdt.m' containing the lines shown on the left column.
Define initial and final times to and tf Define initial condition xo Call the ODE23 solver Plot the solution
- 21 -
MATLAB Miniguide