Matlab Intro 1
Matlab Intro 1
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.
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
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.
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.
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.
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.
Simulink
A Simple Intro
Square-Wave
Feedback-Block realization
Signal Generator
Plots
Rectified Sine
MATLAB Workspace
time time = time: [] signals: [1x1 struct] blockName: 'step/To Workspace' output output = time: []
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
To: Y(1)
150
100
50 10-3
10-2
10-1
100
101
Frequency (rad/sec)
Nyquist
Nyquist Diagrams
From: U(1) 1.5 1
0.5
Imaginary Axis
To: Y(1)
-0.5
-1
-1.5 -2.5
-2
-1.5
-1
-0.5
Real Axis