Professional Documents
Culture Documents
University of Florida
1.5.13 Quadrature/Numerical Integration................................................ 19
1.5.14 ODE23/ODE45............................................................................ 20
MATLAB MINIGUIDE
CONTENTS
1.1 MATLAB Manuals and Literature........................................................... 1
1.1.1 Manuals and Tutorials Available at CIRCA ................................. 1
1.1.2 Manuals Available in the Biery Room in Chemical Engineering .. 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
1.3 List of Essential Commands and Operators ............................................. 4
1.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
1.5 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 .................................. 12
1.5.5 Taking Input from the Keyboard .................................................. 14
1.5.6 Creating a Diary File.................................................................... 14
1.5.7. Creating an M-File....................................................................... 15
1.5.8 If Structure................................................................................... 15
1.5.9 While Structure............................................................................ 16
1.5.10 For Structure................................................................................ 16
1.5.11 Break Command .......................................................................... 17
1.5.12 M-Functions ................................................................................ 17
a. MATLAB user's guide (tutorial and reference) The related program SIMULINK includes MATLAB plus additional capabilities for
b. Control Toolbox manual simulating nonlinear dynamic systems. MATLAB is available in almost all computer platforms
c. SIMULINK Manual (IBM, Macintosh, HP, Sun, DEC). All commands remain unchanged from one computer to
d. Various manuals for each toolbox another.
1.1.1 Manuals and Tutorials Available at CIRCA 1.2.1 Modes of Using MATLAB
Note that you only need to type the word “quit”; the symbol “>>” is the MATLAB prompt.
Type: >> demo to get a demonstration of some of MATLAB's features. The following commands must be thoroughly studied from the MATLAB manual (or
tutorial) so you may carry out the essential operations available.
1.2.5 Useful Keyboard Commands
• diary
1.2.6 Adjusting the Format of Numbers Displayed • format short, format compact, format short e
• plot, hold, axis, title, text, shg, clg, semilog, loglog, subplot
format short shows 5 digits
• %
format short e shows 5 digits in exponential form
• ' (apostrophe)
format long shows 15 digits
• : (colon)
format long e shows 15 digits in exponential form
• ; (semicolon)
format compact does not display extra blank line after each answer is
• , (comma)
echoed
All of these commands must be typed in lowercase (MATLAB is case-sensitive).
To learn more about a command, type help. Example: >> help whos
C = >> A
0 0 0 Displays matrix A
0 0 0
A =
>> C = zeros(3) 5 6
7 8
C = >> B = A'
0 0 0 Transpose of matrix A. The apostrophe is used
0 0 0 to denote transposition, since it is common in
B =
0 0 0
5 7 mathematics to use the nomenclature A’ to
>> D = eye(2, 2) Creates an identity matrix of order 2 6 8 denote AT
>> C = A + B Sum of two matrices
D =
1 0
C =
0 1
10 13
>> D = eye(2, 3) The matrix does not have to be square for eye to 13 16
work >> det(A) Determinant of matrix
D =
1 0 0
ans =
0 1 0
-2
>> D = eye(size(A))
>> C = inv(A) Matrix inversion
D =
C =
1 0 0
-4 3
0 1 0
3.5 -2.5
>> D = eye(3)
>> C * A Matrix multiplication
D =
ans =
1 0 0
1 0
0 1 0
0 1
0 0 1
>> D = abs( [-3, 4] ) Absolute value of elements of row vector [-3, 4]
1.4.3 Defining Transcendental Functions— sin, cos, log, exp D =
3 4
>> C = A^2 Find the second power of matrix A (i.e. A2)
>> pi = 4 * atan(1) Defines % = 4 arctan(1). C =
67 78
pi = 91 106
3.1416 >> eig(A) Finds the eigenvalues of matrix A
sin(x)
0 This is a string
-0.5 »disp(s)
This is a string
-1
0 2 4 6 8 10 »s1 = 'This is';
x Defines string s1
»s2 = ' a string';
»s3 = [s1 s2] Defines string s2
»help plot Provides help on the "plot" command. See also: Defines string s3 as the concatenation of s1 and
loglog, semilog, polar, text s3 = s2
This is a string
1.5.2 Displaying to the Screen
»s1 = 'x is equal to ';
Defines string s1
»x = 17;
»s2 = num2str(x); Assigns a value to variable x
»disp('Result')
Displays the string "Result" on the screen Changes the value numeric variable x to a string
of characters. NOTE: num2str only converts
Result
»s3 = [s1 s2]
only to four decimal places
»disp('Result'),disp('------') s3 =
Displays the string "Result" on the screen
Result followed by a dashed underline x is equal to 17
------
»disp(s3)
»A = [1,2;3,4] x is equal to 17
Defines matrix A and echoes the result to the
A = screen
»help disp
See also: sprintf, fprintf, num2str, int2str
1 2
3 4
1.5.4 Appending Results to a Vector or a Matrix
»disp(A)
Displays the entries of matrix A but does not
1 2
3 4 display the header line "A ="
»x = []
Defines an empty vector
x =
[]
x1 = »t = [z, y]
»x = [x; x1] t y
---- ---
x = 1 0
Appends columns of x1 to x 10 1
10 100 2
20 1000 3
»x = [x;x1]
»y = log10(z); y = x =
y = 0 1 2 1 10 100
0
1 »diary off y = log10(x)
2
An M-file (or M-script) is a text file that contains MATLAB commands, and has the extension
1.5.9 While Structure
.m. Create this text file using a text editor. Common text editors are: xedit, vi, and emacs.
end
»test1
Execute 'test1.m' from the MATLAB command >>test3 Execute 'test3.m' from the MATLAB command
x = window window
x =
1000
1 10 100
x =
y = 100
0 1 2 x =
10
x =
1.5.8 If Structure 1
» help relop
100
The break command forces the termination of a for loop or a while loop.
function f = xsqylog(x, y) ; Create an M-file entitled 'xsqylog.m' containing
x = [0:0.001:10]'; f = x^2 * log10(y) the lines shown on the left column. M-functions
Create an M-file entitled 'test5.m' containing the can have multiple inputs.
y = sin(x);
n = length(y); lines shown on the left column. return
g =
1.5.12 M-Functions 1
M-functions are M-files that take inputs and return outputs.
>>x=10; sqlog(x)
The integration of functions can be done using the gaussian-quadrature function QUAD. This ODE23/ODE45 are differential equation solvers. This section describes solvers that appeared in
function requires that the user define an M-function that calculates the integrand for any value of earlier versions of MATLAB; consequently, these commands may be obsolete. On the other
b
the dummy integration variable. The example below calculates the integral !a (2x + 1)dx . hand, the newer solvers retain the basic ideas of the functions ODE23 or ODE45 described
function f = integrand(x) Create an M-file entitled 'integrand.m' containing below. The differential equation solved is of the form
the lines shown on the left column.
f = 2 * x + 1;
dx/dt = f(t, x)
return
where f(t, x) is known informally as the right-hand-side function. The functions ODE23 and
>>a = 0; b = 1 ; Define integration limits
>>quad('integrand', a, b) ODE45 are ordinary differential equation solvers of variable orders 2-3 and 4-5, respectively.
Call the QUAD function to integrate the function
ans = integrand.m between limits a to b 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
2 See also: quad8
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) Create an M-file entitled 'dxdt.m' containing the
lines shown on the left column.
f = -t^2 * log(x)
return
>>t0 = 0; tf = 4;
Define initial and final times to and tf
>>x0 = 2;
Define initial condition xo
>>[t,x] = ode23('dxdt',t0,tf,x0);
>>plot(t,x) Call the ODE23 solver
>>xlabel('t') Plot the solution
>>ylabel('x')
2
1.8
1.6
1.4
x
1.2
0.8
0 1 2 3 4
t