matrix computations elec 206 computer applications for electrical engineers dr. ron hayne

23
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

Upload: gary-carter

Post on 01-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

Matrix Computations

ELEC 206

Computer Applications for Electrical Engineers

Dr. Ron Hayne

Page 2: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 2

Dot Product

Scalar formed by sum of products of vector elements

dot_product = sum(A.*B); dot(A,B);

n

iiiba

1

BA

Page 3: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 3

Example

Find Total Mass num_items = [ 3 5 2 1 ] mass_items = [3.5 1.5 .79 1.75]

item_totals = num_items .* mass_items total_mass = sum(item_totals)

total_mass = dot(num_items,mass_items)

Page 4: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 4

Matrix Multiplication

Value in position (i,j) is dot product of row i of the first matrix with column j of the second matrix Inner dimensions must be the same (conformable)

A = [2 5 1;0 3 -1] (2 x 3) B = [1 0; -1 4; 5 2] (3 x 2) C = A * B (2 x 2)

C11 = sum(A(1,:).*B(:,1)')

N

kkjikji bac

1,

Page 5: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 5

Matrix Powers

Square of each element of a matrix A.^2

Square of a matrix A^2 A*A

Other powers A^3 A*A*A ...

Page 6: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 6

Other Matrix Functions

Matrix Inverse Product of matrix and it's inverse yields identity matrix AA-1 = I and A-1A = I B = inv(A) I = A*B

Determinant Scalar computed from entries of a square matrix e.g. (2 x 2): |A| = a1,1a2,2 - a2,1a1,2

det(A)

Page 7: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 7

Special Matrices

Matrix of Zeros zeros(3) zeros(3,2)

Matrix of Ones ones(3) ones(3,2)

Identity Matrix eye(3)

Page 8: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 8

Systems of Linear Equations

System of 3 equations with 3 unknowns 3x1 + 2x2 - x3 = 10

-x1 + 3x2 + 2x3 = 5

x1 - x2 - x3 = -1

Matrix representation AX = B

111

231

123

A

3

2

1

x

x

x

X

1

B 5

10

Page 9: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 9

Solving Simultaneous Equations

Solution Using the Matrix Inverse AX = B A-1AX = A-1B IX = A-1B X = A-1B

MATLAB Solution X = inv(A)*B

Better MATLAB Solution using Left Division X = A\B Uses Gaussian elimination (without forming the inverse)

Page 10: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 10

Example

Mesh Analysis Problem Statement

Find mesh currents for a given circuit Input/Output Description

Four resistance valuesThree mesh current values

Three voltage values

Page 11: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 11

Example

Hand ExampleZero voltage results in zero currentSee example in circuits text

Algorithm Development Input resistance values Input voltage source valuesForm resistance matrixForm voltage matrixCompute current matrix

Page 12: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 12

MATLAB Solution

R1 = input('Input the value of R1: ');

R2 = input('Input the value of R2: ');

R3 = input('Input the value of R3: ');

R4 = input('Input the value of R4: ');

V1 = input('Input the value of V1: ');

V2 = input('Input the value of V2: ');

V3 = input('Input the value of V3: ');

resistance = [R1+R2, -R2 , -R1 ;

-R2 , R2+R3, -R3 ;

-R1 , -R3 , R1+R3+R4]

voltage = [V1; -(V2+V3); V3]

I = inv(resistance)*voltage

Ialt = resistance\voltage

Page 13: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 13

Testing

R1 = 1; R2 = 2; R3 = 3; R4 = 4; V1 = 7; V2 = 3; V3 = 9;

I1 = 2; I2 = -1; I3 = 1;

Page 14: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 14

Signal-to-Noise Ratio

Signal Power (Amplitude) power = sum(x.^2)/length(x)

Signal Power (Variance and Mean) power = std(x)^2 + mean(x)^2

Signal Power (Sinusoid) x = A*sin(2*pi*t) power = A^2/2

Signal-to-Noise Ratio SNR = (signal power)/(noise power)

Page 15: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 15

Random Numbers

Uniform Random Numbers rand('seed',n), rand(n), rand(m,n) Interval 0 to 1 Interval a to b

x = (b - a)*r + a;

Gaussian Random numbers randn('seed',n), randn(n), randn(m,n) Normal Distribution

Mean = 0, Standard Deviation = 1.0 Modified Distribution

Mean = b, Standard Deviation = a x = a*r + b;

Page 16: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 16

Random Noise

Uniform Noise rand(1,n) Interval -a to +a

mean = 0 variance = a2/3

x = 2*a*r - a

Gaussian Noise randn(1,n) Standard Deviation = a

mean = 0 variance = a2

x = a*r

Page 17: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 17

Sinusoid plus Uniform Noise

% Sine plus Uniform Noise

t=0:0.01:2;

noise_u=2*rand(1,201)-1;

sine=4*sin(2*pi*t);

s_u=sine+noise_u;

power_sine=sum(sine.^2)/length(sine)

power_noise_u=sum(noise_u.^2)/length(noise_u)

SNR_u=power_sine/power_noise_u

figure(1)

plot(t,s_u)

figure(2)

hist(noise_u)

Page 18: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 18

Sinusoid plus Gaussian Noise

% Sine plus Gaussian Noise

t=0:0.01:2;

sine=4*sin(2*pi*t);

noise_g=1/sqrt(3)*randn(1,201);

s_g=sine+noise_g;

power_sine=sum(sine.^2)/length(sine)

power_noise_g=sum(noise_g.^2)/length(noise_g)

SNR_g=power_sine/power_noise_g

figure(3)

plot(t,s_g)

figure(4)

hist(noise_g)

Page 19: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 19

Problem Solving Applied

Signal Generation with Noise Problem Statement

Generate a sinusoidal signal with a given amplitude and addition of uniform noise with a specified signal-to-noise ratio

Input/Output Description

Signal Plot

Signal and Noise Power

SNR

Sine Wave Amplitude

Desired SNR

Page 20: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 20

Problem Solving Applied

Hand ExampleSNR = (signal power)/(noise power)signal power = A2/2noise power = a2/3

Algorithm DevelopmentPrompt for A and SNRCompute aGenerate sine and noiseCompute powers and SNRPlot sine plus noise

Page 21: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 21

MATLAB Solution

% Sine plus Uniform Noise at SNR

A = input('Enter amplitude of sinusoid: ');

SNR = input('Enter desired signal-to-noise ratio: ');

a=sqrt(1.5*A^2/SNR);

t=0:0.01:2;

sine=A*sin(2*pi*t);

noise_u=2*a*rand(1,201)-a;

power_sine=sum(sine.^2)/length(sine);

power_noise_u=sum(noise_u.^2)/length(noise_u);

SNR_u=power_sine/power_noise_u

s_u=sine+noise_u;

plot(t,s_u)

title('Sinusoid with Uniform Noise')

Page 22: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 22

Summary

Matrix Computations Systems of Simultaneous Equations Signal-to-Noise Ratio

Page 23: Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

206_M6 23

Test #4 Review

Programming in MATLAB Problems with two variables Input / Output Functions Control Structures

Matrix Computations Systems of Simultaneous Equations Signal-to-Noise Ratio