cpet190_lect10
TRANSCRIPT
-
8/12/2019 CPET190_Lect10
1/19
February 21, 2005 Lecture 10 - By Paul Lin 1
CPET 190
Problem Solving with MATLAB
Lecture 10
http://www.etcs.ipfw.edu/~lin
-
8/12/2019 CPET190_Lect10
2/19
February 21, 2005 Lecture 10 - By Paul Lin 2
Lecture 10: MATLAB Program
Design & Flow Control - III
10-1 SWITCH Construct
10-2 Problem Solving - MATH Applications
10-3 Problem Solving - Electrical CircuitApplications
-
8/12/2019 CPET190_Lect10
3/19
February 21, 2005 Lecture 10 - By Paul Lin 3
10-1 SWITCH, CASE, WHATEVER
SWITCH Construct
Another form of flow control or branching General form
switch (expression)
case exp_1,
statement 1-1
statement 1-2****
case exp_2,
statement 2-1
statement 2-2
****case exp_3,
statement 3-1
statement 3-2
****
end
-
8/12/2019 CPET190_Lect10
4/19
February 21, 2005 Lecture 10 - By Paul Lin 4
10-1 SWITCH, CASE, WHATEVER
SWITCH Construct
Example
N = input(Enter a number - less than 10-: );
switch (N)
case {1, 3, 5, 7, 9},
disp(Odd number is matched);,
case {2, 4, 6, 8, 10},
disp(Even number is matched);
otherwise,
disp(No match is found);
end
-
8/12/2019 CPET190_Lect10
5/19
February 21, 2005 Lecture 10 - By Paul Lin 5
10-2 Problem Solving - MATH
Applications
Example 10-1: Evaluating a Function of Two
Variables
Write a MATLAB program to evaluate a function
f(x,y) for any two user-specified values x and y.The function is defined as follows:
00,00,
00,00,
),(22
2
2
yandxyxyandxyx
yandxyxyandxyx
yxf
-
8/12/2019 CPET190_Lect10
6/19
February 21, 2005 Lecture 10 - By Paul Lin 6
10-2 Problem Solving - MATH
Applications
Example 10-1: Evaluating a Function of
Two Variables
Solution:
1. State the problem2. Define the inputs and outputs
3. Design the algorithm
4. Translate the algorithm into MATLAB
statements
5. Test the program
6. Document the process and solution.
-
8/12/2019 CPET190_Lect10
7/19
February 21, 2005 Lecture 10 - By Paul Lin 7
10-2 Problem Solving - MATH
Applications
Example 10-1: Evaluating a Function of TwoVariables (continue)
Solution:
1. State the problemas the question
2. Define the inputs and outputs
User input two values for X and Y
Output is a function value
3. Design the algorithm Read the input X and Y
Calculate f(x,y), based on the X and Y values
Print output
-
8/12/2019 CPET190_Lect10
8/19
February 21, 2005 Lecture 10 - By Paul Lin 8
10-2 Problem Solving - MATH
Applications
Example 10-1:Evaluating a Function of
Two Variables (continue)
4. Translate the
algorithm intoMATLABstatements
%func_evs.m
%
n = true;
while n == truex = input('Enter the x
coefficient: ');
y = input('Enter the ycoefficient: ');
if x >= 0 && y >= 0f_xy = x + y;
elseif x >= 0 && y < 0
f_xy = x + y^2;
elseif x == 0 && y >= 0
f_xy = x^2 + y;else
f_xy = x^2 + y^2;
end
fprintf('x = %f, y = %f,
f(x,y) = %f\n', x, y, f_xy);disp('Enter Ctrl C one or
two times to exit the
program');
disp('');
end
-
8/12/2019 CPET190_Lect10
9/19
February 21, 2005 Lecture 10 - By Paul Lin 9
10-2 Problem Solving - MATH
Applications
Example 10-1: Evaluating a Function of Two
Variables (continue)
5. Test the program
(x,y) f(x,y)
(2,3) -> f(x,y) = 5
(2, -3) -> f(x, y) = 11
(-2, 3) -> f(x,y) = 7
(-2,-3) -> 13
How about x, y => a very large number, and a
very small number (accuracy, and computers
limit)
-
8/12/2019 CPET190_Lect10
10/19
February 21, 2005 Lecture 10 - By Paul Lin 10
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: CardioidMicrophone
Domain Knowledge onMicrophone Pick-up
patterns Omni patternthe most
common pattern
Cardioid patternAdirectional sound pick-uppattern for use on a stage
Super Cardioid pattern
Source: http://homerecording.about.com/library/weekly/aa032597.htm
-
8/12/2019 CPET190_Lect10
11/19
February 21, 2005 Lecture 10 - By Paul Lin 11
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: CardioidMicrophone
Cardioid patternAdirectional sound pick-uppattern for use on a stage
Gain Equation
It varies as a function ofangle according to theequation
g is the constantassociated with aparticular microphone;
(value of g?; assume g =0.5)
is the angle from theaxis of the microphoneto the sound source(range of angle?)
)cos1(2 gGain
Questions Calculate gain
versus angle and
prepare a polar
plot (what is this?)
-
8/12/2019 CPET190_Lect10
12/19
February 21, 2005Lecture 10 - By Paul Lin
12
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid Microphone - continue
)cos1(2
gGain
POLAR(THETA, RHO)
THETA theta in Gainequation RHO -> Gain value
POLAR Polar coordinate
plot. POLAR(THETA, RHO)
makes a plot using polar
coordinates of the angle
THETA, in radians, versus
the radius RHO. POLAR(THETA,RHO,S)
uses the linestyle specified
in string S.
-
8/12/2019 CPET190_Lect10
13/19
February 21, 2005Lecture 10 - By Paul Lin
13
10-3 Problem Solving - Electrical
Circuit Applications Example 10-2: Cardioid
Microphonethe Program%cardioid_mic.m
%Define Variables:
% g -- Microphone gain constant
% gain - Gain(theta) of the
microphone% theta - Angle in radians from
% microphone axis
% Calculate gain versus angle
g = 0.5;
theta = 0:pi/20:2*pi;
gain = 2*g*(1 +cos(theta));% Polar plot Gain vs Angle
polar(theta, gain, 'r-');
title('\bfGain vs Angle \theta');
Verifying Answrs
0.5
1
1.5
2
30
210
60
240
90
270
120
300
150
330
180 0
Gain vs Angle
-
8/12/2019 CPET190_Lect10
14/19
February 21, 2005 Lecture 10 - By Paul Lin 14
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid
MicrophoneVerifying
the answer
Verifying Answers: Gain =
2*g*(1+cos(theta)) g = 0.5, theta = 0, gain =
2*0.5*(1+1) = 2
g = 0.5, theta = 60 degree,
gain = 2*0.5*(1 +0.5) = 1.5
0.5
1
1.5
2
30
210
60
240
90
270
120
300
150
330
180 0
Gain vs Angle
Theta = 0,gain = 2
Theta = 60 degree,gain = 2
-
8/12/2019 CPET190_Lect10
15/19
February 21, 2005 Lecture 10 - By Paul Lin 15
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-3: Plot theamplitude andfrequency responseof Low-Pass Filter
A simple RC low-
pass filter Components: Vi, R
and C are in series
R = 16 k, C = 1 F,
Xc = 1/(2*pi*f*C)
Complex number
calculation onAmplitude and Phaseangle of the Gain =Vo/Vi
Circuit Equations?
Xc
R= 16 k
VoVi
Vr
VcC = 1F
-
8/12/2019 CPET190_Lect10
16/19
February 21, 2005 Lecture 10 - By Paul Lin 16
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-3: CircuitEquations?
Xc = -j*1/(2*pi*f*C);
as f , Xc
Vo = Vc = Vi/(RjXc) * (-
jXc) Gain of response is
Vo/Vi = (-jXc)/(R-jXc)
Vo/Vi = 1/(1 + j2fRC)
Frequency range?
R*C = 16k * 1 uF = 16milli-seconds
F = 1/RC about 60 Hz
Use 0 to 1000 Hz asrange
Xc
R= 16 k
VoVi
Vr
VcC = 1F
-
8/12/2019 CPET190_Lect10
17/19
February 21, 2005 Lecture 10 - By Paul Lin 17
10-3 Problem Solving - Electrical
Circuit Applications
Example 3: MATLABProgram
%rc_lpf.m
%
R = 1600; % 1600 ohms
C = 1E-6; % 1 uFf = 1:2: 1000;
% Frequency varies from
% 1 to 1000 Hz
% Response
Av = 1 ./ (1 + j*2*pi*f*R*C);
Av_amp = abs(Av);% amplitude
% Phase angle
phase = angle(Av)
% Plotsfigure(1), subplot(2,1,1), loglog(f,Av_amp);title('Amplitude Response - Gain');xlabel('Frequency - Hz');ylabel('Av = Vo/Vi');grid onfigure(1), subplot(2,1,2),semilogx(f, phase);title('Phase Response');xlabel('Frequency - Hz');ylabel('Angle of Av = Vo/Vi inRadians');grid on
-
8/12/2019 CPET190_Lect10
18/19
-
8/12/2019 CPET190_Lect10
19/19
February 21, 2005 Lecture 10 - By Paul Lin 19
Summary
SWITCH, CASE, WHATEVER
Problem Solving - MATH Applications
Problem Solving - Electrical Circuit
Applications