EECE 360
Matlab Tutorial Jan 29 2008
Outline What
is Matlab? Matlab Interface Basic Syntax Plotting Graphs Control System Toolbox Examples Useful Resources
Overview of Matlab
What is Matlab? Language
for technical computing Supports mathematical computation, visualization, and programming Composed using high-level programming language similar
For
to syntax of C
EECE 360
Use
Matlab in the assignments
Matlab Interface
Workspace
Command History
Change the current directory to the location of your Matlab file at startup
Command Window
Matlab Programming Two
Approaches
Coding
in Command Window
Good
for short programs Commands must be re-entered each time you run a simulation Coding Good
with .m-files
for long programs Allows users to save all the commands written in the .m-files
Command Window Interface
Write your commands here
(1) Creating .m-files File New MFile (2) Opening .m-files File Open select the .m-file
.m-file Interface
Write your commands here
To run the program, press F5
Workspace Interface The
workspace provides a summary of the variables
Command History Interface The
Command History stores all the commands entered in the Command Window previously No
history of .m-files is shown here
Syntax Errors All
errors are given in the Command Window in red Nature
of error is explained
Examples on Matlab Interface
Basic Syntax
Basic Syntax Assignment
of Variables
Syntax:
Variable = Expression Example: A = 1; Use
of semi-colons
With
semi-colon – suppress output Without semi-colon – displays output in command window Adding Add
comments
% before command line
Example:
% This is a demo
Basic Syntax (cont’d) Step
increment
Example:
k = 1:0.1:10 k is a row-matrix from 1 to 10 with step size = 0.1 Matrix
Expression Row-matrix
Hence, A
Column-matrix
for a 2x2 matrix, the syntax is
= [1 2; 3 4]
Basic Syntax (cont’d) Mathematical
(Array) Operators
Mathematical
Mathematical Array
+
Addition
+ Addition
-
Subtraction
-
* /
Multiplicatio n Division
.* Multiplicatio n ./ Division
^
Power
.^ Power
Note
Subtraction
the dimension of matrix in matrix computation
Example:
A: 2x1 matrix, B: 2x2 matrix A*B invalid operation B*A valid operation
Basic Syntax (cont’d) Common
Built-in Math Functions
sin(x)
- sine cos(x) - cosine tan(x) - tangent exp(x) - exponential log10(x) – base10 logarithm sqrt(x) – square root abs(x) – absolute value The
complete list can be found on pp. 833 of text
Examples on Basic Syntax
Plotting Graphs
Plotting Graphs Matlab
allows graphical displays
By
using the command plot Syntax: plot(x) Types
of plots -
Solid line
-- Dashed line
Syntax:
:
Dotted line
-.
Dashdot line
plot(x,’-’)
Plotting Graphs (cont’d) Labeling
Graphs
Commonly
used Syntax:
ylabel(‘text’)
– label y-axis xlabel(‘text’) – label x-axis title(‘text’) – label title legend(‘legend1’, ‘length2’,…) – puts legend on current plot grid on (grid off) – add (remove) grid lines to figure subplot – subdivides graphic window hold on – hold the previous plot on the graphic window
Example (1)
Example (2) subplot(2,2,1) plot(x,’r’),hold on plot(y,’--’)
subplot(2,2,2) plot(x,’r’),hold on plot(y,’--’)
subplot(2,2,3) plot(x,’r’),hold on plot(y,’--’)
subplot(2,2,4) plot(x,’r’),hold on plot(y,’--’)
Examples on Plotting Graphs
Control System Toolbox
Overview to the Toolbox Modeled
control systems as (1) transfer functions (2) zero-pole-gain (3) state-space form Can manipulate both (1) continuous-time system (2) discrete-time system Can compute and graph (1) time response (2) frequency response
Demo (1) Transfer Syntax:
Function tf(numerator, denomintor)
Demo (2)
State-space Syntax:
Representation
sys_ss = ss(sys)
Demo (3) Finding
Poles and Zeros
Syntax:
z = zero(sys) p = pole(sys)
Pole-Zero Syntax:
Diagram
zplane(z,p)
Demo (4) Closed-Loop Syntax:
+
System
CLsys = feedback(sys1,sys2)
System 1 -
System 2
Demo (5) Step
Response
Syntax:
step(sys)
Demo (6) Pole-zero Syntax:
Impulse
cancellation
sysr = minreal(sys);
Response
Syntax:
impulse(sys)
Examples on Control System Toolbox
Useful Resources Appendix End
A of Textbook
of Chapter – Matlab Demo
http://www.mathworks.com