Download presentation
Presentation is loading. Please wait.
Published byMarybeth Knight Modified over 9 years ago
1
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 procedures 2
3
Using MuPAD Editor Invoking MuPAD Editor Auto-indentation: TAB Comments: //, /*.. */ 3
4
Bookmarks Add Bookmarks Go to the Bookmark Delete all Bookmarks 4
5
Creating a Procedure proc - procedure 5 Name of the procedure Body Suppress the procedure definition message or just end factorial.mu
6
Using the Procedure Read-in the MuPAD file. 6 factorial.mu
7
Return Value By default, a procedure returns the result of the last executed command. Use the return command for other results. 7
8
Returning Multiple Results Any MuPAD object can be returned. 8
9
Exercise Implement a procedure maximum(a, b) returning the maximum value of two arguments. 9
10
Local Variables You can declare an arbitrary number of local variables by specifying a sequence of identifiers after local keyword. If you don't use local keyword for a variable, it becomes global variable. 10 Local Variable DO NOT FORGET to initialize all the local variables! DO NOT FORGET to initialize all the local variables!
11
Global Variables Do not use Global Variables! If you must, save it first. 11 To use more decimal digits inside the procedure. To use more decimal digits inside the procedure.
12
Default Values for Arguments You can give a default value for an argument. 12 default value with argument without argument
13
error Function 13 Aborts with the message.
14
Sub-procedure Local procedure inside a procedure 14 g() is visible only inside this procedure. g() is visible only inside this procedure.
15
Type Declaration Specify the type of input argument 15 Accepts only non-negative integers.
16
Variable Number of Arguments System function max Accessing arguments 16
17
Simulating System Function max 17
18
Var. No. of Args : More Examples You may use both formal parameters and accesses via args in a procedure: Returns itself symbolically. 18
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. 19
20
Exercise We define the function f : N → N by The “ (3 x + 1) problem” asks whether for an arbitrary initial value x 0 ∈ N, the sequence recursively defined by x i +1 := f(x i ) contains the value 1. Write a program that on input x 0 returns the smallest index i with x i = 1. 20
21
option noExpose You can hide your code and publish it. 21
22
Start Debugging Menu Notebook Debug Command 22
23
Debug Control 23 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. 24
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. 25
26
Notes 26
27
Notes 27
28
Notes 28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.