ece340 matlab soln1 s13

12
Diary File for Problem B.7. format compact, % The compact format will reduce the "white space" on the % output. % Prior to executing the next command, I need to make sure the M-file % is in the operating directory I am using within Matlab. Problem_Bp7 , % Execute the commands in the M-script for Problem B.7. w1 = 3.0000 + 4.0000i w2 = 1.4142 + 1.4142i Part (a): Polar Form of w1 mag_w1 = 5 ang_w1 = 0.9273 ang_w1_degrees = 53.1301 Part (b): Rectangular Form of w2 real_w2 = 1.4142 imag_w2 = 1.4142 Part (c): Square of the magnitudes of Both Complex Numbers w1_sq = 25 w2_sq = 4 Part (d): Rectangular Form of w1+w2 r_w1pw2 = 4.4142 i_w1pw2 = 5.4142 Part (e): Polar Form of w1-w2 m_w1mw2 = 3.0333 a_w1mw2 = 1.0207 a_deg_w1mw2 = 58.4804 Part (f): Rectangular Form of w1*w2 w1w2 = -1.4142 + 9.8995i r_w1w2 = -1.4142 i_w1w2 = 9.8995 Part (g): Polar Form of w1/w2 m_w1dw2 = 2.5000 a_w1dw2 = 0.1419 a_deg_w1dw2 = 8.1301 diary off M-file named “Problem_Bp7.m” % Problem_Bp7.m % % M-script to execute the commands necessary to verify Problem B.7 % in the textbook. B.P. Lathi, "Linear Systems and Signals," Second Ed. % w1= 3 + j*4, % Define the first complex number. w2=2*exp(j*(pi/4)), % Define the second complex number. % % Part (a): Express w1 in standard polar form. display('Part (a): Polar Form of w1') mag_w1=abs(w1), % Find the magnitude of the complex number. ang_w1=angle(w1), % Compute the angle of the complex number. ang_w1_degrees=(180/pi)*ang_w1, % Convert the angle from radians to degrees. % % Part (b): Express w2 in standard rectangular form. display('Part (b): Rectangular Form of w2') real_w2=real(w2), % Extract the real part of the complex number. imag_w2=imag(w2), % Extract the imaginary part of the complex number. % % Part (c): Compute the square of the magnitudes of the two complex #'s. display('Part (c): Square of the magnitudes of Both Complex Numbers') w1_sq=(abs(w1))^2, % Display the square of the magnitude of complex number 1. w2_sq=(abs(w2))^2, % Display the square of the magnitude of complex number 2. % % Part (d): Display the sum of w1 and w2 in rectangular form. display('Part (d): Rectangular Form of w1+w2') r_w1pw2=real(w1+w2), % Real part of sum of w1 and w2. i_w1pw2=imag(w1+w2), % Imaginary part of sum of w1 and w2. % % Part (e): Display the difference between w1 and w2 in polar form. display('Part (e): Polar Form of w1-w2') m_w1mw2=abs(w1-w2), % Display the magnitude of w1-w2. a_w1mw2=angle(w1-w2), % Display the angle (in radians) of w1-w2. a_deg_w1mw2=(180/pi)*(a_w1mw2), % Display angle (in degrees) of w1-w2. % % Part (f): Display the product of w1 and w2 in rectangular form. display('Part (f): Rectangular Form of w1*w2') w1w2=w1*w2, % This answer by default should be in rectangular form. r_w1w2=real(w1*w2), % If not, display the real part of the produce w1*w2. i_w1w2=imag(w1*w2), % Display the imaginary part of the product w1*w2. MATLAB Problems #1: ECE 340 Spring 2013 ece340_soln_s13 Page 1

Upload: christopher-cheuvront

Post on 23-Oct-2015

22 views

Category:

Documents


0 download

DESCRIPTION

University of Arizona ECE 340

TRANSCRIPT

Diary File for Problem B.7.

format compact, % The compact format will reduce the "white space" on the

% output.

% Prior to executing the next command, I need to make sure the M-file

% is in the operating directory I am using within Matlab.

Problem_Bp7 , % Execute the commands in the M-script for Problem B.7.

w1 =

3.0000 + 4.0000i

w2 =

1.4142 + 1.4142i

Part (a): Polar Form of w1

mag_w1 =

5

ang_w1 =

0.9273

ang_w1_degrees =

53.1301

Part (b): Rectangular Form of w2

real_w2 =

1.4142

imag_w2 =

1.4142

Part (c): Square of the magnitudes of Both Complex Numbers

w1_sq =

25

w2_sq =

4

Part (d): Rectangular Form of w1+w2

r_w1pw2 =

4.4142

i_w1pw2 =

5.4142

Part (e): Polar Form of w1-w2

m_w1mw2 =

3.0333

a_w1mw2 =

1.0207

a_deg_w1mw2 =

58.4804

Part (f): Rectangular Form of w1*w2

w1w2 =

-1.4142 + 9.8995i

r_w1w2 =

-1.4142

i_w1w2 =

9.8995

Part (g): Polar Form of w1/w2

m_w1dw2 =

2.5000

a_w1dw2 =

0.1419

a_deg_w1dw2 =

8.1301

diary off

M-file named “Problem_Bp7.m”

% Problem_Bp7.m

%

% M-script to execute the commands necessary to verify Problem B.7

% in the textbook. B.P. Lathi, "Linear Systems and Signals," Second Ed.

%

w1= 3 + j*4, % Define the first complex number.

w2=2*exp(j*(pi/4)), % Define the second complex number.

%

% Part (a): Express w1 in standard polar form.

display('Part (a): Polar Form of w1')

mag_w1=abs(w1), % Find the magnitude of the complex number.

ang_w1=angle(w1), % Compute the angle of the complex number.

ang_w1_degrees=(180/pi)*ang_w1, % Convert the angle from radians to degrees.

%

% Part (b): Express w2 in standard rectangular form.

display('Part (b): Rectangular Form of w2')

real_w2=real(w2), % Extract the real part of the complex number.

imag_w2=imag(w2), % Extract the imaginary part of the complex number.

%

% Part (c): Compute the square of the magnitudes of the two complex #'s.

display('Part (c): Square of the magnitudes of Both Complex Numbers')

w1_sq=(abs(w1))^2, % Display the square of the magnitude of complex number 1.

w2_sq=(abs(w2))^2, % Display the square of the magnitude of complex number 2.

%

% Part (d): Display the sum of w1 and w2 in rectangular form.

display('Part (d): Rectangular Form of w1+w2')

r_w1pw2=real(w1+w2), % Real part of sum of w1 and w2.

i_w1pw2=imag(w1+w2), % Imaginary part of sum of w1 and w2.

%

% Part (e): Display the difference between w1 and w2 in polar form.

display('Part (e): Polar Form of w1-w2')

m_w1mw2=abs(w1-w2), % Display the magnitude of w1-w2.

a_w1mw2=angle(w1-w2), % Display the angle (in radians) of w1-w2.

a_deg_w1mw2=(180/pi)*(a_w1mw2), % Display angle (in degrees) of w1-w2.

%

% Part (f): Display the product of w1 and w2 in rectangular form.

display('Part (f): Rectangular Form of w1*w2')

w1w2=w1*w2, % This answer by default should be in rectangular form.

r_w1w2=real(w1*w2), % If not, display the real part of the produce w1*w2.

i_w1w2=imag(w1*w2), % Display the imaginary part of the product w1*w2.

MATLAB Problems #1: ECE 340 Spring 2013

ece340_soln_s13 Page 1

i_w1w2=imag(w1*w2), % Display the imaginary part of the product w1*w2.

%

% Part (g): Display the quotient of w1 and w2 in polar form.

display('Part (g): Polar Form of w1/w2')

m_w1dw2=abs(w1/w2), % Display the magnitude of w1/w2.

a_w1dw2=angle(w1/w2), % Display the angle (in radians) of w1/w2.

a_deg_w1dw2=(180/pi)*(a_w1dw2), % Display the angle (in degrees) of w1/w2.

ece340_soln_s13 Page 2

Problem B.22 solved using MATLAB.

Diary file captured during the execution of the M-file entitled ‘Problem_Bp22.m’.

format compact, % Reduce the amount of "white space" on the output.

Problem_Bp22, % Execute the commands for Problem B.22 via the M-script file.

a =

-1.0000 + 6.2832i

Tend =

6

Tdel =

0.0100

Tbend =

3

b =

1.0000 - 6.2832i

diary off

M-file for Problem B.22:

% Problem B.22

% M-script to execute the commands for Problem B.22.

% Part (a)

a=(-1+j*(2*pi)) % Define the complex number in the exponent for part (a).

% The time constant for this signal is associated with the reciprocal of

% the real part of the exponent. For this exponent, the time constant is

% 1.0. Thus, I will let the time horizon of the simulation for Part (a) be

% a little longer than 5 time constants.

Tend=6.0, % Time value for the end of the simulation.

% One may want 10 samples per time constant (1/10 = 0.1) for a time step

% resolution, or one may need to let the damped frequency of oscillation

% determine the time step resolution. For Part (a), the damped frequency

% is 2*pi*1 (radians/second) or 1 Hertz. A 1 hertz frequency corresponds

% to a period of oscillation of T = 1/f = 1 second. One may want to sample

% the waveform 30 or more times every oscillation cycle. Thus, the time

% step resolution for this part of the problem would be 1/30, which is

% approximately 0.0333. This value is smaller than the time step

% resolution based on the exponential decay rate's time constant. I will

% select a time step resolution based on the smaller of those two

% resolution values. For this part, let me sample with an even finer

% resolution. A nice number that is smaller than 0.0333 could be 0.01.

% Let me pick the sample period resolution to equal 10 milliseconds or

% 0.01.

Tdel=0.01, % Time step resolution value.

t=0:Tdel:Tend; % Define the time vector for the simulation of Part (a).

x1=real(2*exp(a*t)); % Create the exponentially damped cosine waveform.

figure(1)

plot(t,x1)

xlabel('Time (seconds)')

ylabel('x_1(t)')

title('Problem B.22, Part (a)')

%

% Part (b)

% This waveform has a positive exponent, which will cause the resulting

ece340_soln_s13 Page 3

% This waveform has a positive exponent, which will cause the resulting

% waveform to go unbounded rather quickly! The exponential growth rate is

% determined by the positive real exponent value of 1. I will only

% simulate this waveform for 3.0 seconds since the output will grow to

% exp(3) in that length of time. The frequency of the waveform is still 1

% hertz, so I will still use the same time step resolution value of 0.01.

Tbend=3, % Select the end time of the simulation for part (b).

tb=0:Tdel:Tbend; % Create a new time vector for Part (b).

b=(1-j*2*pi), % Define the complex exponent for Part (b).

x2=imag(3-exp(b*tb)); % Create the waveform for part (b).

figure(2)

plot(tb,x2)

xlabel('Time (seconds)')

ylabel('x_2(t)')

title('Problem B.22, Part (b)')

% Part (c)

% The waveform in this part is just like Part (b) except it has an offset

% of 3 in magnitude. The creation of this simulation will be the same as

% in Part (b) so the two waveforms can be compared.

x3=3-imag(exp(b*tb)); % Create the waveform for Part (c).

figure(3)

plot(tb,x3)

xlabel('Time (seconds)')

ylabel('x_3(t)')

title('Problem B.22, Part (c)')

%

% The waveforms in Parts (b) and (c) should differ since the offset of the

% real amount 3.0 is included inside the imaginary function operation in

% Part (b) and is outside the imaginary function operation in Part (c).

Figure 1: Figure for Problem B.22, Part (a). (Below)

Figure 2: Figure for Problem B.22, Part (b). (Below)

Figure 3: Figure for Problem B.22, Part (c). (Below)

ece340_soln_s13 Page 4

Problem B.23 (MATLAB Exercise #3 in Homework Assignment #1)

Diary file for Problem B.23 follows. I only need to include the diary file, since I issued the MATLAB command ‘echo on’ as the first line in the M-file ‘Problem_Bp23.m’ .

format compact, % Reduce the amount of 'white space' in the output.

Problem_Bp23, % Execute the M-file for this problem.

echo on

% The "echo on" command has been inserted as the first line of

% this M-file to force Matlab to display all of the commands that

% are executed in this M-file.

%

% Problem_Bp23

%

% M-script to execute the commands necessary to plot or graph the product

% of a cosine waveform and a sine waveform. I will use the

% element-by-element multiplication operation to create the necessary

% waveform.

%

% cos(t) has an angular frequency of 1 rad/second.

% sin(20t) has an angular frequency of 20 rad/second.

% The two frequencies in Hertz are 1/(2*pi) = 0.159 Hz and 20/(2*pi) = 3.18

% Hz, respectively.

% The corresponding periods for each waveform can help determine how long

% to simulate and how fast to sample.

% One should probably sample at least 30 times the highest (fastest)

% frequency. This sample rate means the time step resolution would be the

% reciprocal of this sample rate. Sample rate of 30*3.18 is a little

% larger than 90 Hertz so let's sample at 100 Hz or every (1/100) = 0.01

% seconds. The slow waveform at 0.159 Hz means that its period of

% oscillation is (1/0.159)=(1/(1/2*pi))=2*pi=6.28 seconds and this will

% provide an estimate of how slow the outer oscillation will be changing in

% time. Let me simulate for a little longer than this slower waveform's

% period.

Tend=8, % Simulate a little longer than 6.28 seconds.

Tend =

8

Tdel=0.01, % Sample a little faster than 30 times the highest frequency.

Tdel =

0.0100

t=0:Tdel:Tend; % Create the time vector for Problem B.23.

x23=cos(t).*sin(20*t); % Perform an element-wise multiplication of waves.

figure, % Create a new figure window.

plot(t,x23) % Plot the modulated waveform.

xlabel('Time (seconds)')

ylabel('Modulated Sinusoids')

title('Problem B.23')

diary off

Figure for Problem B.23 (below):

ece340_soln_s13 Page 5

format compact, % Reduce the amount of 'white space' visible in the output.

Problem_Bp33, % Execute the commands in the M-file for Problem B.33 .

echo on

% Display all of the commands in this M-file during execution.

%

% Problem_Bp33.m

%

% Solve a system of four equations in four unknowns.

% MATLAB is designed to do this heavy lifting matrix algebra!

A=[1,1,1,1;1,1,1,-1;1,1,-1,-1;1,-1,-1,-1], % Coefficient Matrix.

A =

1 1 1 1

1 1 1 -1

1 1 -1 -1

1 -1 -1 -1

B=[4;2;0;-2], % Right-hand side vector in Ax=B.

B =

4

2

0

-2

xx=inv(A)*B, % Determine the solution of this linear set of equations.

xx =

1

1

1

1

%

% As a check on the work, I will go ahead and multiply the answer by A to

% see if I obtain the right hand side matrix B.

B_chk=A*xx, % Check my work.

B_chk =

4

2

0

-2

diary off

Problem B.36.

Diary file captured during the execution of ‘Problem_Bp36.m’ is provided below.

format compact, % Reduce the 'white space' in the output.

Problem_Bp36, % Execute the commands in the M-file for Problem B.36.

echo on

%

% Display the commands listed in this M-file.

%

% Problem_Bp36.

%

% Perform the partial fraction expansion of the given transforms using the

% 'residue' command in Matlab.

% I will define the numerator and denominator of each transfer function in

% terms of polynomials to allow 'residue' to work properly. (See page 64

% of the textbook.)

%

% Part (a)

ece340_soln_s13 Page 6

% Part (a)

numa=[1,5,6], % Vector of coefficients for the numerator polynomial.

numa =

1 5 6

dena=[1,1,1,1], % Vector of coefficients for the denominator polynomial.

dena =

1 1 1 1

[Ra,Pa,Ka]=residue(numa,dena), % Compute the residues, poles, and direct

% terms.

Ra =

1.0000

0.0000 - 2.5000i

0.0000 + 2.5000i

Pa =

-1.0000

0.0000 + 1.0000i

0.0000 - 1.0000i

Ka =

[]

%

% If there are repeated roots, the residues are ordered or listed in

% ascending order.

% If the transfer function is not proper (the numerator degree equals or

% exceeds the denominator degree), then the 'Ka' vector contains the direct

% terms in the expansion ordered in descending powers of the independent

% variable.

%

% Part (b)

% This transfer function is the reciprocal of the transfer function in part

% (a). Thus, this transfer function is improper and should contain

% non-zero values in the "Ka" vector. In particular, there should be two

% non-zero values in "Ka" with the first value corresponding the s^1 term

% and the second value corresponding to the s^0 term (or the constant

% term).

numb=[1,1,1,1], % Enter the numerator polynomial.

numb =

1 1 1 1

denb=[1,5,6], % Enter the denominator polynomial.

denb =

1 5 6

[Rb,Pb,Kb]=residue(numb,denb)

Rb =

20.0000

-5.0000

Pb =

-3.0000

-2.0000

Kb =

1 -4

%

% Part (c)

numc=1, % Numerator polynomial for Part (c).

numc =

1

d1=conv([1,1],[1,1]), % Illustrate how to multiply two polynomials with the

'conv' function.

d1 =

1 2 1

d2=[1,0,1], % Enter the second polynomial in the denominator expression.

d2 =

1 0 1

denc=conv(d1,d2), % Combine the (s+1)^2 polynomial with (s^2+0*s+1)

polynomial.

denc =

1 2 2 2 1

[Rc,Pc,Kc]=residue(numc,denc)

Rc =

0.5000

0.5000

-0.2500 + 0.0000i

-0.2500 - 0.0000i

Pc =

-1.0000

-1.0000

-0.0000 + 1.0000i

-0.0000 - 1.0000i

Kc =

[]

%

% Part (d)

% This transfer function is proper and not strictly proper.

% Thus, there should be a term in the "Kd" vector.

% This non-zero term will be the constant term or s^0 term.

numd=[1,5,6], % Numerator polynomial.

numd =

1 5 6

dend=[3,2,1], % The denominator polynomial does not have to be monic.

dend =

3 2 1

[Rd,Pd,Kd]=residue(numd,dend)

Rd =

0.7222 - 1.4928i

0.7222 + 1.4928i

Pd =

-0.3333 + 0.4714i

-0.3333 - 0.4714i

Kd =

0.3333

diary off

ece340_soln_s13 Page 7

ece340_soln_s13 Page 8

ece340_soln_s13 Page 9

ece340_soln_s13 Page 10

ece340_soln_s13 Page 11

ece340_soln_s13 Page 12