Upload
antony-real
View
230
Download
0
Embed Size (px)
Citation preview
7/25/2019 Base Class PPT
1/25
Theimageca nnotbe displayed.Your computer may nothave enough memory to open theimage,o r theimagemay havebeen corrupted.Restartyour computer,and then open thefileagain .Ifthe red x stillappears,yo u may haveto deletetheimage and then insertitag ain.
BASICS OF
7/25/2019 Base Class PPT
2/25
Introduction
MatLab : MatrixLaboratoryDr.CleveMolder
MATLABis
an
interpreted
language
no
compilation
nee
possible)
NumericalComputationswithmatrices
WhyMatLab? UserFriendly(GUI)
Easytoworkwith
Powerfultoolsforcomplexmathematics
MatLab hasextensivedemoandtutorialstolearnbyyou
Usehelp
command
7/25/2019 Base Class PPT
3/25
Features Donthavetodeclaretype Dont
even
have
to
initialise
Dynamicmemoryallocation
Basiccomputation
unit
is
amatri
Indexingstartsfrom1insteadof
Great
visualisation
capabilities Loadsofbuiltinfunctions Easytolearnandsimpletouse
7/25/2019 Base Class PPT
4/25
TheMATLABInterfaces
Workspace
Command History
7/25/2019 Base Class PPT
5/25
CreatingVariables
Itcan
be
any
string
of
upper
and
lower
cas
ongwithnumbersandunderscoresbutitnwithaletter
Reservednames
are
IF,
WHILE,
ELSE,
ENDtc.
Namesarecasesensitive
Thevariables
are
1x1
matrices
with
double
[Datatypes..]
>>a=12;
%variable
ais
assigned
12
N
7/25/2019 Base Class PPT
6/25
SingleValues
Singletons Toassignavaluetoavariableusetheequals
ymbol=
>>A=32
Tofind
out
the
value
of
avariable
simply
type
thenamein
Thevalueoftwovariablescanbeaddedtogether,andtheresultdisplayed
>>
A
=
10
>>
A
+
A
ortheresultcanbestoredinanother
variable>>A=10
>>
B
=
A
+
A
7/25/2019 Base Class PPT
7/25
NumericTypes
Numeric classes includes signed and unsigned integers, and
ion and double-precision floating-point numbers
By default MATLAB stores all numeric values as double-prec
point
7/25/2019 Base Class PPT
8/25
Vectors
Avector
is
alist
of
numbers
Usesquarebrackets[]tocontainthenumbers
Tocreatearowvectoruse,toseparatethecontent
Tocreateacolumnvectoruse;toseparatethecontent
7/25/2019 Base Class PPT
9/25
Arowvectorcanbeconvertedintoacolumnvectorbyusingt
transposeoperator
o Max(),min();
max/mi
o Std(),var();standarddeviatio
o Sum(),prod(); sum/pr
o
Sort();
sort
in
ao Mean(),median();
o linspace(a,b,n);
Try this ??
Vectors
7/25/2019 Base Class PPT
10/25
The:operator
VERY
important
operator
in
Matlab Meansto
>>1:10
ans=
1
2
3
4
5
6
7
8
9
10>>1:2:10
ans=
1 3 5 7 9
10
Try
>>
>>
7/25/2019 Base Class PPT
11/25
Matrices
Dontneedtoinitialisetype,ordimensions>>A=[321;510;217]
A=
3 2 1
5 1 0
2
1
7>>
Listthenumbersusing,toseparateeachcolumnand
then;todefineanewrow
11
square brackets to define matrices
semicolon for next row in matrix
7/25/2019 Base Class PPT
12/25
MatrixIndex
Thematrixindicesbeginfrom1(not0 (asinC))
The
matrix
indices
must
be
positive
integerGiven:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)Error: ??? Index exceeds matrix dimensions.
>> A
ans =
3
6
2
5
87
3
2
3
7/25/2019 Base Class PPT
13/25
ChangingMatrix
Rows
or
Colum
To change all of the values in a row or column to use
>> results :, 3) = 0 >> results :, 5) = results :, 3
>> results 3, :) = [10, 1
>> results :, 3) = [1; 1;
Try this ??
7/25/2019 Base Class PPT
14/25
ManipulatingMatrices
>>A' %transpose
>>B*A
%
matrix
multiplication>>B.*A%elementbyelementmultiplication
>>B/A %matrixdivision
>>B./A %
element
by
element
divisio>>[BA] %Joinmatrices(horizontally
>>[B;A] %Joinmatrices(vertically)
14
Create matrices A and B and try out the thein this slide
Enter maB into thMatlabworkspac
7/25/2019 Base Class PPT
15/25
MatricesOperationsGiven A and B:
Addition Subtraction Product Transpose
>>>>>
7/25/2019 Base Class PPT
16/25
Theuseof. ElementOperatio
K= x^2Erorr:??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:??? Error using ==> mtimes Inner matrix dimensions must agree.
A = [1 2 3; 5 1 4; 3 2 1]A =1 2 35 1 4
3 2 -1
y = A(3 ,:)
y=3 4 -1
b = x .* y
b=3 8 -
3
c = x . / y
c=0.33 0.5 -
3
x = A(1,:)
x=1 2 3
7/25/2019 Base Class PPT
17/25
ConcatenationofMatrices
x=[12],y=[45],z=[00]
A=[xy] %HorizontalConcatenation
1 2 4 5
B
=
[x
;
y]
%
Vertical
concatenation
12
45
C = [x y ;z]Error:??? Error using ==> vertcat CAT arguments dimensions are notconsistent.
7/25/2019 Base Class PPT
18/25
BasicMatrixFunctions
zeros(m, n) : matrix w
ones(m, n) : matrix w
eye(m, n) : the iden
rand(m, n) : uniform
randn(m, n) : norma
magic(m) : square
have therow, colu
pascal(m) : Pascal
18
Inv (A); % inverse of A
sum(A); %summation.
Rank(A); % rank of matrix A
A %transpose of A
Det (A) %determinant
dot(a,b) %dot product of two vectors
Cross(a,b) %cross product of two vectors
V= eig (A) % eigenvalue vector of A
Size(A): % return [m n]
Length(A): % length of a vector
max(size(A));
B = A(2:4,3:5); % B is the subset of A
A(:, 2)=[]; % Delete second column
Creating Ma
7/25/2019 Base Class PPT
19/25
Operators(relational,logica
==Equalto
~=Not
equal
to
Strictly
greater
=Greater
than
equal
to
&Andoperator
|Or
operator
7/25/2019 Base Class PPT
20/25
ScriptsandFunctions
There are two kinds of Mfiles:
Scripts, which do not accept input argumenttput arguments. They operate on data in the
Functions, which can accept input argumen
output arguments. Internal variables are locion.
7/25/2019 Base Class PPT
21/25
FlowControl
if
for
while
break
C l S
7/25/2019 Base Class PPT
22/25
ControlStructures
If
Statement
Syntax
if
(Condition_1)Matlab Commandselseif (Condition_2)
Matlab Commandselseif (Condition_3)Matlab Commands
else Matlab Commandsend
Some Dummy E
if ((a>3) & (b==5))
Some Matlab Comend
if (a
7/25/2019 Base Class PPT
23/25
Control Structures
Forloopsyntax
fori=Index_Array
Matlab Commandsend
Some Dummy
for i=1:100
Some Matlab Cend
for j=1:3:200
Some Matlab C
end
for m=13:-0.2:-21
Some Matlab C
end
for k=[0.1 0.3 -13
Some Matlab C
end
7/25/2019 Base Class PPT
24/25
ControlStructures
While
Loop
Syntax
while(condition)
Matlab Commands
end
Dummy Exam
while ((a>3) & (
Some Matlab
end
7/25/2019 Base Class PPT
25/25
VisualizationandGraphics
plot(x,y),plot(x,sin(x)) plot1Dfunction
figure,figure(k)
open
anew
figure
holdon,holdoffrefreshing
mesh(x_ax,y_ax,z_mat)
viewsurface
contour(z_mat) viewzastop.map
subplot(3,1,2)
locateseveral
plots
in
figu
axis([xmin xmax ymin ymax])changeaxe
title(figuretitle)
add
title
to
figure