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

Post on 18-Jan-2016

254 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 8:

MuPAD Programming IConditional 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

3

type and testtype

Useful for checking some conditions

Cf. domtype()

For all available Types

4

IF statement

if condition if condition – else

x<0

S1

x<0

S1

T

F

S2

T

F

S1 S1

S2

5

Multiple Conditions

Logical Operations

and or not xor

==> <=>

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

7

Case StatementDefine a new procedure.

8

Return Value

Result of the last executed command

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

9

Displaying Intermediate Results

No automatic display inside conditional statements.

Use print to display something.

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?

11

For Loops : Fixed No. of Iterations

for loop

with step

12

For Loops

Down to

Lists, Matrices

13

While and Repeat

while

Repeat

14

Nested Loops

Loop in a Loop

15

Exiting a Loop

break – exit from the nearest loop

Empty List

List Concatenation

16

Exercise

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

17

Skipping Part of Iteration

next

Skip this. Go to the end of the loop.

18

Return Value of a Loop

The result of the last executed statement

Use a colon to suppress displaying the return value.

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.

20

Notes

top related