chapter 9: mupad programming ii procedures matlab for scientist and engineers using symbolic toolbox

28
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

Upload: marybeth-knight

Post on 01-Jan-2016

242 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

Chapter 9:

MuPAD Programming IIProcedures

MATLAB for Scientist and Engineers

Using Symbolic Toolbox

Page 2: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

2

You are going to See that MuPAD provides built-in editor for

programming Implement simple MuPAD procedures Use local variables inside the procedures Handle variable number of arguments to pro-

cedures

Page 3: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

3

Using MuPAD Editor

Invoking MuPAD Editor

Auto-indentation: TAB Comments: //, /* .. */

Page 4: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

4

Bookmarks

Add Bookmarks

Go to the Bookmark Delete all Bookmarks

Page 5: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

5

Creating a Procedure

proc - procedure

Name of the procedure

Body

Suppress the procedure definition message

or just end

or just end

factorial.mu

Page 6: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

6

Using the Procedure

Read-in the MuPAD file.factorial.mu

Page 7: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

7

Return Value

By default, a procedure returns the result of the last executed command.

Use the return command for other results.

Page 8: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

8

Returning Multiple Results

Any MuPAD object can be returned.

Page 9: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

9

Exercise

Implement a procedure maximum(a, b) re-turning the maximum value of two arguments.

Page 10: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

10

Local Variables

You can declare an arbitrary number of local variables by specifying a sequence of identi-fiers after local keyword.

If you don't use local keyword for a vari-able, it becomes global variable.

Local Variable

DO NOT FORGET to initialize all the local variables!

Page 11: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

11

Global Variables

Do not use Global Variables! If you must, save it first.

To use more decimal digits inside the procedure.

Page 12: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

12

Default Values for Arguments

You can give a default value for an argument.

default value

with argument

without argument

Page 13: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

13

error Function

Aborts with the message.

Page 14: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

14

Sub-procedure

Local procedure inside a procedure

g() is visible only inside this procedure.

Page 15: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

15

Type Declaration

Specify the type of input argument

Accepts only non-negative integers.

Page 16: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

16

Variable Number of Arguments

System function max

Accessing arguments

Page 17: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

17

Simulating System Function max

Page 18: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

18

Var. No. of Args : More Examples

You may use both formal parameters and ac-cesses via args in a procedure:

Returns itself symbolically.

Page 19: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

19

Exercise

Write a short procedure date that takes three integers month, day, year as input and prints the date in the usual way. For example, the call date(5, 3, 1990) should yield the screen output 5/3/1990.

Page 20: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

20

Exercise

We define the function f : N → N by

The “(3 x + 1) problem” asks whether for an arbi-trary initial value x0 ∈ N, the sequence recur-sively defined by xi+1 := f(xi) contains the value 1. Write a program that on input x0 returns the smallest index i with xi = 1.

Page 21: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

21

option noExpose

You can hide your code and publish it.

Page 22: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

22

Start Debugging

Menu Notebook Debug

Command

Page 23: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

23

Debug Control

Step Over

Step Into

Step Out

Continue

Toggle Breakpoint

Watch

Evaluate

Page 24: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

24

Modifying Source

File New Editor …

Re-read the file or

Copy & Paste

to the Notebook.

Page 25: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

25

Key Takeaways

Now, you are able to use MuPAD editor for writing your procedures, implement simple procedures and specify return

values deal with the variable number of arguments perform type checking on the input arguments. and debug your procedures.

Page 26: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

26

Notes

Page 27: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

27

Notes

Page 28: Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox

28

Notes