computer simulation lab

35
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”

Upload: clarke

Post on 18-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

Computer Simulation Lab. “Lecture 4”. Electrical and Computer Engineering Department SUNY – New Paltz. Logical vectors. objectives L ogical operators Logical vectors Logical functions. Logical Operators. r = 0; r

TRANSCRIPT

Page 1: Computer Simulation Lab

SUNY-New Paltz

Computer Simulation Lab

Electrical and Computer Engineering Department

SUNY – New Paltz

“Lecture 4”

Page 2: Computer Simulation Lab

SUNY-New Paltz

Logical vectors

objectives

1. Logical operators2. Logical vectors3. Logical functions

Page 3: Computer Simulation Lab

SUNY-New Paltz

Logical Operators

r = 1;

r <= 0.5

ans= 0

r = 0;

r <= 0.5

ans= 1

r = .5;

r <= 0.5

ans= 1

r = 1:5;

r <= 3

ans= 1 1 1 0 0

a = 1:5;

b = [0 2 3 5 6];

a == b

ans= 0 1 1 0 0

Page 4: Computer Simulation Lab

SUNY-New Paltz

Application of Logical Vectors

x = 0 : pi/20 : 3 * pi;y = sin(x);y = y .* (y > 0); plot(x, y)

1-form vector 0<x<3*pi2- form a vector such that if y > 0 then it is one Multiply y by this vector

Page 5: Computer Simulation Lab

SUNY-New Paltz

Avoiding Division by Zero

x = -4*pi : pi/20 : 4*pi;x = x + (x == 0)*eps;y = sin(x) ./ x;plot(x, y) Sin(ε)/ε=1

1-form vector x -4*pi <x< 4*pi2- form a new vector x such that if x=0, then it is eps3- Divide sin(x) by this vector

Page 6: Computer Simulation Lab

SUNY-New Paltz

Logical Operators

Page 7: Computer Simulation Lab

SUNY-New Paltz

Operator Precedence

Page 8: Computer Simulation Lab

SUNY-New Paltz

Be Careful!

0 < r < 1 Wrong!

(0 < r) & (r < 1)

Correct!

Page 9: Computer Simulation Lab

SUNY-New Paltz

Subscripting with logical vectors

a = [-2 0 1 5 9];

a([5 1 3])

ans: 9 -2 1

a(logical([0 1 0 1 0]))

ans: 0 5

a = a(a > 0)

ans: 1 5 9

Page 10: Computer Simulation Lab

SUNY-New Paltz

Verifying Logical Vectors

a = [ 1 –1 2 0 3]b= a > 0islogical(a)ans: 0islogical(b)ans: 1

Page 11: Computer Simulation Lab

SUNY-New Paltz

Logical functions

any(x)

all(x)

exist(’a’)

find(x)

returns the scalar 1 (true) if any element of x is non-zero (true).

returns the scalar 1 if all the elements of x are non-zero.

returns 1 if a is a workspace variable.

returns a vector containing the subscripts of the non-zero (true) elements of x

Page 12: Computer Simulation Lab

SUNY-New Paltz

Examples

a=[0 2 1 2 3 4]

a = a( find(a) )

x = [8 1 -4 8 6];

find(x >= max(x))

Page 13: Computer Simulation Lab

SUNY-New Paltz

Logical functions

isempty(x)

isinf(x)

isnan(x)

returns 1 if x is an empty array and 0 otherwise.

returns 1’s for the elements of x which are +Inf or −Inf, and 0’s otherwise

returns 1’s where the elements of x are NaN and 0’s otherwise.Example: x(isnan(x)) = [ ]

Page 14: Computer Simulation Lab

SUNY-New Paltz

Using ‘any’ and ‘all’

if any(a ~= b)

statementend

if a ~= b

statement

end

if all(a >= 1)

do something

end

Page 15: Computer Simulation Lab

SUNY-New Paltz

Logical vectors instead of ‘elseif’ ladders

Page 16: Computer Simulation Lab

SUNY-New Paltz

Matlab Code Using if/elseif

inc =[5000 10000 15000 30000 50000];

for ti = inc

if ti < 10000

tax = 0.1 * ti;

elseif ti < 20000

tax = 1000 + 0.2 * (ti - 10000);

else

tax = 3000 + 0.5 * (ti - 20000);

end;

disp( [ti tax] )

end;

Page 17: Computer Simulation Lab

SUNY-New Paltz

Logical Way

inc = [5000 10000 15000 30000 50000];

tax = 0.1 * inc .* (inc <= 10000);

tax = tax+(inc > 10000 & inc <= 20000) ...

.* (0.2 * (inc-10000) + 1000);

tax = tax + (inc > 20000) .* (0.5*(inc-20000)+ 3000);

disp( [inc’ tax’] );

Page 18: Computer Simulation Lab

SUNY-New Paltz

Matrices

Objectives

• ways of creating and manipulating matrices;

• matrix operations.

Page 19: Computer Simulation Lab

SUNY-New Paltz

Creating matrices

a = [1 2; 3 4];x = [5 6];a = [a; x]

a =

1 2

3 4

5 6

b = a’

b=1 3 5

2 4 6

Page 20: Computer Simulation Lab

SUNY-New Paltz

Subscripts

a(3,3)

a(2:3,1:2)

a(3,:)

a(1:2,2:3) = ones(2)

x = [0:30:180]’;

trig(:,1) = x;

trig(:,2) = sin(pi/180*x);

trig(:,3) = cos(pi/180*x);

a =1 2 3

4 5 6

7 8 9

trig = 0 0 1.0000 30.0000 0.5000 0.8660 60.0000 0.8660 0.5000 90.0000 1.0000 0.0000 120.0000 0.8660 -0.5000 150.0000 0.5000 -0.8660 180.0000 0.0000 -1.0000

Page 21: Computer Simulation Lab

SUNY-New Paltz

‘:’ Operator

a =1 23 4

b = a(:)

b=1234

b =1 2 34 5 6

a =0 00 00 0

a(:) = b

a =1 54 32 6

Page 22: Computer Simulation Lab

SUNY-New Paltz

Duplicating rows and columns: tiling

a =

1 2 3

repmat(a, [3 1]) or repmat(a, 3, 1)

ans =

1 2 3

1 2 3

1 2 3

Page 23: Computer Simulation Lab

SUNY-New Paltz

Exercise

1 2 3 1 2 3 1 2 3

4 5 6 4 5 6 4 5 6

1 2 3 1 2 3 1 2 3

4 5 6 4 5 6 4 5 6

1 2 3 1 2 3 1 2 3

4 5 6 4 5 6 4 5 6

4 3 4 3 4 3

1 2 1 2 1 2

4 3 4 3 4 3

1 2 1 2 1 2

1 3 5 7 9 11 13 15 17 19

10 9 8 7 6 5 4 3 2 1

1 3 5 7 9 11 13 15 17 19

10 9 8 7 6 5 4 3 2 1

1 3 5 7 9 11 13 15 17 19

10 9 8 7 6 5 4 3 2 1

1 3 5 7 9 11 13 15 17 19

10 9 8 7 6 5 4 3 2 1

Page 24: Computer Simulation Lab

SUNY-New Paltz

Deleting rows and columns

a(:,2) = [ ]

a(1,2) = [ ]

a(2:2:6) = [ ]

a(:, logical([1 0 1]))ans =

1 34 67 9

a =1 2 34 5 67 8 9

a =

1 7 5 3 6 9

Page 25: Computer Simulation Lab

SUNY-New Paltz

Elementary matrices

eye(3)ans =

1 0 00 1 00 0 1

pascal(4)ans =

1 1 1 11 2 3 41 3 6 101 4 10 20

Page 26: Computer Simulation Lab

SUNY-New Paltz

Generating Specialized Matrices

compan - Companion matrix. gallery - Higham test matrices. hadamard - Hadamard matrix. hankel - Hankel matrix. hilb - Hilbert matrix. invhilb - Inverse Hilbert matrix. magic - Magic square. pascal - Pascal matrix. rosser - Classic symmetric eigenvalue toeplitz - Toeplitz matrix. vander - Vandermonde matrix. wilkinson - Wilkinson's eigenvalue test

matrix.

Page 27: Computer Simulation Lab

SUNY-New Paltz

Using MATLAB functions with matrices

a =1 0 11 1 10 0 1

all(a)

any(a) 1 1 1

0 0 1

Page 28: Computer Simulation Lab

SUNY-New Paltz

Manipulating matrices

diag

fliplr

flipud

rot90

tril

extracts or creates a diagonal.

flips from left to right.

flips from top to bottom.

Rotate 90 egrees

Extract Lower Triangle

Page 29: Computer Simulation Lab

SUNY-New Paltz

Array (element-by-element) operations on matrices

a =1 2 34 5 6

a .^ 2ans =1 4 9 16 25 36

a =1 2 34 5 67 8 9for v = adisp(v’)end

1 4 72 5 83 6 9

Page 30: Computer Simulation Lab

SUNY-New Paltz

Visualization of matrices

A = 1000; % amount borrowed

n = 12; % number of payments per year

for r = 0.1 : 0.01 : 0.2

fprintf( ’%4.0f%’, 100 * r );

for k = 15 : 5 : 25

temp = (1 + r/n) ^ (n*k);

P = r * A * temp / n / (temp - 1);

fprintf( ’%10.2f’, P );

end;

fprintf( ’\n’ ); % new line

end;

Page 31: Computer Simulation Lab

SUNY-New Paltz

Vectorize the Problem

for r = 0.1 : 0.01 : 0.2

k = 15 : 5 : 25;

temp = (1 + r/n) .^ (n*k);

P = r * A * temp / n ./ (temp - 1);

disp( [100 * r, P] );

end;

Page 32: Computer Simulation Lab

SUNY-New Paltz

Eliminate for loops!

r = [0.1:0.01:0.2]’

r = repmat(r, [1 3])

k = 15:5:25

k = repmat(k, [11 1])

temp = (1 + r/n) .^ (n * k);

P = r * A .* temp / n ./ (temp - 1)

Page 33: Computer Simulation Lab

SUNY-New Paltz

Multi-dimensional arrays

a = [1:2; 3:4]

a(:,:,2) = [5:6; 7:8]

a(:,:,1) =1 2

3 4

a(:,:,2) =5 6

7 8

Page 34: Computer Simulation Lab

SUNY-New Paltz

Matrix operations

Matrix multiplication

Page 35: Computer Simulation Lab

SUNY-New Paltz

Other matrix functions

det determinant.eig eigenvalue decomposition.expm matrix exponential, i.e. eA, where A

is a matrix.inv inverse.lu LU factorization (into lower and upper

triangular matrices).qr orthogonal factorization.svd singular value decomposition