chapter 2 vectors - Çankaya Üniversitesime313.cankaya.edu.tr/uploads/files/lecture 3...

56
CHAPTER 2 VECTORS

Upload: truongnguyet

Post on 29-Apr-2018

215 views

Category:

Documents


3 download

TRANSCRIPT

CHAPTER 2

VECTORS

Vectors: To create a row vector, separate the

elements by comma or white space. Use square

brackets.

>>p = [3,7,9]

p =

3 7 9

>> u=[5 6 7]

u =

5 6 7

1.Column vectors are created by separating

the elements by semicolons ( ; ).

>> v=[3;7;9]

v =

3

7

9

Column Vectors

1-4

2.Column vector s can be created by using

transpose notation(‘)

>> u=[5 6 7] '

u =

5

6

7

The colon operator (:) easily generates a large vector of

regularly spaced elements. Parentheses are not needed but

can be used for clarity.

Typing

>>x = s:d:f

or

>>x = [s:d:f]

creates a vector x of values with a spacing d. The first or

starting value is s. The last or final value is f.

6

Example

typing:

>> x = 0:2:8 creates the vector

x =

0 2 4 6 8

or typing

>> x = [0:2:7 ] creates the vector

x =

0 2 4 6 7

To create a row vector z consisting of the values

from 0 to 1 in steps of 0.2, type

>> z = [0:0.2:1]

z =

0 0.2000 0.4000 0.6000 0.8000 1.0000

8

If the increment q is omitted, it is presumed

to be 1. Thus typing

>> y = [-3:2] produces the vector

y =

-3 -2 -1 0 1 2

9

10

linspace

Example:

If we want to create a vector that starts at 0.2, ends at 0.92, and

increases by 0.12 using linspace the script is

s=0.2;

f=0.92;

d=0.12;

n=(f-s)/d+1;

x=linspace(s,f,n)

x = 0.2000 0.3200 0.4400 0.5600 0.6800 0.8000 0.9200

11

12

logspace

More Examples

Magnitude, Length, and Absolute Value of a Vector

Keep in mind the precise meaning of these terms when using

MATLAB.

The length command gives the number of elements in the

vector.

The magnitude of a vector x having elements x1, x2, …, xn is

a scalar, given by x12 + x2

2 + … + xn2), and is the same as

the vector's geometric length.

The absolute value of a vector x is a vector whose elements

are the absolute values of the elements of x.

15

For example, if x = [2,-4,5],

Number of elements is determined using length function

>> length(x)

ans =

3

its magnitude is [22 + (–4)2 + 52] = 6.7082; Compute

using norm function

>> norm(x)

ans =

6.7082

• its absolute value is:

>> abs(x)

ans =

2 4 5

16

vector Index Consider vector u

>> u=[0:0.1:1]

u =

Columns 1 through 7

0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000

Columns 8 through 11

0.7000 0.8000 0.9000 1.0000

>>u(7)

ans =

0.6000

• Use the length function to determine how

many values are in an array.

>> m=length(u)

m =

11

You can create vectors by ''appending'' one vector to

another.

Example: To create the row vector u whose first three columns

contain the values of r = [2,4,20]

and whose fourth, fifth, and sixth columns contain

the values of w = [9,-6,3],

you type u = [r,w].

The result is the vector u = [2,4,20,9,-6,3].

19

Vector Adressing The colon operator selects individual elements Here are some examples: ■ v(:) represents all the row or column elements of the vector v. ■ v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5). Example: v=[0:0.5:4]

v =

Columns 1 through 7

0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000

Columns 8 through 9

3.5000 4.0000

>> v(2:5)

ans =

0.5000 1.0000 1.5000 2.0000

Some MATLAB plotting commands

22

23

24

Element-by-element operations:

Symbol

+

-

+

-

.*

./

.\

.^

Examples

[6,3]+2=[8,5]

[8,3]-5=[3,-2]

[6,5]+[4,8]=[10,13]

[6,5]-[4,8]=[2,-3]

[3,5].*[4,8]=[12,40]

[2,5]./[4,8]=[2/4,5/8]

[2,5].\[4,8]=[2\4,5\8]

[3,5].^2=[3^2,5^2]

2.^[3,5]=[2^3,2^5]

[3,5].^[2,4]=[3^2,5^4]

Operation

Scalar-vector addition

Scalar-vector subtraction

Vector addition

Vector subtraction

Vector multiplication

Vector right division

Vector left division

vector exponentiation

Form

A + b

A – b

A + B

A – B

A.*B

A./B

A.\B

A.^B

25

>> r=[3,5,2]

r =

3 5 2

>> v=[2,-3,1]

v =

2 -3 1

>> c=r+v

c =

5 2 3

>> r=[3,5,2]

r =

3 5 2

>> v=[2,-3,1]

v =

2 -3 1

>> w=r-v

w =

1 8 1

Element-by-element multiplication is defined only for vectors

having the same size. The definition of the product x.*y, where x and y each

have n elements, is

x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]

if x and y are row vectors. For example, if

x = [2, 4, – 5], y = [– 7, 3, – 8]

then z = x.*y gives

z = [2(– 7), 4 (3), –5(–8)] = [–14, 12, 40]

28

If x and y are column vectors, the result of x.*y is a

column vector. For example z = (x’).*(y’) gives

Note that x’ is a column vector with size 3 × 1 and y’ is

a column vector with size 3 × 1 .

2(–7)

4(3)

–5(–8)

–14

12

40 = z =

29

Vectorized Functions The built-in MATLAB functions such as sqrt(x) and

exp(x) automatically operate on array arguments to

produce an array result the same size as the array argument x.

Thus these functions are said to be vectorized functions.

Example: in the following session the result y has the same size as

the argument x.

>>x = [4, 16, 25];

>>y = sqrt(x)

y =

2 4 5

30

>> t=0:0.03:0.5;

>> y=exp(-8*t).*sin(9.7*t+pi/2);

>> plot(t,y)

>> xlabel('t (sec)'),...

ylabel('Normalized Pressure Difference y(t)'),...

title('Pressure as a function of time')

>> grid on

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5-0.2

0

0.2

0.4

0.6

0.8

1

1.2

t (sec)

Norm

aliz

ed P

ressure

Diffr

ere

nce y

(t)

Pressure as a function of time

The definition of vector division is similar to the

definition of vector multiplication except that the

elements of one vector are divided by the

elements of the other vector. Both vectors must

have the same size. The symbol for vector right

division is ( ./).

Example

if x = [8, 12, 15] y = [–2, 6, 5]

then z = x./y gives

z = [8/(–2), 12/6, 15/5] = [–4, 2, 3]

36

To find the miles traveled on each leg, we multiply the speed by the time. To do so, we

use the MATLAB symbol .*, which species the multiplication s.*t to produce the

row vector whose elements are the products of the corresponding elements in s and t:

To find the total miles traveled, we use the matrix product, denoted by s*t’. In this

definition the product is the sum of the individual element products; that is,

Example

Element by Element Exponentiation

MATLAB enables us not only to raise

vectors to powers but also to raise scalars

and vectors to vector powers.

To perform exponentiation on an element-

by-element basis, we must use the .^ symbol.

Example if x = [3, 5, 8], then typing x.^3

produces the vector [33, 53, 83] = [27, 125,

512].

However, when multiplying or dividing these functions, or

when raising them to a power, you must use element-by-

element operations if the arguments are vectors.

Example

To compute z = (ey sin x) cos2x,

you must type

z = exp(y).*sin(x).*(cos(x)).^2.

You will get an error message if the size of x is not the

same as the size of y. The result z will have the same size

as x and y.

39

We can raise a scalar to an vector power.

Example if p = [2, 4, 5], then typing 3.^p produces the vector

[32, 34, 35] = [9, 81, 243].

Remember that .^ is a single symbol. The dot in 3.^p is

not a decimal point associated with the number 3. The following operations, with the value of p given here, are

equivalent and give the correct answer:

3.^p

3.0.^p

3..^p

(3).^p

3.^[2,4,5]

40

Spring

F (N) 11 7 8 10 9

K (N/m) 1000 800 900 1200 700

42

Polynomial Roots

Polynomial roots are found by with roots(p)function

Example:

Find the roots of x3 – 7x2 + 40x – 34 = 0

Solution

Put the equation in vector form as follows.

>>p = [1,-7,40,-34];

>>roots(p)

ans =

3.0000 + 5.000i

3.0000 - 5.000i

1.0000

The roots are x = 1 and x = 3 ± 5i.

44

Polynomial coefficients The function poly(r)computes the

coefficients of the polynomial whose roots are specified by the vector r. The result is a

row vector that contains the polynomial’s

coefficients arranged in descending order of

power.

Example,

The roots polynomial are: r=[-2, -5].Find the polynomial coefficients.

>>c = poly([-2, -5])

c =

1 7 10

Polynomial Multiplication and Division

48

Plotting Polynomials

Polyval

The function polyval(a,x)evaluates a polynomial at

specified values of its independent variable x, which can

be a vector. The polynomial’s coefficients of descending powers are stored in the array a. The result is the same

size as x.

51

Example

Plotting a Polynomial

To plot the polynomial f (x) = 9x3 – 5x2 + 3x + 7

for -2 ≤ x ≤ 5, you type

>>p = [9,-5,3,7];

>>x = -2:0.01:5;

>>f = polyval(p,x);

>>plot(x,f)

>>xlabel(’x’)

>>ylabel(’f(x)’)

52

53

continued