Title: W A T K I N S - J O H N S O N C O M P A N Y Semiconductor Equipment Group
1Engr/Math/Physics 25
Chp4 MATLABProgramming-4
Bruce Mayer, PE Licensed Electrical Mechanical
EngineerBMayer_at_ChabotCollege.edu
2Learning Goals
- Write MATLAB Programs That can MAKE Logical
Decisions that Affect Program Output - Write Programs that Employ LOOPing Processes
- For ? No. Loops know a priori
- while ? Loop Terminates based on Logic Criteria
3Loop Structures
- The conditional statements (if, else, elseif) we
learned last time allowed us to determine at
run-time whether or not to execute a block of
code.
- What these Decision Statements Do NOT do is to
allow us to execute a block more than once - The TWO Things that Computers Do Better than
People - STORE Massive Amounts of Data
- REPEAT operations
4Repetition ? LOOPs
- A LOOP is a Program Structure that REPEATS
Until some CONDITION is MET - The NUMBER of Loops may Be
- Known a priori (ahead of time)
- No. of Loops Determined by simple COUNTING
- Determined Dynamically
- No. of Loops Determined by a DECISION statement
- The Loop consists of
- A Condition Test
- A Repeated Statement-Block
5Test vs Statement Locations
- The key feature ? we test to see whether or not
to continue before executing the body of the
loop. - i.e., The Loop May Not Execute at All
- Good if Potential Zero Executions is Desired
- a.k.a. While DO
6Test vs Statement Locations
- The Key feature ? Do Not Test Until the Block
Executes at Least Once - Use if Design Calls for at Least-One Repetition
- a.k.a. DO While
7Test vs Statement Locations
- The generalization of both the pre-test and the
post-test loops - Empty Block-1 ? PreTest Loop
- Empty Block-2 ? PostTest Loop
8for Loop Statement
Start
- A PreTested, COUNTED Loop
Set k m
- No. Repetitions Known
- MATLAB Syntax
- for Counter Start Increment Endstatements
- end
k n?
Increment kby s
True
Statements-1
False
end
Statements
9for Loop Rules
- Given for Loop Counting Variable kmsn
- The step value s may be negative
- Example k 10-24 produces k 10, 8, 6, 4
- If s is omitted, the step value defaults to 1
- If s is positive, the loop will not be executed
if m is greater than n - If s is negative, the loop will not be executed
if m is less than n - If m equals n, the loop will be executed only
once - If the step value s is not an integer, round-off
errors can cause the loop to execute a different
number of passes than intended
10The continue Statement
- The continue statement passes control to the next
iteration of the loop in which it appears,
skipping any remaining statements in the body of
the loop. - The Following Code Uses a continue
statement to avoid taking the log of a negative
number.
x 10,1000,-10,100 y NaNx for k
1length(x) if x(k) lt 0 continue end y(k)
log10(x(k)) end
y 1, 3, NaN, 2
11Remove continue Statement
- Lets Fine Tune the No-Neg-Log Code by COMMENTING
OUT the if-continue Commands
x 10,1000,-10,100 y NaNx for k
1length(x) if x(k) lt 0 continue
end y(k) log10(x(k)) end
y 1.0000 3.0000 1.0000 1.3644i
2.0000
12Use of a Logical MASK
- The use of loops and branching can often be
avoided, thus creating simpler and faster
programs by using a logical array as a mask that
selects elements of another array. - Any elements not selected will remain unchanged.
- The following session creates the logical array D
from the 3x3 numeric array B
13Use of a Logical MASK cont
gtgt B 0, -1, 4 9, -14, 25 -34, 49, 64 B
0 -1 4 9 -14 25 -34 49
64 gtgt D (B gt 0) D 1 0 1
1 0 1 0 1 1
Mask Array ?a Logical that masks out Negative
numbers
14Logical MASK cont
Original B 0 -1 4 9 -14
25 -34 49 64
- Logical Mask Session cont
gtgt B(D) sqrt(B(D)) B 0 -1 2
3 -14 5 -34 7 8 gtgt B(D)
B(D) 50 B 0 49 2 3 36
5 16 7 8
Negative Values Unchanged ? Masked OUT by D(m,n)
0
Positive Values Unchanged ? Masked OUT by D(m,n)
1
15while Loops
- The while loop is used when the looping process
terminates because a specified condition is
satisfied, and thus the number of passes is not
known in advance. - A simple example of a while loop is
x 5 while x lt 25 disp(x) x 2x - 1 end
- Results from the disp statement are 5, 9, and 17.
16while Loop Statement
Start
Set Loop VarInitial value
LogicalDecision
- No. Repetitions UNknown
- MATLAB Syntax
- while Logical Expression
- statements
- end
True
Statements(MUST IncrementLoop Variable)
False
end
Statements
17while Loop Statement
Start
Set Loop VarInitial value
- For the while loop to function properly two
conditions must occur
LogicalDecision
- The loop variable must have a value before the
while statement is executed - The loop variable must be changed somehow by the
statements Inside the Loop
True
Statements(MUST IncrementLoop Variable)
False
end
Statements
18while Loop Example
k 1 x 9 k 2 x 17 k
3 x 33
x 5k 0 while x lt 25 k k 1 y(k)
3x x 2x-1 end
gtgt y y 15 27 51
- The loop variable x is initially assigned the
value 5, and it keeps this value until the
statement x 2x - 1 is encountered the first
time. Its value then changes to 9. Before each
pass through the loop, x is checked to see if its
value is less than 25. If so, the pass is made.
If not, the loop terminates
19Another while Loop Example
tot 0k 0 while tot lt 10e3 k k 1
tot 5k2 - 2k tot end disp(No.
terms ') disp(k) disp('The Sum ') disp(tot)
- Write a .m- file to determine
- The min. number of terms required for the sum of
the series 5k2 2k k 1, 2, 3, to exceed
10,000. - the sum for this number of terms
- The .m-file and the Results
No. Terms 18 Sum 10203
20The switch Structure
- The switch structure provides an alternative to
using the if, elseif, and else commands.
Anything programmed using switch can also be
programmed using if structures. - However, for some applications the switch
structure producesmore readable code than when
using the if structure.
21MATLAB switch Syntax
- switch input expression (which can be a
scalar or string). - case value1
- statement group 1
- case value2
- statement group 2
- .
- .
- .
- otherwise
- statement group n
- end
22switch Example
grade_level input('Hi-School Grade Level.
') switch grade_level case 9 disp('
Freshman') case 10 disp('
Sophomore') case 11 disp(' Junior')
case 12 disp(' Senior')
otherwise disp(' NOT a Hi-Schl Grade
Lvl') end
- This switch Block displays the High School
Class-Name that Corresponds to a Given Grade Level
23switch Example Results
Hi-School Grade Level. 9 Freshman Hi-School
Grade Level. 11 Junior Hi-School Grade
Level. 13 NOT a Hi-Schl Grade Lvl Hi-School
Grade Level. 10 Sophomore
24Example Prob 4.24
- Consider an Electrical Diode ?
- We can MODEL the V-I Behavior of this Device in
Several ways
25Problem-24 cont
- The Diode exhibits a form of RECTIFICATION
- i.e., It allows current to Flow in the FORWARD
direction, But NOT in the REVERSE direction - Think of a diode as a Check-Valve for
Electrical Current
26Problem-24 cont
- Now Lets Connect the Diode to
- A Power Source, Vs
- A Useful Load, RL
VL ?
- Next Assume that Vs is a Decaying Sinusoidal,
Alternating Current (AC) Voltage-Source modeled
mathematically as
27Problem-24 ? Plot Vs
VL ?
Bruce Mayer, PE 08Jul05 ENGR25 Problem
4-24 file Prob4_24_Vs_plot.m INPUT
SECTION tmax input('Max time in sec ') Vmax
input('Max Supply Potential in V
') CALCULATION SECTION use linspace command
to generate 500 time pts t linspace(0,tmax,500)
Use for-Loop to generate plotting vector,
vs for k 1500 Calc SUPPLY V-Level
vsup Vmaxexp(-t(k)/3)sin(pit(k)) vs(k)
vsup end PLOT SECTION plot(t,vs),ylabel('Loa
d Voltage (V)'),xlabel('Time (sec)'),...
title('Ideal-Diode Rectifier'), grid disp('Plot
Complete')
28Problem-24 ? Plot Vs
VL ?
29Prob 24 cont
- Recall the Ideal-Diode Model ?
- With This Diode Behavior weExpect Load a Voltage
in this form
VL ?
- Write a MATLAB Program to Plot VL vs t for 0 ?t
? 10s
30Problem-24 ? Plot VL Ideal
Bruce Mayer, PE 08Jul05 ENGR25 Problem
4-24a file Prob4_24a_ideal_diode.m INPUT
SECTION tmax input('Max time in sec ') Vmax
input('Max Supply Potential in V ')
CALCULATION SECTION use linspace command to
generate 500 time pts t linspace(0,tmax,500)
Use for-Loop to generate plotting vector, vL for
k 1500 Calc SUPPLY V-Level at the
current t(k) vs Vmaxexp(-t(k)/3)sin(pit(k
)) chk Fwd or Rev condition by if-else
if vs gt 0 vL(k) vs else
vL(k) 0 end end plot(t,vL),ylabel('Load
Voltage (V)'),xlabel('Time (sec)'),...
title('Ideal-Diode Rectifier'), grid
VL ?
VS
31Problem-24 ? Plot VL Ideal
32Prob 24 cont
- Recall the OffSet-Diode Model ?
- With This Diode Behavior weExpect Load Voltage
in this form
VL ?
- Write a MATLAB Program to Plot VL vs t for 0 ?t
? 10s
33Problem-24 ? Plot VL Offset
Bruce Mayer, PE 08Jul05 ENGR25 Problem
4-24b file Prob4_24b_offset_diode.m INPUT
SECTION tmax input('Max time in sec ') Vmax
input('Max Supply Potential in V ')
CALCULATION SECTION use linspace command to
generate 500 time pts t linspace(0,tmax,500)
Use for-Loop to generate plotting vector, vL for
k 1500 Calc SUPPLY V-Level at current
t(k) vs Vmaxexp(-t(k)/3)sin(pit(k))
chk Fwd or Rev condition by if-else if vs gt
0.6 vL(k) vs-0.6 else
vL(k) 0 end end plot(t,vL),ylabel('Load
Voltage (V)'),xlabel('Time (sec)'),...
title('Offset-Diode Rectifier'), grid
VL ?
VS
34Problem-24 ? Plot VL Offset
35Prob 24 Analysis
VL ?
- Compare Plots Side-by-Side
- 0.6V Offset has a large affect when the Vs
amplitude is only 3V - OffSet is 20 of amplitude
36Prob 24 Analysis
VL ?
- Makes less difference
- Note different vertical scales
37All Done for Today
SinusoidalHalfWaveRectifier
38Engr/Math/Physics 25
Appendix
Time For Live Demo
Bruce Mayer, PE Licensed Electrical Mechanical
EngineerBMayer_at_ChabotCollege.edu