Вы находитесь на странице: 1из 40

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 to syntax of C

 For EECE 360


 Use Matlab in the assignments
Matlab Interface
Change the current
directory to the
location of your
Matlab file at
startup
Workspace

Command
History 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 with .m-files


Good 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  M-
File

(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
 TheCommand History stores all the commands
entered in the Command Window previously
 No history of .m-files is shown here
Syntax Errors
 Allerrors 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
 Withsemi-colon – suppress output
 Without semi-colon – displays output in
command window
 Adding comments
 Add % 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 Column-matrix

 Hence, for a 2x2 matrix, the syntax is


A = [1 2; 3 4]
Basic Syntax (cont’d)
 Mathematical (Array) Operators
Mathematical Mathematical Array
+ Addition + Addition
- Subtraction - Subtraction
* Multiplicatio .* Multiplicatio
n n
/ Division ./ Division
^ Power .^ Power

 Note 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
 Byusing the command plot
 Syntax: plot(x)

 Types of plots
- Solid line
-- Dashed line
: Dotted line
-. Dashdot
line
 Syntax: 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) subplot(2,2,2)
plot(x,’r’),hold on plot(x,’r’),hold on
plot(y,’--’) plot(y,’--’)

subplot(2,2,3) subplot(2,2,4)
plot(x,’r’),hold on plot(x,’r’),hold on
plot(y,’--’) 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 Function
 Syntax: tf(numerator, denomintor)
Demo (2)

 State-space Representation
 Syntax: sys_ss = ss(sys)
Demo (3)
 Finding Poles and Zeros
 Syntax: z = zero(sys)
p = pole(sys)

 Pole-Zero Diagram
 Syntax: zplane(z,p)
Demo (4)
 Closed-Loop System
 Syntax: CLsys = feedback(sys1,sys2)

+
System 1
-

System 2
Demo (5)
 Step Response
 Syntax: step(sys)
Demo (6)
 Pole-zero cancellation
 Syntax: sysr = minreal(sys);
 Impulse Response
 Syntax: impulse(sys)
Examples on
Control System Toolbox
Useful Resources
 Appendix A of Textbook

 End of Chapter – Matlab Demo

 http://www.mathworks.com

Вам также может понравиться