01 Matlab Basics
01 Matlab Basics
Matlab Basics
What is Matlab?
Matlab = Matrix Laboratory
y The Matlab Program implement the Matlab
programming language and provide extensive
lib
library of prdefined
f d fi d functions that make technical
f ti th t k t h i l
prograaming more efficient
y Matlab is a huge Program.
1
What is Matlab?
Matlab = Matrix Laboratory
C
Capable
bl off solving
l i any technical
t h i l problems
bl
y Examples:
Examples:
¾ Matrix computations and linear algebra
¾ Solving nonlinear equations
¾ Numerical solution of differential equations
¾ Mathematical optimization
¾ Statistics and data analysis
¾ Signal processing
¾ Modelling of dynamical systems
¾ Solving partial differential equations
¾ Simulation of engineering systems
Introduction to MATLAB
The MATLAB Environment includes
y Graphical user interface
y MATLAB desktop and Command Window
y A command history
y An editor and debugger, and
y A Browser for viewing help, the workspace, files,
and the search path.
2
Matlab Desktop
Launch Pad
>>
History Command
Window
Matlab Desktop
Workspace
Command
Wi d
Window
Current
Directory
3
MATLAB shortcuts
New Open files SIMULINK Help
file
The Matlab Environment
y Matlab is an interpreted language
y Commands are typed into the COMMAND Window and executed
immediately
y Variables are allocated in memory as soon as they are first used in an
expression
y Commands must be re‐entered to be re‐executed
y All variables created in the Command Window are in what is
called the Base Workspace
y Variables can be reassigned new values as needed
y Variables can be selectively cleared from the workspace
y The Workspace can be saved to a data file
y File extension is .mat (ex: mydata.mat)
y File is in binary and essentially unreadable by humans
y .mat files can be reloaded back into the Matlab Workspace
4
Matlab Help
y You can’t possibly learn everything there is to know about
MATLAB, … and you don’t need to.
y It is crucial to develop the ability to augment your knowledge in
MATLAB toward accomplishing a given task.
Matlab Help
10
5
Matlab Help
y Click the tab in the navigation pane labeled Search.
y Then type into the Search field the name ezplot.
11
The Help System
S
Search for appropriate function
h f i t f ti
y >> lookfor keyword
Rapid help with syntax and function definition
y >> help function
An advanced hyperlinked help system is launched by
y >> helpdesk
12
6
Matlab Editor
Access to
commands
Color keyed
text with auto
indents
tabbed sheets
for other files
being edited
13
Expressions
Like most other programming languages, MATLAB
provides mathematical expressions,
expressions but unlike most
programming languages, these expressions involve entire
matrices.
The building blocks of expressions are
y Numbers
y Variables
V i bl
y Functions
y Operators
14
7
Numbers
MATLAB uses conventional decimal notation for
numbers For example:
numbers.
3, -99, 0.0001, 3.14159, 1.60210e-20, 6.02252e23, 1i, 1j,
3e5i
All numbers are stored using the long format specified by
the IEEE floating-point standard (i.e. 8 bytes). Floating-
point numbers have a finite precision of roughly 16
significant decimal digits and a finite range of roughly 10-
308 to 10+308.
15
Variables
When MATLAB encounters a new variable name, it
automatically creates the variable and allocates the
appropriate amount of storage. MATLAB does not require
any type declarations or dimension statements.
Variable names consist of a letter, followed by any number
of letters, digits, or underscores. MATLAB uses only the
first 31 characters of a variable name. MATLAB is case
sensitive (i.e. A and a are not the same variable).
16
8
Functions
MATLAB provides a large number of standard
elementary
l t mathematical
th ti l functions,
f ti suchh as: abs,
b sqrt,
t
exp, and sin. MATLAB handles complex numbers thus
taking the square root or logarithm of a negative
number is not an error.
For a list of the elementary functions:
help elfun
help specfun
help elmat
17
Operators
Expressions use familiar arithmetic operators and
precedence rules
precedence rules.
+ Addition
‐ Subtraction
* Multiplication
/ Division
\ Left division
^ Power
` Complex conjugate transpose
( ) Specify evaluation order
18
9
MATLAB as a Calculator
Command window
>> 39*4.4+5
The result.
19
MATLAB Assignment
z Variable names:
z Starts with a letter
» A=2.3
A =
The MATLAB command
2.3000
This is the result of the
MATLAB statement
20
10
Basic Syntax
y Common Built-in Math Functions
y sin(x) sine
y cos
cos(x)
(x) cosine
y tan(x) tangent
y asin
asin(x)
(x) sin inverse
y acos
acos(x)
(x) cosine inverse
y atan
atan(x)
(x) tangent inverse
y sinh
sinh(x)
(x) sin hyperbolic
y cosh
cosh(x)
(x) cosine hyperbolic
y tanh
tanh(x)
(x) tangent hyperbolic
21
Basic Syntax
y Common Built-in Math Functions
bessel , gamma , erf , legendre
y exp(x) exponential
y log(x) base e logarithm
y log10
log10(x)
(x) base 10 logarithm
y sqrt
sqrt(x)
(x) square root
y abs(x) absolute value
y round(x) round towards nearest integer.
» round(2.58) = 3 » round(-2.58)= -3
y fix(x)
fi ( ) roundd towards
t d zero
» fix(2.58)= 2 » fix(-2.58)= -2
other functions:
y ceil(x)
(x) round towards (+ infinity)
» ceil(2.58) = 3 » ceil(-2.58) = -2
y floor(x)
(x) round towards (- infinity)
floor(-2.58)= -3 » floor(2.58) = 2 22
11
Useful Constants
y Inf infinity
y NaN Not and number (div by zero)
y eps machine epsilon
y ans most recent unassigned answer
y pi 3.14159….
y i and j Matlab supports imaginary
numbers!
23
MATLAB variable
y Predefined variables:
which can be used at any time:
i= sqrt(-1) Use semi‐
j= sqrt(-1) colon after
pi= 3.1416... commands for
“quiet”
quiet
y complex number: execution
y= 2*(1+4*j)
yields: y = 2.0000 + 8.0000i
24
12
Interactive Calculations
Matlab is interactive, no need to declare variables
y >> 2+3*4/2 /
y >> a=5e-3; b=1; a+b
Use ‘;’ at the end of command if you don’t want to see the result of that command
Most elementary functions and constants are already defined
y >> cos(pi)
y >> abs(1+i)
y >> sin(pi)
Complex numbers
>> c1=1-2i
c1=
1
1.0000 – 2.0000i
>> c2=6+sin(.5)*j
c2=
6.0000 + 0.4794i
>>M=abs(c1)
>>theta=angle(c1)
>>a=real(c1)
>> b=imag(c1)
26
13
Syntax Errors
y All errors are given in the Command Window
Command Window in red
y Nature of error is explained
27
Interactive Calculations
Example1:
14
Interactive Calculations
Example 2:
Interactive Calculations
Example 3:
>> x = 1 + 2+3+4+5+6+7+8+9+10+11+12
x=
78
>> x = 1 + 2 + 3 + 4 + ...
5 + 6 + 7 + 8 + 9 + 10 + 11 + 12
x=
78
30
15
Floating point numbers in Matlab
y IEEE Standard for double precision numbers
Each format contains three components:
Sign bit Exponent Fraction
s e f
1 2 12 13 64
Format
Control display format for output
All computations in MATLAB are done in double precision.
Format may be used to switch between different output display formats as follows:
y >> format Default. Same as Short.
y >> format short Scaled fixed point format with 5 digits.
y >> format long Scaled fixed point format with 15 digits.
y >> format short e Floating point format with 5 digits.
y >> format long e Floating point format with 15 digits.
y >> format short g Best of fixed or floating point format with 5 digits.
y >> format long g Best of fixed or floating point format with 15 digits.
y >> format hex Hexadecimal format.
f h H d i l f
y >> format + The symbols +, ‐ and blank are printed (Imaginary parts are ignored)
y >> format bank Fixed format for dollars and cents.
y >> format rat Approximation by ratio of small integers.
y >> format compact Suppress extra line‐feeds.
y >> format loose Puts the extra line‐feeds back in.
32
16
Who and Whos
List variables in the workspace
y >> who a*
finds all variables in the current workspace that start with a.
33
Save & Load
Variables can be stored on file
p
saves all workspace variables to the binaryy "MAT-file"
y>> save filename
34
17
Matlab Files
y File types of MATLAB
y .m file
y .fig files
y .mat files
y Basic Commands in command window
y save <filename>
y load <filename>
35
Clear
Clear workspace:
Remove variables and functions, freeing up system memory
y>> clear
y>> clear
l allll
Clear Command Window :
y >> clc
As an alternative to the clc function, use Clear Command
Window in the MATLAB desktop Edit menu.
Move the cursor to the upper left corner of the Command
Window
y >>home
h
Clear Command History:
y select an entry, or use Ctrl+A to select all entries. Then right-click
and select one of the delete options from the context menu.
y Another way to clear the entire history is by selecting Clear
Command History from the Edit menu.
36
18
Dir
lists the files in the current working directory.
y >> dir
lists the specified files. The name argument can be a
pathname, filename, or can include both. You can use
absolute and relative pathnames and wildcards (*).
y >> dir filename
y >> dir s
>> dir s*
lists all the M‐files
y >> dir *.m
y >>ls
37
Path
>> path
View or change the MATLAB directory search path
View or change the MATLAB directory search path.
>> pwd
Display current directory.
y >> cd
prints out the current directory.
y >> cd ..
moves to the directory above the current one.
y >> cd work
Changes the current working directory to demos
38
19
What
List MATLAB specific files in current directory
y >> what
lists the m, mat, mex, mdl, and p-files and the class directories
that reside in the current working directory.
y >> what dirname
lists more information about each variable lists the files in
directory dirname on the MATLAB search path. It is not
necessary to enter the full pathname of the directory. The last
component, or last two components, is sufficient.
39
Which
Locate functions and files
y >> date
y >> clock
Year, Month, Day, Hour, Minute, Seconds
y >> datestr(now)
40
20