Anda di halaman 1dari 56

MATLAB

MATLAB Introduction
• MATLAB was originally developed to be a
"matrix laboratory," written to provide easy
access to matrix software developed by the
LINPACK and EISPACK projects.
• Since then, the software has evolved into an
interactive system and programming
language for general scientific and technical
computation and visualization.
SIMULINK
• SIMULINK is an interactive system for the nonlinear
simulation of dynamic systems.
• A graphical, mouse-driven program that allows
systems to be modeled by drawing a block diagram on
the screen.
• It can handle linear, nonlinear, continuous-time,
discrete-time, multivariable, and multirate systems.
• SIMULINK runs on workstations using X-Windows.
• SIMULINK is fully integrated with MATLAB, and,
together with MATLAB and the Control System
Toolbox, forms a complete control system design and
analysis environment.
Basic Features of MATLAB
• MATLAB commands are expressed in a form very
similar to that used in mathematics and engineering.
– For instance, b = A x, where A, b, and x are matrices, is
written b = A * x .
– To solve for x in terms of A and b, write x = A\b
• There is no need to program matrix operations
explicitly like multiplication or inversion.
• Solving problems in MATLAB is much quicker than
programming in a high-level language such as C or
FORTRAN.
Character Set
• ASCII Character Set
• The following are used as operators:
– ! ‘ “ ( : ) , … / : ; < <= == > >= @ [..] [] % & \
^ | >> ~ ~=
– .’ .* ./ .\ .^
Comments
• A line beginning with %
Variable Names
• Case sensitive
• Up to 31 characters long
• Begin with a letter
• Followed by letters, digits, underscores
• Variables are created when they are assigned.
• pi, i, and j are predefined.
• To list variables use who or whos (detailed)
• clear or clear var clears out the workspace
Commands
• Type exit or quit to quit MATLAB
• Type foo to run foo.m
• Terminate a command with ; to suppress output
• A continuation line is shown with …
• clc clears the command window
• help foo displays the help for foo()
• lookfor will search for a string in the help
• ^C to abort a command
More Commands
• To save the current workspace to
filename.mat: save filename
• To load: load filename
• To capture output: diary filename and
diary off or diary on
• To display a variable: disp var
• To interact with the operating system: !
Data Types
• Originally just complex matrices
• Now include
– double
– sparse (2-D only)
– char
– cell
– struct
– storage (specialized)
– function handle
• The fundamental types are multi-dimensional arrays
Matrices and Arrays
• A(i,j,k) accesses that entry in the matrix A
Operators 1
• ! System command
• ‘ Conjugate transpose, string delimiter
• “ Quote
• ( : ) Used in matrix subscripting
• , Separates commands
• … Continuation
• / Right division
• : Colon
• ; Terminates a command without output
Operators 2
• < Less than
• <= Less than or equal
• == Logical equal
• > Greater than
• >= Greater than or equal
• @ Function handle
• [..] Matrix building
• [] Empty matrix
• % Comment
• & Logical and
Operators 3
• \ Left division
• ^ Power
• | Logical or
• >> Prompt
• ~ Logical not
• ~= Logical not equal
• * Multiplication
• + Addition
• - Subtraction
Array Operators
• .’ Transpose
• .* Array multiplication
• ./ Array right division
• .\ Array left division
• .^ Array exponentiation
• inv(A) Inverse
IEEE Arithmetic
• All arithmetic is in accordance with the
double precision IEEE standard.
• 64 bits per number
• All computations are in floating point.
Statements
• Multiple statements can appear on the same
line, separated by semicolons or commas.
• If a statement is terminated by a semicolon,
output is suppressed; otherwise it is printed.
• Output can be formatted with the format
command
Functions
• MATLAB has thousands of functions, and
you can add your own using m-files.
Function Argument Lists
• Input arguments are to the right of the function
name, within parentheses
• Output arguments are to the left of the function
name, within square brackets
– X = [3 4];
– norm(X)
ans = 5
– norm(X,1) (largest column sum)
ans = 7
– [m,n] = size(A)
m=5
n=3
M-Files
• Sample file
%MARKS
Exmark = [12 0 5 28 87 3 56];
Exsort = sort(Exmark)
Exmean = mean(Exmark)
Exmed = median(Exmark)
Exstd = std(Exmark)
Storage Allocation
• Automatic, as necessary, and with garbage
collection.
• Array dimensions are expanded
automatically as needed to make
assignments sensible.
Control System Toolbox
• This is a toolbox for control system design
and analysis. It supports transfer function
and state-space forms (continuous/discrete
time, frequency domain), as well as
functions for step, impulse, and arbitrary
input responses. Functions for Bode,
Nyquist, Nichols plots, design with root-
locus, pole-placement, and LQR optimal
control are also included.
Image Processing Toolbox
• The Image Processing Toolbox builds on
MATLAB's numeric, signal processing, and
visualization capabilities to provide a
comprehensive system for image processing
and algorithm development.
MMLE3 Identification Toolbox
• The MMLE3 Identification Toolbox is a
specialized toolbox for use with MATLAB
and the Control System Toolbox for the
estimation of continuous-time state-space
models from observed input-output data.
Model Predictive Control
Toolbox
• The Model Predictive Control Toolbox is
especially useful for applications involving
constraints on the manipulated and/or
controlled variables. For unconstrained
problems, model predictive control is
closely related to linear quadratic optimal
control, but includes modeling and tuning
options that simplify the design procedure.
Mu-Analysis and Synthesis
Toolbox
• The Mu-Analysis and Synthesis Toolbox
contains specialized tools for the analysis
and design of robust, linear control systems,
extending MATLAB to provide additional
application-specific capabilities.
Nonlinear Control Design
• This toolbox provides a Graphical User
Interface to assist in time-domain-based
control design. With this toolbox, you can
tune parameters within a nonlinear
SIMULINK model to meet time-domain
performance requirements. You can view
the progress of an optimization while it is
running. Optimization routines have been
taken from the Optimization Toolbox.
Neural Network Toolbox
• This is a toolbox for designing and
simulating neural networks and supports
implementation of the perceptron learning
rule, the Widrow-Hoff rule, and several
variations of the backpropagation rule.
Transfer functions included are hard limit,
linear, logistic, and hypertangent sigmoid.
Optimization Toolbox
• This is a toolbox for linear and nonlinear
optimization. It supports unconstrained and
constrained minimization, minimax,
nonlinear least squares, multi-objective,
semi-infinite optimization, linear
programming, quadratic programming, and
the solution of nonlinear equations.
Robust Control Toolbox
• This is a toolbox for robust control system
design and supports LQG/loop transfer
recovery, H2, H0, and mu- control
synthesis, singular value frequency
response, and model reduction.
Signal Processing Toolbox
• This is a toolbox for digital signal processing
(time series analysis). It includes functions for the
design and analysis of digital filters, like
Butterworth, Elliptic, and Parks-McClellan, and
for FFT analysis (power spectrum estimation). It
also includes some two-dimensional signal
processing capabilities.
• Very popular in the acoustics field.
Spline Toolbox
• This is a toolbox for working with splines
and is typically used for curve fitting,
solution of function equations, and
functional approximation.
Statistics Toolbox
• The Statistics Toolbox builds on the
computational and graphics capabilities of
MATLAB to provide: 1) statistical data
analysis, modeling, and Monte Carlo
simulation 2) building blocks for creating
your own special-purpose statistical tools,
and 3) GUI tools for exploring fundamental
concepts in statistics and probability.
Symbolic Math Toolbox
• The Symbolic Math Toolbox contains
functions for symbolic algebra, exact linear
algebra, variable precision arithmetic,
equation solving, and special mathematical
functions. Its underlying computational
engine is the kernel of Maple. The Extended
Symbolic Math Toolbox augments the
functionality to include Maple
programming features and specialized
libraries.
System Identification Toolbox
• This is a toolbox for parametric modeling.
Identified models are in transfer function
form (either z transform or Laplace
transform) and state-space form
Frequency Domain System
Identification Toolbox
• This toolbox contains tools for accurate
modeling of linear systems with or without
delay. The models are transfer functions in
s-domain or in z-domain. The procedures
include excitation signal design, data
preprocessing, parameter estimation,
graphical presentation of results, and model
validation (tests, uncertainty bounds,
modelling errors).
Reading Data from files
• MATLAB supports reading an entire file and creating a matrix of
the data with one statement.
>> load mydata.dat; % loads file into matrix.
% The matrix may be a scalar, a vector, or a
% matrix with multiple rows and columns. The
% matrix will be named mydata.
>> size (mydata) % size will return the number
% of rows and number of
% columns in the matrix
>> length (myvector) % length will return the total
% no. of elements in myvector
Plotting with MATLAB

• MATLAB will plot one vector vs. another. The first one will be
treated as the abscissa (or x) vector and the second as the
ordinate (or y) vector. The vectors have to be the same length.
• MATLAB will also plot a vector vs. its own index. The index
will be treated as the abscissa vector. Given a vector “time” and
a vector “dist” we could say:
>> plot (time, dist) % plotting versus time
>> plot (dist) % plotting versus index
Plotting with MATLAB

• There are commands in MATLAB to "annotate" a plot to put on


axis labels, titles, and legends. For example:
>> % To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')

>> % To put a title on the plot, we would use:


>> title ('Title of my plot')
Plotting with MATLAB

• Vectors may be extracted from matrices. Normally, we wish to


plot one column vs. another. If we have a matrix “mydata” with
two columns, we can obtain the columns as a vectors with the
assignments as follows:

>> first_vector = mydata ( : , 1) ; % First column


>> second_vector = mydata ( : , 2) ; % Second one
>>% and we can plot the data
>> plot ( first_vector , second_vector )
Simulink

A Simple Intro
Square-Wave

Feedback-Block realization
Signal
Generator
Plots
Laplace form of Transfer Function
Rectified Sine
Outputing Data to MATLAB
Step Source Plots
MATLAB Workspace
» time

time =

time: []
signals: [1x1 struct]
blockName: 'step/To
Workspace'

» output

output =

time: []
Higher Order Transfer Function
MATLAB CONSOLE

» num = [10 5 1 0];


» den = [100 1 20 4 1 1];
» H = TF(num, den)

Transfer function:
10 s^3 + 5 s^2 + s
--------------------------------------
100 s^5 + s^4 + 20 s^3 + 4 s^2 + s + 1

» bode(H)
» nyquist(H)
Bode
Bode Diagrams

From: U(1)
20

0
Phase (deg); Magnitude (dB)

-20

-40

-60

200

150
To: Y(1)

100

50
10-3 10-2 10-1 100 101

Frequency (rad/sec)
Nyquist
Nyquist Diagrams
From: U(1)
1.5

0.5
Imaginary Axis
To: Y(1)

-0.5

-1

-1.5
-2.5 -2 -1.5 -1 -0.5 0

Real Axis
Output Data to MATLAB

Anda mungkin juga menyukai