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

Post on 01-Jan-2016

240 Views

Category:

Documents

7 Downloads

Preview:

Click to see full reader

Transcript

Chapter 9:

MuPAD Programming IIProcedures

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

3

Using MuPAD Editor

Invoking MuPAD Editor

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

4

Bookmarks

Add Bookmarks

Go to the Bookmark Delete all Bookmarks

5

Creating a Procedure

proc - procedure

Name of the procedure

Body

Suppress the procedure definition message

or just end

or just end

factorial.mu

6

Using the Procedure

Read-in the MuPAD file.factorial.mu

7

Return Value

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

Use the return command for other results.

8

Returning Multiple Results

Any MuPAD object can be returned.

9

Exercise

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

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!

11

Global Variables

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

To use more decimal digits inside the procedure.

12

Default Values for Arguments

You can give a default value for an argument.

default value

with argument

without argument

13

error Function

Aborts with the message.

14

Sub-procedure

Local procedure inside a procedure

g() is visible only inside this procedure.

15

Type Declaration

Specify the type of input argument

Accepts only non-negative integers.

16

Variable Number of Arguments

System function max

Accessing arguments

17

Simulating System Function max

18

Var. No. of Args : More Examples

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

Returns itself symbolically.

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.

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.

21

option noExpose

You can hide your code and publish it.

22

Start Debugging

Menu Notebook Debug

Command

23

Debug Control

Step Over

Step Into

Step Out

Continue

Toggle Breakpoint

Watch

Evaluate

24

Modifying Source

File New Editor …

Re-read the file or

Copy & Paste

to the Notebook.

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.

26

Notes

27

Notes

28

Notes

top related