Sie sind auf Seite 1von 20

1

LESSON OUTCOMES
At the end of this topic, the students will be
able:

To create vector, array and matrix using


Matlab.

To perform the simple calculations in Matlab by


applying vector, array and matrix.
Outline
1.0 Assignment and Mathematical Operations in Matlab
1.1 Assignment

1.1.1 Extended arithmetic (IEEE)


1.2 Vector, Array and Matrix

1.2.1 Creating a vector


1.2.2 Appending a vector
1.2.3 Creating Matrices
1.2.4 Element by Element Operations
1.2.5 Array Addressing

3
1.1 Assignment

Refers to assigning values to variable names

The result of the rst calculation is labelled as ans


by Matlab and is used in the second calculation
where its value is changed.

The own names can be used to store numbers. Then,


the names can be used in further calculations.

Matlab distinguishes between lower case and capital


letters. A and a are different objects for Matlab.
1.1.1 Extended arithmetic (IEEE)

What does Matlab produce if you ask it to find:


>> 1/0
>> -1/0
>> 0/0
>> 1/Inf

The symbols Inf (meaning infinity) and NaN (not


a well defined number) are part of a special
standard for computer arithmetic.

This is the IEEE international standard for


extended arithmetic
1.2 Vector, Array and Matrix

A vector can have only one column, or only one


row.

An array can have multiple rows, multiple


columns, or both.

Two-dimensional array : MATRIX


1.2.1 Creating a vector

To create a column vector:


can separate the elements by semicolons;
>> g = [3;7;9]
use the transpose notation
>> g=[3,7,9]'
or
>> g=[3
7
9]
To create a row vector:
enclose the values in square brackets.
>> v = [9 7 5 3 1]

with the sequence notation start:end or


start:inc:end
>> v=[1:10]
>> v=[1:0.1:2]
>> v=[10:-1:1]
1.2.2 Appending a vector

Matlab allows to append columns vectors


together to create new ones.
>> A=[1;4;5];
>> B=[ 2;3;3];
>> D=[A;B]
D??
>> E=[A B]
E??
Vector elements are accessed using numbers
in parentheses.
>> v =[9 7 5 3 1]

second element of v
>> v(2)
ans?
element of v can be changed
>> v(4) = 100
ans?
1.2.3 Creating Matrices

Spaces or commas separate elements in different


columns
Semicolons separate elements in different rows
>> A=[2 4 10;16 3 7]
>> A=[2,4,10;16,3,7]
A=

2 4 10
16 3 7
Matrix also can create from row or column vectors.
(if matrix A and row vector r have same number of
columns)
>> A=[2 4 10;16 3 7];
>> d=[1,3,5];
>> e=[4;5];
>> x=[A;d] ans??
>> y=[A,e] ans??

Difference between [a b] and [a;b]


>> a=[1,3,5]
>> b=[7,9,11]
>> c=[a b] ans??
>> d=[a;b] ans??
1.2.4 Element by Element Operations
>> v =[9 7 5 3 1]
Addition - the command v+val adds val to each element
of v:
>> v+5
ans?
Subtraction - the command v-val subtracts val from each
element of v:
>> v-5
ans?
Multiplication - the command v*val multiplies each
element of v by val:
>> v*5
ans?
Division - the command v/val divides each element of v
by val:
>> v/5
ans?
- the command val./v divides val by each
element of v:
>> 5./v
ans?

Exponentiation - the command v.^val raises each


element of v to the val power:
>> v.^2
ans?
Symbol Operation Form
+ Scalar-array addition A+ b
Scalar-array subtraction A- b
+ Array addition A+ B
- Array subtraction A B
.* Array multiplication A.*B
./ Array right division A./B
.\ Array left division A.\B
.^ Array exponentiation A.^B
b.^A
A.^b

Exercise:
>> A=[6,3;2,1]
>> B=[4,8;2,7]
>> b=2
Symbol Operation
MN M*N
M-1 inv(M)
MT M'
det(M) det(M)
X=M/N solution to X*N = M {X = M*inverse(N)}
X=M\N solution to M*X = N {X = inverse(M)* N}

Exercise:
>> M=[6,3;2,1]
>> N=[4,8;2,7]
1.2.5 Array Addressing
ROW number is always listed first!!
>>A=[1 3 4 6 8; 12 23 1 3 5; 22 3 4 6 7]
A(2,3) = element in row 2,column 3 in matrix A

A(:,2) = all the elements in the second column of the


matrix A

A(:,2:5) = all the elements in the second through fifth


columns of A

A(2:3,1:3) = all the elements in the second and third


rows that are also in the first through third columns
Exercise
ans =

4 10 13
X= 3 7 18
4 9 25
2 4 10 13 12 15 17
16 3 7 18
8 4 9 25 ans =
3 12 15 17
3 7 18
4 9 25
Command??

ans =
>> X(2,3)
X=
10
7
2 4 10 13
9
16 3 7 18
15
8 4 9 25 ans = ??
3 12 15 13
Exercise
The terminal velocity reached by a sky diver depends on many factors, including
their weight, their body position as they fall, and the density of the air through
which they fall. The terminal velocity is given by

where
2mg m is the sky diver's mass
V g is Earth's gravitational constant
rACd r is the atmospheric density
A is the sky diver's effective area
Cd is the sky diver's coefficient of drag

Compute the terminal velocity of the sky diver for each of the following values of
m:
m = 40kg
m = 80kg
m = 120kg

Use the following values for the other variables:


g = 9.8
r = 1.2
A = 0.5
C =1
Function Description

length(A)

size(A)

reshape(A)

diag(v)

diag(A)

Das könnte Ihnen auch gefallen