42
[email protected] • ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Engr/Math/Physics 25 Chp4 MATLAB Programming-4

[email protected] ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

Embed Size (px)

Citation preview

Page 1: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt1

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

1

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Chp4 MATLAB

Programming-4

Page 2: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt2

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

2

Please HELP Rm 3906A Lab

Please do NOT SAVE ANY Files to the DESKTOP on the computers in Rm3906A Lab

Saving to the machine DeskTop Leads to Clutter and Glitchy Computers

Thank You

Page 3: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt3

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

3

Learning 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

Page 4: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt4

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

4

Loop 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

Page 5: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt5

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

5

Repetition → 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

Page 6: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt6

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

6

Test vs Statement Locations

PreTest Loop 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”

Page 7: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt7

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

7

Test vs Statement Locations

PostTest Loop 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”

Page 8: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt8

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

8

Test vs Statement Locations

MidTest Loop The generalization of both the pre-test and the post-test loops• Empty Block-1 →

PreTest Loop• Empty Block-2 →

PostTest Loop

Page 9: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt9

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

9

for Loop Statement

A PreTested, COUNTED Loop

Start

k ≤ n?

Statements-1

end

Statements

True

False

Set k = m

Increment kby s

• No. Repetitions Known

MATLAB Syntaxfor Counter = Start :

Increment: End

statements

end

Page 10: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt10

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

10

for Loop Rules

Given for Loop Counting Variable: k=m:s:n• The step value s may be negative

– Example: k = 10:-2:4 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

Page 11: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt11

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

11

For Loop Example Construct a 23x11

2D Array filled with RANDOM integer between −99 and +99

Game Plan: • Use Nested for

Loops along with rand, round, & fix commands

• Track the No. of Construction Steps

The MATLAB Code% Bruce Mayer, PE % ENGR25 * 27Feb12 % Build_Random_Array_by_FOR_120228.m % % Build 7x11 Array filled with Random Integers between -99 to +99 % Keep Track of Each Construction Step Step = 0 for k = 1:23 % ROW Count for m = 1:11 % COL Count Step = Step + 1 % counts how many times in Calc-§ % A(k,m) = (Random-SIGN)*(Randon-VALUE; 0-99) A(k,m)= (-1)^(round(rand(1)))*(fix(99.9*rand(1))) end end % % check No. of Positives & Negatives q = A>0; r = sum(q); NoPOS = sum(r) u = A<0; v = sum(u); NoNEG = sum(v) Steps = Step

Page 12: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt12

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

12

The 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

x = [10,1000,-10,100];y = NaN*x;for k = 1:length(x)

if x(k) < 0continue

endy(k) = log10(x(k));

end

statement to avoid taking the log of a negative number.

The Result:y = 1, 3, NaN, 2

Page 13: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt13

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

13

Remove continue Statement

Let’s Fine Tune the No-Neg-Log Code by COMMENTING OUT the if-continue Commands

x = [10,1000,-10,100];y = NaN*x;for k = 1:length(x) %if x(k) < 0 %continue %end y(k) = log10(x(k));end

The Result:y =

1.0000 3.0000 1.0000 + 1.3644i 2.0000

Page 14: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt14

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

14

Use 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

Page 15: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt15

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

15

Use of a Logical MASK cont

Logical Mask Session>> B = [0, -1, 4; 9, -14, 25; -34, 49, 64]B = 0 -1 4 9 -14 25 -34 49 64

>> D = (B >= 0)D = 1 0 1 1 0 1 0 1 1

Mask Array →a Logical that “masks out” Negative numbers

Page 16: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt16

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

16

Logical MASK cont

>> B(D) = sqrt(B(D))B = 0 -1 2 3 -14 5 -34 7 8

>> B(~D) = B(~D) + 50B = 0 49 2 3 36 5 16 7 8

Negative Values Unchanged → Masked OUT by D(m,n) = 0

Original B = 0 -1 4 9 -14 25 -34 49 64

Positive Values Unchanged → Masked OUT by D(m,n) = 1

Logical Mask Session cont

Page 17: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt17

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

17

Logical Masking Subtlety>> x = [-7 0 8 5 -2]x = -7 0 8 5 -2

>> nn = x>=0 % the logical masknn = 0 1 1 1 0

>> y = x(nn)y = 0 8 5

>> sqrt1 = sqrt(x(nn))sqrt1 = 0 2.8284 2.2361

>> sqrt2 = x % make starting copy of xsqrt2 = -7 0 8 5 -2

>> sqrt2(nn) = sqrt(sqrt2(nn))sqrt2 = -7.0000 0 2.8284 2.2361 -2.0000

ONLY the Three Sq-Roots

the Three Sq-Roots AND the two NON-Roots

Page 18: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt18

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

18

while 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 isx = 5;while x < 25

disp(x) x = 2*x - 1;

end

Results from the disp statement are 5, 9, and 17.

Page 19: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt19

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

19

while Loop Statement

A PreTested DYNAMIC Loop

Start

LogicalDecision

Statements(MUST Increment

Loop Variable)

end

Statements

True

False

• No. Repetitions UNknown

MATLAB Syntaxwhile Logical Expression

statements

end

Set Loop VarInitial value

Page 20: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt20

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

20

while Loop Statement

For the while loop to function properly two conditions must occur

Start

LogicalDecision

Statements(MUST Increment

Loop Variable)

end

Statements

True

False

1. The loop variable must have a value BEFORE the while statement is executed (initialize)

2. The loop variable must be changed somehow by the statements INSIDE the Loop

Set Loop VarInitial value

Page 21: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt21

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

21

while Loop Build Vector

A simple while loopx = 5;k = 0;while x < 25 k = k + 1 y(k) = 3*x; x = 2*x-1end

The Results

k = 1x = 9k = 2x = 17k = 3x = 33

• The loop variable x is initially assigned the value 5, and it keeps this value until the statement x = 2*x - 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

>> y

y =

15 27 51

Page 22: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt22

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

22

Another while Loop Example

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 just exceed 10,000.

• the sum for this number of terms

The .m-file and the Results

tot = 0;k = 0;while tot < 10e3 k = k + 1; tot = 5*k^2 - 2*k + tot; enddisp('No. terms = ')disp(k)disp('The Sum = ')disp(tot)

No. Terms = 18Sum = 10203

Page 23: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt23

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

23

Demos: for & while

Prob 4-22 → Evaluate with for

10

1

35k

k

ksum• Also list the value of

the individual Terms

Use while to find the number of terms, qmax, such that

max

1

999973.1qk

k

kTotal

Page 24: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt24

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

24

The 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.

Page 25: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt25

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

25

MATLAB switch Syntax

switch input expression (which can be a scalar or string).case value1

statement group 1case value2

statement group 2...otherwise

statement group nend

Page 26: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt26

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

26

switch Example

This switch Block displays the High School Class-Name that Corresponds to a Given Grade Level

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

Page 27: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt27

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

27

switch 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

Page 28: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt28

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

28

Example: Prob 4.27

Consider an Electrical Diode → We can MODEL the V-I Behavior of this Device in Several ways

V

I

REALBehavior

IDEALModel

OFFSETModel

LINEARModel

Page 29: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt29

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

29

Problem-27 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”

Page 30: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt30

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

30

Problem-27 cont

Now Let’s Connect the Diode to• A Power Source, Vs

• A Useful Load, RL

Next Assume that Vs is a Decaying Sinusoidal, Alternating Current (AC) Voltage-Source modeled mathematically as

+VL

-

ts

eVV sts

sin3 3/

Page 31: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt31

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

31

Problem-27 → Plot Vs+

VL

-

% Bruce Mayer, PE * 08Sep11% ENGR25 * Problem 4-27% file = Prob4_27_Vs_plot.m% INPUT SECTIONtmax = input('Max time in sec = ');Vmax = input('Max Supply Potential in V = ');%CALCULATION SECTION% use linspace command to generate 500 time ptst = linspace(0,tmax,500);% Use for-Loop to generate plotting vector, vsfor k = 1:500 % Calc SUPPLY V-Level vsup = Vmax*exp(-t(k)/3)*sin(pi*t(k)); vs(k) = vsup;end % PLOT SECTIONplot(t,vs),ylabel('Load Voltage (V)'),xlabel('Time (sec)'),... title('Ideal-Diode Rectifier'), griddisp('Plot Complete')

Page 32: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt32

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

32

Problem-27 → Plot Vs +VL

-

Dio

de

ON

Page 33: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt33

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

33

Prob 27 cont

Recall the Ideal-Diode Model → With This Diode Behavior weExpect Load a Voltage in this form

IDEALModel

0 if0

0 if

s

ssL V

VVV

+VL

- Write a MATLAB Program to Plot

VL vs t for: 0 t 10s

Page 34: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt34

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

34

Problem-27 → Plot VL Ideal

+VL

-

% Bruce Mayer, PE * 08Sep11% ENGR25 * Problem 4-27a% file = Prob4_27a_ideal_diode.m% INPUT SECTIONtmax = input('Max time in sec = ');Vmax = input('Max Supply Potential in V = ');% CALCULATION SECTION% use linspace command to generate 500 time ptst = linspace(0,tmax,500);% Use for-Loop to generate plotting vector, vLfor k = 1:500 % Calc SUPPLY V-Level at the current t(k) vs = Vmax*exp(-t(k)/3)*sin(pi*t(k)); % chk Fwd or Rev condition by if-else if vs > 0 vL(k) = vs; % diode absorbs NO voltage else vL(k) = 0; % diode BLOCKS ALL Current endendplot(t,vL),ylabel('Load Voltage (V)'),xlabel('Time (sec)'),... title('Ideal-Diode Rectifier'), grid

VS

Page 35: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt35

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

35

Problem-27 → Plot VL Ideal

IDEALModel

Page 36: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt36

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

36

Prob 27 cont

Recall the OffSet-Diode Model → With This Diode Behavior weExpect Load Voltage in this form

VV

VVVVV

s

ssL 6.0 if0

6.0 if6.0+

VL

- Write a MATLAB Program to Plot

VL vs t for: 0 t 10s

OFFSETModel

Page 37: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt37

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

37

Problem-27 → Plot VL Offset

+VL

-

% Bruce Mayer, PE * 08Sep11% ENGR25 * Problem 4-27b% file = Prob4_27b_offset_diode.m% INPUT SECTIONtmax = input('Max time in sec = ');Vmax = input('Max Supply Potential in V = ');% CALCULATION SECTION% use linspace command to generate 500 time ptst = linspace(0,tmax,500);% Use for-Loop to generate plotting vector, vLfor k = 1:500 % Calc SUPPLY V-Level at current t(k) vs = Vmax*exp(-t(k)/3)*sin(pi*t(k)); % chk Fwd or Rev condition by if-else if vs > 0.6 vL(k) = vs-0.6; % diode absorbs 0.6V else vL(k) = 0; % diode BLOCKS All current endendplot(t,vL),ylabel('Load Voltage (V)'),xlabel('Time (sec)'),... title('Offset-Diode Rectifier'), grid

VS

Page 38: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt38

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

38

Problem-27 → Plot VL Offset

OFFSETModel

Page 39: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt39

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

39

Prob 27 Analysis Compare Plots Side-by-Side

0.6V Offset has a large affect when the Vs amplitude is only 3V• OffSet is 20% of amplitude

+VL

-

Page 40: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt40

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

40

Prob 24 Analysis

Plots for 24V amplitude

Makes less difference• Note different vertical scales

+VL

-

Page 41: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt41

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

41

All Done for Today

SinusoidalHalfWaveRectifier

Page 42: BMayer@ChabotCollege.edu ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical

[email protected] • ENGR-25_Programming-4.ppt42

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

42

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Appendix 6972 23 xxxxf