Sunteți pe pagina 1din 19

February 21, 2005 Lecture 10 - By Paul Lin 1

CPET 190
Problem Solving with MATLAB
Lecture 10




http://www.etcs.ipfw.edu/~lin
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 Circuit
Applications
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
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
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:

< < +
> < +
< > +
> > +
=
0 0 ,
0 0 ,
0 0 ,
0 0 ,
) , (
2 2
2
2
y and x y x
y and x y x
y and x y x
y and x y x
y x f
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 problem
2. 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.
February 21, 2005 Lecture 10 - By Paul Lin 7
10-2 Problem Solving - MATH
Applications
Example 10-1: Evaluating a Function of Two
Variables (continue)
Solution:
1. State the problem as 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
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 into
MATLAB
statements
%func_evs.m
%
n = true;
while n == true
x = input('Enter the x
coefficient: ');
y = input('Enter the y
coefficient: ');
if x >= 0 && y >= 0
f_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
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)
February 21, 2005 Lecture 10 - By Paul Lin 10
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid
Microphone
Domain Knowledge on
Microphone Pick-up
patterns
Omni pattern the most
common pattern
Cardioid pattern A
directional sound pick-up
pattern for use on a stage
Super Cardioid pattern
Source: http://homerecording.about.com/library/weekly/aa032597.htm
February 21, 2005 Lecture 10 - By Paul Lin 11
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid
Microphone
Cardioid pattern A
directional sound pick-up
pattern for use on a stage
Gain Equation
It varies as a function of
angle according to the
equation
g is the constant
associated with a
particular microphone;
(value of g?; assume g =
0.5)
is the angle from the
axis of the microphone
to the sound source
(range of angle?)
) cos 1 ( 2 u + = g Gain
Questions
Calculate gain
versus angle and
prepare a polar
plot (what is this?)
February 21, 2005 Lecture 10 - By Paul Lin 12
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid Microphone - continue
) cos 1 ( 2 u + = g Gain
POLAR(THETA, RHO)
THETA theta in Gain
equation
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.
February 21, 2005 Lecture 10 - By Paul Lin 13
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid
Microphone the 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 u
February 21, 2005 Lecture 10 - By Paul Lin 14
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-2: Cardioid
Microphone Verifying
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 u
Theta = 0,
gain = 2
Theta = 60 degree,
gain = 2
February 21, 2005 Lecture 10 - By Paul Lin 15
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-3: Plot the
amplitude and
frequency response
of 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 on
Amplitude and Phase
angle of the Gain =
Vo/Vi
Circuit Equations?
Xc
R= 16 k
Vo
Vi
Vr
Vc
C = 1F
February 21, 2005 Lecture 10 - By Paul Lin 16
10-3 Problem Solving - Electrical
Circuit Applications
Example 10-3: Circuit
Equations?
Xc = -j*1/(2*pi*f*C);
as f , Xc
Vo = Vc = Vi/(R jXc) * (-
jXc)
Gain of response is
Vo/Vi = (-jXc)/(R-jXc)
Vo/Vi = 1/(1 + j2fRC)
Frequency range?
R*C = 16k * 1 uF = 16
milli-seconds
F = 1/RC about 60 Hz
Use 0 to 1000 Hz as
range
Xc
R= 16 k
Vo
Vi
Vr
Vc
C = 1F
February 21, 2005 Lecture 10 - By Paul Lin 17
10-3 Problem Solving - Electrical
Circuit Applications
Example 3: MATLAB
Program
%rc_lpf.m
%
R = 1600; % 1600 ohms
C = 1E-6; % 1 uF
f = 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)
% Plots
figure(1), subplot(2,1,1), loglog(f,
Av_amp);
title('Amplitude Response - Gain');
xlabel('Frequency - Hz');
ylabel('Av = Vo/Vi');
grid on
figure(1), subplot(2,1,2),
semilogx(f, phase);
title('Phase Response');
xlabel('Frequency - Hz');
ylabel('Angle of Av = Vo/Vi in
Radians');
grid on
February 21, 2005 Lecture 10 - By Paul Lin 18
10-3 Problem Solving - Electrical
Circuit Applications
10
0
10
1
10
2
10
3
10
-2
10
-1
10
0
Amplitude Response - Gain
Frequency - Hz
A
v

=

V
o
/
V
i
10
0
10
1
10
2
10
3
-1.5
-1
-0.5
0
Phase Response
Frequency - Hz
A
n
g
l
e

o
f

A
v

=

V
o
/
V
i

i
n

R
a
d
i
a
n
s
February 21, 2005 Lecture 10 - By Paul Lin 19
Summary
SWITCH, CASE, WHATEVER
Problem Solving - MATH Applications
Problem Solving - Electrical Circuit
Applications

S-ar putea să vă placă și