Download presentation
Presentation is loading. Please wait.
Published byJoella Lane Modified over 9 years ago
1
February 21, 2005 Lecture 10 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 10 http://www.etcs.ipfw.edu/~lin
2
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
3
February 21, 2005 Lecture 10 - By Paul Lin 3 10-1 SWITCH, CASE, WHATEVER SWITCH Construct Another form of flow control or branching Another form of flow control or branching General form General form switch (expression) case exp_1, statement 1-1 statement 1-1 statement 1-2 statement 1-2 **** **** case exp_2, case exp_2, statement 2-1 statement 2-1 statement 2-2 statement 2-2 **** **** case exp_3, case exp_3, statement 3-1 statement 3-1 statement 3-2 statement 3-2 **** ****end
4
February 21, 2005 Lecture 10 - By Paul Lin 4 10-1 SWITCH, CASE, WHATEVER SWITCH Construct Example Example N = input(‘Enter a number - less than 10-: ‘); switch (N) case {1, 3, 5, 7, 9}, disp(‘Odd number is matched’);, disp(‘Odd number is matched’);, case {2, 4, 6, 8, 10}, disp(‘Even number is matched’); disp(‘Even number is matched’); otherwise, otherwise, disp(‘No match is found’); disp(‘No match is found’);end
5
February 21, 2005 Lecture 10 - By Paul Lin 5 10-2 Problem Solving - MATH Applications Example 10-1: Evaluating a Function of Two Variables 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: 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:
6
February 21, 2005 Lecture 10 - By Paul Lin 6 10-2 Problem Solving - MATH Applications Example 10-1: Evaluating a Function of Two Variables Example 10-1: Evaluating a Function of Two Variables Solution: 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.
7
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) Example 10-1: Evaluating a Function of Two Variables (continue) Solution: Solution: 1.State the problem – as the question 2.Define the inputs and outputs User input two values for X and Y User input two values for X and Y Output is a function value Output is a function value 3.Design the algorithm Read the input X and Y Read the input X and Y Calculate f(x,y), based on the X and Y values Calculate f(x,y), based on the X and Y values Print output Print output
8
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: '); x = input('Enter the x coefficient: '); y = input('Enter the y coefficient: '); y = input('Enter the y coefficient: '); if x >= 0 && y >= 0 f_xy = x + y; f_xy = x + y; elseif x >= 0 && y = 0 && y < 0 f_xy = x + y^2; f_xy = x + y^2; elseif x == 0 && y >= 0 elseif x == 0 && y >= 0 f_xy = x^2 + y; f_xy = x^2 + y; else else f_xy = x^2 + y^2; f_xy = x^2 + y^2; end end fprintf('x = %f, y = %f, f(x,y) = %f\n', x, y, f_xy); 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('Enter Ctrl C one or two times to exit the program'); disp(''); disp('');end
9
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) Example 10-1: Evaluating a Function of Two Variables (continue) 5.Test the program (x,y) f(x,y)(x,y) f(x,y) (2,3) -> f(x,y) = 5(2,3) -> f(x,y) = 5 (2, -3) -> f(x, y) = 11(2, -3) -> f(x, y) = 11 (-2, 3) -> f(x,y) = 7(-2, 3) -> f(x,y) = 7 (-2,-3) -> 13(-2,-3) -> 13 How about x, y => a very large number, and a very small number (accuracy, and computer’s limit) How about x, y => a very large number, and a very small number (accuracy, and computer’s limit)
10
February 21, 2005 Lecture 10 - By Paul Lin 10 10-3 Problem Solving - Electrical Circuit Applications Example 10-2: Cardioid Microphone Example 10-2: Cardioid Microphone Domain Knowledge on Microphone Pick-up patterns Domain Knowledge on Microphone Pick-up patterns Omni pattern – the most common patternOmni pattern – the most common pattern Cardioid pattern – A directional sound pick-up pattern for use on a stageCardioid pattern – A directional sound pick-up pattern for use on a stage Super Cardioid patternSuper Cardioid pattern Source: http://homerecording.about.com/library/weekly/aa032597.htm
11
February 21, 2005 Lecture 10 - By Paul Lin 11 10-3 Problem Solving - Electrical Circuit Applications Example 10-2: Cardioid Microphone Example 10-2: Cardioid Microphone Cardioid pattern – A directional sound pick-up pattern for use on a stageCardioid pattern – A directional sound pick-up pattern for use on a stage Gain EquationGain Equation It varies as a function of angle according to the 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) 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?) θ is the angle from the axis of the microphone to the sound source (range of angle?) Questions Questions Calculate gain versus angle andCalculate gain versus angle and prepare a polar plot (what is this?)prepare a polar plot (what is this?)
12
February 21, 2005 Lecture 10 - By Paul Lin 12 10-3 Problem Solving - Electrical Circuit Applications Example 10-2: Cardioid Microphone - continue Example 10-2: Cardioid Microphone - continue POLAR(THETA, RHO) THETA theta in Gain equation THETA theta in Gain equation RHO -> Gain value 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.
13
February 21, 2005 Lecture 10 - By Paul Lin 13 10-3 Problem Solving - Electrical Circuit Applications Example 10-2: Cardioid Microphone – the Program 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 Verifying Answrs
14
February 21, 2005 Lecture 10 - By Paul Lin 14 10-3 Problem Solving - Electrical Circuit Applications Example 10-2: Cardioid Microphone – Verifying the answer Example 10-2: Cardioid Microphone – Verifying the answer Verifying Answers: Gain = 2*g*(1+cos(theta)) Verifying Answers: Gain = 2*g*(1+cos(theta)) g = 0.5, theta = 0, gain = 2*0.5*(1+1) = 2g = 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.5g = 0.5, theta = 60 degree, gain = 2*0.5*(1 +0.5) = 1.5 Theta = 0, gain = 2 Theta = 60 degree, gain = 2
15
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 A simple RC low- pass filter Components: Vi, R and C are in series Components: Vi, R and C are in series R = 16 kΩ, C = 1 μF, R = 16 kΩ, C = 1 μF, Xc = 1/(2*pi*f*C) Xc = 1/(2*pi*f*C) Complex number calculation on Amplitude and Phase angle of the Gain = Vo/Vi Complex number calculation on Amplitude and Phase angle of the Gain = Vo/Vi Circuit Equations? Circuit Equations?
16
February 21, 2005 Lecture 10 - By Paul Lin 16 10-3 Problem Solving - Electrical Circuit Applications Example 10-3: Circuit Equations? Example 10-3: Circuit Equations? Xc = -j*1/(2*pi*f*C); Xc = -j*1/(2*pi*f*C); as f ↑, Xc↓ Vo = Vc = Vi/(R –jXc) * (- jXc) Vo = Vc = Vi/(R –jXc) * (- jXc) Gain of response is Vo/Vi = (-jXc)/(R-jXc) Gain of response is Vo/Vi = (-jXc)/(R-jXc) Vo/Vi = 1/(1 + j2πfRC) Frequency range? Frequency range? R*C = 16k * 1 uF = 16 milli-secondsR*C = 16k * 1 uF = 16 milli-seconds F = 1/RC about 60 HzF = 1/RC about 60 Hz Use 0 to 1000 Hz as rangeUse 0 to 1000 Hz as range
17
February 21, 2005 Lecture 10 - By Paul Lin 17 10-3 Problem Solving - Electrical Circuit Applications Example 3: MATLAB Program 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
18
February 21, 2005 Lecture 10 - By Paul Lin 18 10-3 Problem Solving - Electrical Circuit Applications
19
February 21, 2005 Lecture 10 - By Paul Lin 19 Summary SWITCH, CASE, WHATEVER SWITCH, CASE, WHATEVER Problem Solving - MATH Applications Problem Solving - MATH Applications Problem Solving - Electrical Circuit Applications Problem Solving - Electrical Circuit Applications
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.