Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment Command window, Workspace,...
-
Upload
colleen-henry -
Category
Documents
-
view
237 -
download
0
Transcript of Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment Command window, Workspace,...
Introduction to
Matlab By
E. Noura Semary
Contents
MATLAB Environment Command window, Workspace, Path window, Editor window,and
Figure window) Basic Functions: clc, clear, save, load, who, whos … Variables in MATLAB
Arrays Control flow (for – end, while – end, if – else – elseif – end, switch M-files Plotting Introduction to GUI Introduction to Image processing Toolbox
MATLAB Environment Command window
Use Menus as an alternative to typing some commands
Use of Toolbar for easy access to popular operations
Status bar
Command window
Status of Caps, Num, and Scroll locks
New M-file
Open file
Undo
New Simulink Model
Help
Cut
PasteCopy Workspace Browser
Path Browser
MATLAB Environment Workspace
Open the selected matrix
Delete the selected matrix from the workspace
Displays the total number of elements in the workspace and total size of them.
Close the workspace browser
size of the matrix Number of bytes in the matrix
Name of the matrix Type of the matrix
MATLAB Environment Workspace
Name of the matrix
Current dimension of the matrix: User can change the dimensions of the matrix by simply type the dimensions he/she want
Current Cell
Current values: User can change the values o the matrix’s element by editing in the cell.
MATLAB Environment Path browser
The current Directory
The directories in the path
The Files in the selected path.
Add a new path
Remove the selected path
MATLAB Environment Editor window
Current line number
Automatic Indenting
Aut
omat
ic C
olor
Hig
hlig
htin
g to
Dis
ting
uish
Dif
fere
nt E
lem
ents
The toolbar
Names of the opened files in the editor
New file
Save file
Copy
PrintSet/Clear break point
Step in
Continue List if functions in call stack
Open file
Cut
Paste
Help
Clear all break points
Single stepQuit debugging
MATLAB Environment Figure window
New figure
Open an exciting figure
Save the current figurePrint Zoom the plot
Rotate the plot
Start / End the plot editor mode
Draw an arrow in the plot
Type a text in the plotDraw a line in the plot
Basic Functions
Function name Function mean
clc Clear the command window
clear Clear workspace
who Show workspace
whos Show workspace in details
help Show information about any function
lookfor Search for a word in MATLAB files
save Saving workspace
load Loading saved workspace
Variables
MATLAB variables are arrays Variable must be one word
myvariable accepted my variable not accepted
Variable length is up to 31 character Variables must begin by a character MATLAB variables are case sensitive There are preserved variable names:
Variables
ans Default function output
pi Pi = 3.14
eps Very small value
inf Very large value = ∞ as the result of (1/0)
NaN When the result = 0/0
realmin Minimum real number = 10-308× 2.2251
realmax Maximum real number = 10308× 1.7977
nargin The number of input parameters
nargout The number of output parameters
Arrays : review Vector Array : (1 × n), (m ×1)
Matrix : (m × n)
Identity array
5 4 0 7
057
1 4 4 5
12 8 9 15
1 0 00 1 00 0 1
1 53 40 0
(2×4) -> (3×2) ->
(1×4) -> (3×1) ->
Arrays : review
Assume You have A = B=
A+B= A’ =
2A = =
1 3 0 2
5 4 0 7
0 1 4 3
7 4 9 8
1 4 4 5
12 8 9 15
2 6 0 4
10 8 0 14
1 53 40 02 7
a bc de f
xy
ax+bycx+dyex+fy
(m×k)(k×n)=(m×n)
Arrays : Marlab vision
m=4 m=[4]
a=3*6 m=[18]
a=[1,4,6,8,9] a=[1 4 6 8 9]
M=[2 3 5 9 1 ; 0 3 6 4 5 ; 1 10 8 6 4] M(1) 2M(6) 10M(3, 4) 6M(3, :) 1 10 8 6 4M(:, 2) 3
3
10M(1, 2:4) 3 5 9M(2, 1:2:5) 0 6 5
M= 2 3 5 9 10 3 6 4 5
1 10 8 6 4
Standard arrays
zeros zeros(3)= zeros(2,3)=
ones ones(3)= ones(2,3)=
eye eye(3)= eye(2,3)=
magic magic(3) =
rand rand(3,4) =
linspace linspace (0,4,5)= [0 1 2 3 4] linspace (1,2,3)= [1 1.5 2]
logspacelogspace (0,4,5)= [1 10 100 1000 10000]logspace (1,2,3)= [10 31.6228 100]
0 0 00 0 00 0 0
0 0 00 0 0
1 1 11 1 11 1 1
1 1 11 1 1
1 0 00 1 00 0 1
1 0 00 1 0
8 1 63 5 74 9 2
0.1389 0.6038 0.0153 0.9318
0.2028 0.2722 0.7468 0.4660 0.1987 0.1988 0.4451 0.4186
Array functions
size size(N)=[3 3] sum sum(N) = [14 9 16]
length length(N)=[3] max max(N) = [9 7 8]
det det(N)=-119 min min(N) = [1 0 3]
diag diag(N)= flipudflipud(N)=
inv fliplr fliplr(N)=
transpose (´ ) transpose(N)= rot90 rot90(N)=
find find (N ==7 ) = 5 find (N == 10 ) = []
N=1 0 34 7 59 2 8
178
9 2 84 7 51 0 3
3 0 15 7 48 2 9
3 5 80 7 21 4 9
-0.3866 -0.0504 0.1765 -0.1092 0.1597 -0.0588 0.4622 0.0168 -0.0588
1 4 90 7 23 5 8
Relational and Logical Operators
Relational Operators
Operator name Symbol Comment
eq = = equal
ne ~= not equal
lt < less than
gt > greater than
le <= less than or equal
ge >= greater than or equal
Logical Operators
and & logical and
or | logical or
not ~ logical not
xor logical exclusive or
Control Flow
for – end for variable= expression
statements
endexample:
for i=1:10 for j=1:2:10
x(i) = i*2; y=j
end end
Control Flow
while – end x=0;
while (x<5)
x=x+s;
disp(x);
end
Control Flow
if – else – elseif – enda=6; % a=7; % a=10;
if (rem(a,3)==0)
a=a*3;
elseif (rem(a,2)==0)
a=a*2;
else
a=a*10;
end
disp (a);
Control Flow
switch – case – otherwise – endx= input (‘The value of x:’);units= input (“Enter the unit of x: (Please Enter the unit
between ‘ ‘) ’);switch units
case (‘inch’,’in’)y=x*2.54 ;
case (‘feet’,’ft’)y=x*2.54*12 ;
case (‘meter’,’m’)y=x*100 ;
case (‘millimeter’,’mm’)y=x/10 ;
otherwisedisp (‘Unknown unit’);
end
Control Flow
breakfor i=1:10
if (i>5)break;
elsea=a*4.5;
endenddisp (a);
M-files
1. Scripts : Deal with the work space directly without inputs or outputs
2. Functions: Needs inputs and only the outputs are saved in the workspace.
examples:
function [output1,output2…]=function_name(input1,input2,…) function function_name(input1,input2,…)
M-files
Function or script calling is done by typing the file name.
The function file name must be the same as function name
Function name must be one word Has a maximum of 31character size Starting by a character
M-files
Assume a function: function [a,b,c]=my_function(x,y)
is called as[b]=my_function(x,y,z);[a,b,c]=my_function(u);
then b = a value x= u value
Math functions
Basicfunctions
triangular functions
Approximation functions
complex functions
conversion functions
abs cos fix abs dec2hex
sqrt sin round angle dec2bin
mean tan floor conj dec2base
power acos ceil imag hex2dec
log asin rem real bin2dec
log10 atan floor base2dec
exp csc ceil rad2deg
max sec rem deg2rad
min cot cart2sph
sort acsc cart2pol
asec pol2cart
acot sph2cart
cosh
sinh
tanh
Exercise1. write a matlab program that sum a series
begins by 2 and ends by 100 Use both for and while loops in separated m-files
2. write a switch – case program such that: if 35>=S>20 display ‘Hot Day’ if 20>=S>10 display ‘Nice Day’ if 10>=S>=0 display ‘Cold Day ‘ otherwise display ‘Out of Range’
That S is the sum of the array M, and M is: Random matrix of size 3×6 and has values between 0 and
2 A square matrix of size 3 and values between 1 and 9 A vector of equal elements =5 and size of 7 Identity matrix of size 10×10