Tải bản đầy đủ (.ppt) (35 trang)

Lecture 2 MATLAB fundamentals potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (190.95 KB, 35 trang )

Lecture 2
Lecture 2
MATLAB fundamentals
MATLAB fundamentals
Variables, Naming Rules,
Variables, Naming Rules,
Arrays (numbers, scalars, vectors, matrices),
Arrays (numbers, scalars, vectors, matrices),
Arithmetical Operations,
Arithmetical Operations,
Defining and manipulating arrays
Defining and manipulating arrays
© 2007 Daniel Valentine. All rights reserved. Published by
Elsevier.
Variables
Variables

What are variables?
What are variables?

You name the
You name the
variables
variables
(as the programmer)
(as the programmer)
and assign them numerical values.
and assign them numerical values.
Variable Naming Rules
Variable Naming Rules


Must begin with a LETTER
Must begin with a LETTER

May only contain letters, numbers and
May only contain letters, numbers and
underscores ( _ )
underscores ( _ )

No spaces or punctuation marks allowed!
No spaces or punctuation marks allowed!

Only the first 63 characters are significant;
Only the first 63 characters are significant;
beyond that the names are truncated.
beyond that the names are truncated.

Case sensitive
Case sensitive
(e.g. the variables
(e.g. the variables
a
a
and
and
A
A


are
are

not
not
the same
the same
)
)
Which variable names are valid?
Which variable names are valid?

12oclockRock
12oclockRock

tertiarySector
tertiarySector

blue cows
blue cows

Eiffel65
Eiffel65

red_bananas
red_bananas

This_Variable_Name_Is_Quite_Possibly_Too_Lon
This_Variable_Name_Is_Quite_Possibly_Too_Lon
g_To_Be_Considered_
g_To_Be_Considered_
Good_Practice_However_I
Good_Practice_However_I

t_Will_Work % (the green part is not part of the
t_Will_Work % (the green part is not part of the
recognized name)
recognized name)
Variable Naming Conventions
Variable Naming Conventions

There are different ways to name variables. The
There are different ways to name variables. The
following illustrate some of the conventions used:
following illustrate some of the conventions used:

lowerCamelCase
lowerCamelCase

UpperCamelCase
UpperCamelCase

underscore_convention
underscore_convention

If a variable is a constant, some programmers use all
If a variable is a constant, some programmers use all
caps:
caps:

CONSTANT
CONSTANT

It does not matter which convention you choose to work

It does not matter which convention you choose to work
with; it is up to you.
with; it is up to you.
In MATLAB, a
In MATLAB, a
variable
variable
is stored as an array of
is stored as an array of
numbers. When appropriate, it is interpreted as a
numbers. When appropriate, it is interpreted as a
scalar
scalar
,
,
vector
vector
or
or
matrix
matrix
.
.
The size of an array is specified by the number of
The size of an array is specified by the number of
rows and the number of columns in the array, with
rows and the number of columns in the array, with
the number of rows indicated first.
the number of rows indicated first.

Variables as Arrays
Variables as Arrays
scalar
scalar
1
1
×
×
1
1
vector
vector
n
n
×
×
1 or 1
1 or 1
×
×
n
n
matrix
matrix
n
n
×
×
m
m


Scalars
Scalars
are 1×1 arrays.
are 1×1 arrays.

They contain a single value, for example:
They contain a single value, for example:
Scalars
Scalars
Vectors
Vectors

A
A
vector
vector
is a list of numbers expressed as a 1
is a list of numbers expressed as a 1
dimensional array.
dimensional array.

A vector can be n×1 or 1×n.
A vector can be n×1 or 1×n.

Columns are separated by
Columns are separated by
commas (or spaces)
commas (or spaces)
:

:

Rows are separated by
Rows are separated by
semicolons
semicolons
:
:
Matrices
Matrices

A
A
matrix
matrix
is a two
is a two
dimensional array of
dimensional array of
numbers.
numbers.

For example, this is a
For example, this is a
4×3 matrix:
4×3 matrix:
1
1
2
2

3
3
1
1
3.0
3.0
1.8
1.8
3.6
3.6
2
2
4.6
4.6
-2.0
-2.0
21.3
21.3
3
3
0.0
0.0
-6.1
-6.1
12.8
12.8
4
4
2.3
2.3

0.3
0.3
-6.1
-6.1
Columns
Columns
Rows
Rows
Indexed-location of numbers in an
Indexed-location of numbers in an
array
array

Each item in an array
Each item in an array
is located in the
is located in the
(row, column)
(row, column)
.
.
1
1
2
2
3
3
1
1
3.0

3.0
1.8
1.8
3.6
3.6
2
2
4.6
4.6
-2.0
-2.0
21.3
21.3
3
3
0.0
0.0
-6.1
-6.1
12.8
12.8
4
4
2.3
2.3
0.3
0.3
-6.1
-6.1
Columns

Columns
Rows
Rows

ans =
ans =


21.3000
21.3000

Enter the following into MATLAB:
Enter the following into MATLAB:

Scalar:
Scalar:

Vectors:
Vectors:

Matrix:
Matrix:
Examples
Examples
Examples
Examples

Enter (input) the following matrix into MATLAB:
Enter (input) the following matrix into MATLAB:
-7

-7
21
21
6
6
2
2
32
32
0
0
-5
-5
0
0
-18.5
-18.5
whiteRabbit =
whiteRabbit =
Scalar Operations
Scalar Operations
Operation
Operation
Algebraic
Algebraic
Syntax
Syntax
MATLAB
MATLAB
Syntax

Syntax
Addition
Addition
a + b
a + b
a + b
a + b
Subtraction
Subtraction
a - b
a - b
a – b
a – b
Multiplication
Multiplication
a × b
a × b
a .* b
a .* b
Division
Division
a ÷ b
a ÷ b
a ./ b
a ./ b
Exponentiation
Exponentiation
a
a
b

b
a .^ b
a .^ b
Array Operations
Array Operations

Arrays of numbers in MATLAB can be interpreted as
Arrays of numbers in MATLAB can be interpreted as
vectors and matrices if vector or matrix algebra is to be
vectors and matrices if vector or matrix algebra is to be
applied. Recall that matrices are mathematical objects
applied. Recall that matrices are mathematical objects
that can be multiplied by the rules of matrices. To do
that can be multiplied by the rules of matrices. To do
matrix multiplication, you need to use the standard
matrix multiplication, you need to use the standard
*
*
,
,
/
/
,
,


and
and
^
^

operators [without the preceding
operators [without the preceding
.
.
(dot)]. They
(dot)]. They
are
are
not
not


for
for
array multiplication, division and
array multiplication, division and
exponentiation
exponentiation
.
.

To deal with arrays on an
To deal with arrays on an
element-by-element
element-by-element
level we
level we
need to use the following
need to use the following
array

array
or
or
dot-operators
dot-operators
:
:
.*
.*
,
,


./
./
and
and


.^
.^
Array operations & dot-operators
Array operations & dot-operators

Because scalars are equivalent to a 1×1
Because scalars are equivalent to a 1×1
array, you can either use the standard or
array, you can either use the standard or
the
the

dot-operators
dot-operators
when doing
when doing
multiplication, division and exponentiation
multiplication, division and exponentiation
of scalars (i.e., of single numbers).
of scalars (i.e., of single numbers).

It is okay for you to always use the dot-
It is okay for you to always use the dot-
operators, unless you intend to perform
operators, unless you intend to perform
vector or matrix multiplication or division.
vector or matrix multiplication or division.
.*
.*


,
,


./
./
and
and


.^

.^

Example:
Example:
z = x .* y
z = x .* y


results in [10, 6; 21, 32]; this is
results in [10, 6; 21, 32]; this is
array
array
multiplication
multiplication
z = x * y
z = x * y
results in [17, 20; 43, 50]; this is
results in [17, 20; 43, 50]; this is
matrix
matrix
multiplication
multiplication
So, do NOT forget the dot when doing array
So, do NOT forget the dot when doing array
operations! (.* ./ .^)
operations! (.* ./ .^)
Array vs. Matrix Operations
Array vs. Matrix Operations
Hierarchy of Operations
Hierarchy of Operations

Just like in mathematics the operations are done in the
Just like in mathematics the operations are done in the
following order:
following order:
Left to right
Left to right
doing what is in
doing what is in
P
P
arentheses &
arentheses &
E
E
xponents first, followed by
xponents first, followed by




M
M
ultiplication &
ultiplication &
D
D
ivision, and then
ivision, and then



A
A
ddition &
ddition &
S
S
ubtraction last.
ubtraction last.
An example:
An example:
c = 2+3^2+1/
c = 2+3^2+1/
(
(
1+2
1+2
)
)
1
1
st
st
c = 2+3^2+1/
c = 2+3^2+1/
3
3
c = 2+
c = 2+
3
3

^
^
2
2
+1/(1+2)
+1/(1+2)
2
2
nd
nd
c = 2+
c = 2+
9
9
+1/3
+1/3
c = 2+3^2+
c = 2+3^2+
1
1
/
/
(1+2)
(1+2)
3
3
rd
rd
c = 2+9+
c = 2+9+

0.33333
0.33333
c =
c =
2
2
+
+
3^2
3^2
+1/(1+2)
+1/(1+2)
4
4
th
th
c =
c =
11
11
+0.33333
+0.33333
c =
c =
2+3^2
2+3^2
+
+
1/(1+2)
1/(1+2)

5
5
th
th
c =
c =
11.33333
11.33333
Hands-on
Hands-on

Enter these two arrays into MATLAB:
Enter these two arrays into MATLAB:

Multiply, element-by-element, a × b.
Multiply, element-by-element, a × b.

Since this is an array operation, the
Since this is an array operation, the
.*
.*


multiplication operation is implied by the
multiplication operation is implied by the
request.
request.
a =
a =



10 5 5
10 5 5


2 9 0
2 9 0


6 8 8
6 8 8
b =
b =


1 0 2
1 0 2


0 0 0
0 0 0


1 1 0
1 1 0
Defining & manipulating arrays
Defining & manipulating arrays

All variables in MATLAB are arrays!
All variables in MATLAB are arrays!


Single number array & scalar: 1 × 1
Single number array & scalar: 1 × 1

Row array & row vector:
Row array & row vector:
1 × n
1 × n

Column array & column vector: n x 1
Column array & column vector: n x 1

Array of n rows x m columns & Matrix: n × m
Array of n rows x m columns & Matrix: n × m

Naming rules
Naming rules

Indexed by (row, column)
Indexed by (row, column)

Remark:
Remark:
vectors and matrices are special
vectors and matrices are special
mathematical objects, arrays are lists or
mathematical objects, arrays are lists or
tables of numbers.
tables of numbers.
The equal sign assigns

The equal sign assigns

Consider the command lines:
Consider the command lines:
>>
>>
ax = 5;
ax = 5;
>>
>>
bx = [1 2];
bx = [1 2];
>>
>>
by = [3 4];
by = [3 4];
>>
>>
b = bx + by;
b = bx + by;



The equal sign (
The equal sign (
=
=
) commands that the
) commands that the
number computed on the right of it is

number computed on the right of it is
input to the variable named on the left;
input to the variable named on the left;
thus, it is
thus, it is
an assignment operation
an assignment operation
.
.

An
An
array
array
can be defined by typing in a list of numbers
can be defined by typing in a list of numbers
enclosed in
enclosed in
square brackets
square brackets
:
:

Commas
Commas
or
or
spaces
spaces
separate numbers.

separate numbers.

Semicolons
Semicolons
indicate a new row.
indicate a new row.
Defining (or assigning) arrays
Defining (or assigning) arrays
A =
A =


12 18 -3
12 18 -3
B =
B =


2 5 2
2 5 2


1 1 2
1 1 2


0 -2 6
0 -2 6
D =
D =



12 18 -3
12 18 -3


12 18 -3
12 18 -3


2 5 2
2 5 2


2 5 2
2 5 2


1 1 2
1 1 2


1 1 2
1 1 2


0 -2 6
0 -2 6



0 -2 6
0 -2 6
Defining arrays continued
Defining arrays continued

You can define an array in terms of another array:
You can define an array in terms of another array:


C =
C =


12 18 -3
12 18 -3




2 5 2
2 5 2


1 1 2
1 1 2


0 -2 6
0 -2 6


Create an array of zeros:
Create an array of zeros:

Create an array of ones:
Create an array of ones:
Note: Placing a single number inside either function will return an
Note: Placing a single number inside either function will return an
n
n
×
×
n
n
array.
array.
e.g.
e.g.
ones(4)
ones(4)
will return a 4 × 4 array filled with ones.
will return a 4 × 4 array filled with ones.
Creating Zeros & Ones arrays
Creating Zeros & Ones arrays
E =
E =


0 0 0 0 0
0 0 0 0 0



0 0 0 0 0
0 0 0 0 0


0 0 0 0 0
0 0 0 0 0
F =
F =


1 1 1
1 1 1


1 1 1
1 1 1

Index
Index
– a number used to identify elements in an array
– a number used to identify elements in an array

Retrieving a value from an array:
Retrieving a value from an array:
Retrieving Values in an Array
Retrieving Values in an Array
ans =
ans =
4

4
ans =
ans =
8
8
G =
G =


1 2 3
1 2 3


4
4
5 6
5 6


7
7
8
8
9
9

×