Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
206_M62 Dot Product Scalar formed by sum of products of vector elements dot_product = sum(A.*B); dot(A,B);
206_M63 Example Find Total Mass num_items = [ ] mass_items = [ ] item_totals = num_items.* mass_items total_mass = sum(item_totals) total_mass = dot(num_items,mass_items)
206_M64 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)')
206_M65 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...
206_M66 Other Matrix Functions Matrix Inverse Product of matrix and it's inverse yields identity matrix AA -1 = I and A -1 A = I B = inv(A) I = A*B Determinant Scalar computed from entries of a square matrix e.g. (2 x 2): |A| = a 1,1 a 2,2 - a 2,1 a 1,2 det(A)
206_M67 Special Matrices Matrix of Zeros zeros(3) zeros(3,2) Matrix of Ones ones(3) ones(3,2) Identity Matrix eye(3)
206_M68 Systems of Linear Equations System of 3 equations with 3 unknowns 3x 1 + 2x 2 - x 3 = 10 -x 1 + 3x 2 + 2x 3 = 5 x 1 - x 2 - x 3 = -1 Matrix representation AX = B
206_M69 Solving Simultaneous Equations Solution Using the Matrix Inverse AX = B A -1 AX = A -1 B IX = A -1 B X = A -1 B MATLAB Solution X = inv(A)*B Better MATLAB Solution using Left Division X = A\B Uses Gaussian elimination (without forming the inverse)
206_M610 Example Mesh Analysis Problem Statement Find mesh currents for a given circuit Input/Output Description Four resistance values Three mesh current values Three voltage values
206_M611 Example Hand Example Zero voltage results in zero current See example in circuits text Algorithm Development Input resistance values Input voltage source values Form resistance matrix Form voltage matrix Compute current matrix
206_M612 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
206_M613 Testing R1 = 1; R2 = 2; R3 = 3; R4 = 4; V1 = 7; V2 = 3; V3 = 9; I1 = 2; I2 = -1; I3 = 1;
206_M614 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)
206_M615 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;
206_M616 Random Noise Uniform Noise rand(1,n) Interval -a to +a mean = 0 variance = a 2 /3 x = 2*a*r - a Gaussian Noise randn(1,n) Standard Deviation = a mean = 0 variance = a 2 x = a*r
206_M617 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)
206_M618 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)
206_M619 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
206_M620 Problem Solving Applied Hand Example SNR = (signal power)/(noise power) signal power = A 2 /2 noise power = a 2 /3 Algorithm Development Prompt for A and SNR Compute a Generate sine and noise Compute powers and SNR Plot sine plus noise
206_M621 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')
206_M622 Summary Matrix Computations Systems of Simultaneous Equations Signal-to-Noise Ratio
206_M623 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