You are on page 1of 66

Appearance of the Matlab main

windowView or change the current working directory

Work
space
Current window
directo Prompt: means the
ry Matlab is ready to
window accept command
following the
prompt. Command
history
window

Command window :
Enter the Matlab statements after the
prompt “>>”
Undocking of windows

Click the “undock” button, and


the command window will be
released from the group of
window.
Control of the
appearance

Uncheck the option and the corresponding window will be


turned off.
Another appearance of the Matlab
window
•If start the Matlab with command option “-nojvm ” (means: no java virtual
machine)
Advantage:
üThe Matlab starts at a faster speed.
üThe Matlab consumes less memory.

To leave Matlab, either enter


“exit” or “quit” or click on the
X at the upper right corner of
the Matlab window.
Some useful commands
MATLAB is case-sensitive - it differentiates between the
lowercase and uppercase letters. Thus 'm' and 'M' are different
variables. The built-in commands and function in MATLAB are
typed in lowercase letters.
•pwd/cd •help/helpwin/helpdesk/doc
•dir/mkdir/rmdir •lookfor
•delete/copyfile •dbtype
•who/whos •demo
•what/which
•clear/clc

Only the most fundamental usage of the commands will be presented


in the following slides. Consult with the online help for more
detail. For example:
>>help who
Useful command –
‘pwd’/’cd’
lpwd (present working directory): the directory of the folder in which
the user is.
lcd ‘ new directory’ : change the working directory to the new directory
specified.
üThe string representing the new directory is enclosed inside
the ‘’ sign
Useful command – ‘dir’,
’mkdir’/‘rmdir’
lCommand ‘dir’ is used to show the files under the specified
folder
lCommand ‘mkdir’ is used to create a new folder.
lCommand ‘rmdir’ is used to delete an empty folder.
Useful command –
‘delete’/’copyfile’
lCommand ‘delete’ is used to delete a file.
lCommand ‘copyfile’ is used to copy file.
Useful command –
‘who’/’whos’
l‘wh/whos: check the variables existed in the memory.

Variables may
also be observed
here.

Each variable is stored as a matrix in the


memory
Useful command – ‘what’
lwhat: find the Matlab source code files exist in the folder
specified.

By default, the current folder

Find source files in the specified directory


Useful command –
lwhich:
‘which’
find the full directory of the specified file.
Useful command –
‘clear’/’clc’
lCommand ‘clear’ is used to clear variable from the workspace (memory).
lCommand ‘clc’ is to clean the work space.

The command window is filled with old


information, and there are variables
existed in the workspace memory. Clean command window
Useful command –
‘clear’/’clc’
lCommand ‘clear’ is used to clear variable from the workspace (memory).
lCommand ‘clc’ is to clean the work space.

>> clear
a

Variable “a” is
gone.

>> clear
all

All the variable are


gone.
Useful command – ‘help’
lFor the command “help” alone, the Matlab will display all the
toolboxes that is available from the current system.
Useful command – ‘help’
lhelp + ‘toolbox name’: Matlab displays all the fuction calls available
in the specified boolbox.
Useful command – ‘help’
lhelp + ‘command/function name’: Matlab will display a brief
introduction of the command/function designated.
Useful command –
‘helpwin’
lCommand “helpwin” will open the online help window.
Useful command –
‘helpdesk’
lCommand “helpwin”, “helpdesk” and “doc” will open the online help
window.
Useful command – ‘doc’
lCommand “helpwin” will open the online help window.
Useful command –
‘lookfor’
llookfor + ‘keyword’: Matlab will display all the command/function that
is possible relevant to the keyword.

Matlab shows all the


commands/functions that is
relevant to the keyword
designated.
Useful command –
‘dbtype’
ldbtype + ‘function name’: Matlab will provide the source code of the
function. This command does not work for the “built - in” functions
of Matlab.
Useful command –
‘dbtype’

Source code of
the command
“polar”
Useful command –
‘demo’
lCommand “demo” will open the demonstration window, from where the
user may find help on topics in animation.
Adding path to Matlab
Whenever Matlab receives a command, it checks in the
following order whether the command is:
•A variable
•A built-in function
•A Matlab source code filename in the current folder
•A Matlab source code filename in other folders
listed in the environment variable “path”.

The user may want to add directory of more folders,
where there are Matlab source code file generated
by the user, to the Matlab environment variable
“path” such that the Matlab has access to the
source code files in those folders for later use.
Adding path to Matlab
Use command “path” to view the path of the folders
Adding path to Matlab
To add path to Matlab for source file search, the
user may:
•Use command “ path(path, ‘path to append’) ”
Adding path to Matlab
To add path to Matlab for source file search, the
user may change from the file menu:
•File  Set Path
Types of commonly used data and
their derivatives
lCommonly used type of data
ØReal numbers: pi, 1.0, sqrt(2), …
uComplex numbers: 4+i, 5+3i,
ØCharacter / string: ‘YZU’, ‘a’
lEach data is stored as a matrix in Matlab.

lA array / matrix is a group of the same type data, the maximum


dimension allowed in Matlab is 7 , a vector is a matrix with
either one row or one column.
lThere are other type of data objects, such as ‘structure’ and
‘cell’, which we will consider later this semester.
Elementary Mathematical
operations

Without assigning any variable


name for the result of a
statement, Matlab stores the
result with variable “ans”.

•Enter the statements next to the prompt “>>”,


followed by clicking the “enter” button.
•To turn off the display of statement result, add
a semicolon ‘;’ at end of the statement.
Elementary Mathematical
operations
lExample s of built-in constants:
Øpi  
Øi or j 
−1
lCommonly used elementary mathematic functions may be found with
command:
help elfun
Example: “sin”, “cos”, “tan”, “sind”, “cosd”, “tand’” and “exp”
Elementary Mathematical
operations
•Try the other trigonometric functions – for variable in both
radian and degree unit.
•Question: What causes the following “inaccuracy” and how to
correct it?
Elementary Mathematical
operations
•May put more than one statements in a line – use comma or semi-
colon to separate the statements. Matlab echoes the result of a
statement that ends with a comma, but not for a statement that
ends with a semicolon.
•Space between statements does not change the statements.
Variables
To keep the result of a statement for later use, the
user may assign a name to the result  variable.
Rules for variable naming:
ØName of a variable must begin with a “character/letter ”, say
a, b, a17, E_in, My_var, and the maximum length of variable
name is 31 characters . For variable with name longer than
31 characters, Matlab keeps the first 31 figures and ignore
the rest .
ØAcceptable ingredients for the variable name are:
character/letters, numbers, ‘_’.
ØVariable name is case sensitive: ‘a’ is different from ‘A’.
ØNo need to declare the data type of variable.
ØNo “space” is allowed at middle of any variable name, such
as: “is number”.
ØVariables could be “cleared” by using command “clear
variable name”.
Variables
Matlab is case sensitive, therefore “a” is different from “A”.

No space is allowable in middle of a variable name.


Variables
Variable ‘cSine_1’ has been removed from
memory

Observed from response of command “whos”, without additional


declaration, each real variable is stored as “double” data type and
occupies 8 bytes.
Variables
Long statement may be continued with more lines  append
“…” at end of each line to continue the command .

A space is placed between “7” and


“…”.

This technique is commonly used in script files and


function calls programmed by the users.
Vector and matrix
ØVector and matrix are used to store and represent a group
of data of the same type. (A vector is a matrix of either
one row or one column.)
Different assemble type
of data
ØData of different type could be stored under a
variable name  structure. (will cover this
later)
Construction of a vector
The elements may be separated with either a comma ‘ , ’ or a
space.

Construction of
vector
Construction of a vector
lConstruct a vector with command
“linspace”:
linspace(init, end, number of point)
Operation on vector
elements
>> s = [1 3 5 2]; % use [] for construction of a vector , separate the
elements with % either a comma ‘ , ’ or a space
>> t = 2*s+1
t =
3 7 11 5

lMATLAB may operate on a single element of the vector/matrix :


>> t(3) = 2 % change the value of the 3rd element to 2
t =
3 7 2 5
>> t(6) = 10 % append one more element to the end of the
vector, length % of the vector is increased by 2 to
be 6, and a ‘0’ has been % padded to be the 5th
element.
t =
3 7 2 5 0 10
Operation on vector
elements
>> t(4) = [] % delete the 4th element of the vector  length of
the vector has % been decreased to be 5
t =
3 7 2 0 10

Use command ‘length’ to


find the length (total
number of element) of
vector t.
Operation on vector
ØFind the minimum, maximum, sum, mean of a
vector
(may apply to matrix as well)
Operation on vector
ØSort element of vector (may apply to matrix
as well)
Operation on vector
ØProduct of vectors – element by element
product

The dot symbol “ . ” in front of the


operators product “*” and power
“ ^ ” indicate the operation is element-
by-element
Operation on vector – the
dot product
ØThe product of a row vector and a column
vector
Operation on vector – the outer
product
ØThe product of a column vector and a row
vector
Construction of special
vectors
lVector with elements of value ‘0’
or ‘1’:
Construction of matrix
lIn matrix construction, different rows are separated with a
semi-colon ‘ ; ’.
lElements in a row are separated with either a space or a comma “
, “.

Identity matrix is created


with command ‘eye(n)’ where
n is the rank of the
matrix.
Property of matrix
lMatlab stores elements in column-wise style

mat2(2,4) = mat2(14)
Operation on matrix – change value of
elements
ØChange value of elements in the matrix – assign value
directly or by operation on elements

Change value of an element directly.

Change value of a column of


element with operation between
columns
Operation on matrix
ØConstruction of a matrix from an existing matrix
ØErase any number of row or column of a matrix

Construct mat2 from


mat1

Erase the 5th and 6th


rows
Operation on matrix - flip
ØCommand ‘flipud’ flips the matrix upside down
ØCommand ‘fliplf’ flips the matrix left-hand to right-
hand
Operation on matrix – exchange of
rows or columns

Exchange of the
1st and the 4th
rows
Operation on matrix – information of
matrix
Øsize: find out the dimension of the matrix
Øndims: number of dimension (rank) of the matrix

mat2 is a 4x4 matrix

rank of mat2 is 2 (2
indices)
Operation on matrix –
transpose
ØTranspose of a matrix:
üAdd the apostrophe symbol “ ‘ “ after
the matrix
üUse command ‘transpose’
Operation on matrix –
transpose
Complex conjugate has
been applied upon the
elements

Elements are kept the


original value

Complex conjugate has


been applied upon the
elements

ØDifference between “ ‘ “ and command


‘transpose ’
Operation on matrix –
product
ØProduct of matrices

Common matrix product

Element-by-element product
Operation on matrix –
inverse
ØCommand “inv” calculates the inverse of
matrices
Operation on matrix – diagonal
elements
ØCommand “diag” extracts the diagonal elements of
matrices
Operation on matrix – eigenvalue
and characteristic polynomial
ØCommand “eig ( A ) ” calculates the eigenvalues of matrix A
ØCommand “round(poly(A))” returns the characteristic polynomial of
matrix A

Eigenvalues of matrix A
are the roots of the
characteristic polynomial
of matrix A
Operation on matrix – display of
characteristic polynomial
ØDisplay the characteristic polynomial with
variable ‘s’