chapter 8: mupad programming i conditional control and loops matlab for scientist and engineers...

20
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

Upload: dayna-norton

Post on 18-Jan-2016

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

Chapter 8:

MuPAD Programming IConditional Control and Loops

MATLAB for Scientist and Engineers

Using Symbolic Toolbox

Page 2: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

2

You are going to See that MuPAD provides flow control mech-

anism, such as if and loops Get to know how to use if and case state-

ments Use for, while, repeat statements to iterate

various loops

Page 3: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

3

type and testtype

Useful for checking some conditions

Cf. domtype()

For all available Types

Page 4: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

4

IF statement

if condition if condition – else

x<0

S1

x<0

S1

T

F

S2

T

F

S1 S1

S2

Page 5: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

5

Multiple Conditions

Logical Operations

and or not xor

==> <=>

Page 6: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

6

F F

if – elif – else – end_if

F

Nested IF

if

S1 S2

T

elif elif

S3 S4

T T

S1

S2

S3

S4

Page 7: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

7

Case StatementDefine a new procedure.

Page 8: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

8

Return Value

Result of the last executed command

Abs is a function.One of them becomes the return value.

Page 9: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

9

Displaying Intermediate Results

No automatic display inside conditional statements.

Use print to display something.

Page 10: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

10

Exercise In if statements the system evaluates composite condi-

tions with Boolean operators one after the other. The evaluation routine stops prematurely if it can decide whether the final result is TRUE or FALSE (“lazy evalua-tion”). Are there problems with the following statements? What happens when the conditions are evaluated?

Page 11: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

11

For Loops : Fixed No. of Iterations

for loop

with step

Page 12: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

12

For Loops

Down to

Lists, Matrices

Page 13: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

13

While and Repeat

while

Repeat

Page 14: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

14

Nested Loops

Loop in a Loop

Page 15: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

15

Exiting a Loop

break – exit from the nearest loop

Empty List

List Concatenation

Page 16: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

16

Exercise

Write the expected output from the previous commands without using MuPAD.

Page 17: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

17

Skipping Part of Iteration

next

Skip this. Go to the end of the loop.

Page 18: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

18

Return Value of a Loop

The result of the last executed statement

Use a colon to suppress displaying the return value.

Page 19: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

19

Key Takeaways

Now, you are able to control a conditional execution of a set of state-

ments using if and case, use for loop to repeat some actions for a fixed

number of times, check loop exit condition using while and repeat –

until statements, and to control looping actions using next and

break statements.

Page 20: Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox

20

Notes