nurul farahain user-de ned matlab...

21
MATLAB Programs Nurul Farahain Mohammad User-Defined Functions Types Return more than One Value Returning No Values Return Values VS Printing Passing Arguments to Functions MATLAB Program Organization Modular Programs Subfunctions Application: Menu-Driven Modular Program Variable Scope Persistent Variables Debugging Techniques Types of Errors Tracing Editor/Debugger Function stubs Code Cells and Publishing Code MATLAB Programs SMS 3515: Scientific Computing Nurul Farahain Mohammad Department of Computational and Theoretical Sciences, Kulliyyah of Science, International Islamic University Malaysia. Sem 1 2014/2015

Upload: lymien

Post on 31-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

MATLAB ProgramsSMS 3515: Scientific Computing

Nurul Farahain Mohammad

Department of Computational and Theoretical Sciences,Kulliyyah of Science, International Islamic University Malaysia.

Sem 1 2014/2015

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

More Types of User-Defined Functions

I Functions can be categorized into:I functions that calculate and return one value.I functions that calculate and return more than one value.I functions that just accomplish a task without returning

any values i.e. printing.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

More Types of User-Defined Functions

I In general, any function in MATLAB consists of thefollowing:

I The function header (the first line); this has:I the reserved word function.I the name(s) of the output argument(s). followed by

the assignment operator =, if the function returnsvalues.

I the name of the function (note: this should be thesame as the name of the m-file in which this functionis saved to avoid confusion)

I the input arguments in parentheses, if there are any(separated by commas if there is more than one).

I A comment that describes what the function does (thiswill be shown when using help).

I The body of the function, which includes all statements,including putting values in all output arguments if thereare any.

I end at the end of the function.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Functions that Return more than One Value

I General form (file name is functionname.m):

function [output arguments]=functionname(input arguments)

% Comment describing the function

Statements here; these must include putting values in

all of the output arguments listed in the header

end

I In the vector of output arguments, the output arguments namesare separated by commas.

I In recent version of MATLAB, a template of user-defined functionis provided at the Editor by clicking New, and then chooseFunction at the toolbar.

I As the function is returning two values, it is important to captureand store these values in separate variables when the function iscalled. If this is not done, only the first value returned is retained.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Functions that Accomplish a Task WithoutReturning Values

I As these functions do not return any values, there areno output arguments and no assignment operator = inthe function header.

I General form (file name is functionname.m):

function functionname(input arguments)

% Comment describing the function

Statements here;

end

I This type of function cannot be called from anassignment statement.

I Tasks accomplished by functions that do not return anyvalues are referred as side effects.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Functions that Return Values Versus Printing

I If a function just prints a value, rather than returning it(’save’ it), the value cannot be used later in othercalculations.

I A function that calculates and returns values does notnormally also print them. Usually it is left to the callingscript or function.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Passing Arguments to Functions

I Call-by-value method: method of passing the values ofthe arguments to the input arguments in the functions.

I In some cases, it is not necessary to pass any argumentsto the function. i.e. a function of print tasks only.

I General form of no input and no output arguments (filename is functionname.m):

function functionname

% Comment describing the function

Statements here;

end

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Passing Arguments to Functions

I General form of no input but with output arguments(file name is functionname.m):

function [output arguments] = functionname

% Comment describing the function

Statements here;

end

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Modular Programs

I Modular program: a program in which the solution isbroken down into modules, and each is implemented asa function.

I Main program: a script that calls functions.

I In a modular program, there would be one main scriptthat calls appropriate functions to accomplish desiredtasks.

I For each modules, there would be separate m-filefunctions, and later one m-file script as the mainprogram.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Subfunctions

I It is possible to have more than one function in a givenm-file.

I Primary function: A function that calls other function/s.

I Subfunction: A function that is called by other function.

I Both primary function and subfunction/s are stored inthe same m-file; first the primary function and then thesubfunction/s.

I Name of the m-file would be the same as the name ofthe primary function.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Application: Menu-Driven Modular Program

I Menu-driven program: programs that have interactionwith the user. Usually print a menu of choices and thencontinues to loop to print the menu of choices until theuser chooses to end the program.

I A modular menu-driven program would typically have:I a function that represents the menu and gets the user’s

choice.I functions to implement the action for each choice (may

involve subfunctions).I a function that would error-check all user input.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Variable Scope

I Scope of any variable: the workspace in which it is valid.

I Base workspace: the workspace created in theCommand Window.

I A variable defined in any function is a local variable tothat function (only used and known within thatfunction).

I Local variables only exist while the function is executing;they cease to exist when the function stops executing.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Variable Scope

I Variables that are defined in the Command Windowcannot be used in a function unless passed as argumentsto the function. However, scripts interact with variablesthat are defined in the Command Window.

I Variables in script do become part of the baseworkspace.

I It is recommended to start your script with commandclear to eliminate variables that may have already beencreated.

I Writing a main function instead of a script, whendealing with many functions, is recommended.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Persistent Variables

I Persistent variable: a variable which its values are notcleared even though a function stops executing. Thevariable still exists and retains its former value when thenext time the same function is called.

I persistent variablename: declare a persistent variable.

I The way to restart a persistent variable is to use theclear function.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Debugging Techniques

I Bug: any error in a computer program.

I Debugging: the process of finding errors in a program,and correcting them.

I checkcode(’filename’): help find mistakes or potentialproblems in script and function files. Unfortunately thisfunction is not available yet in Octave.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Types of Errors

I Syntax errors: mistakes in using the language.

I Runtime, or execution errors: Errors are found when ascript or function is executing.

I Logical errors: mistakes in reasoning by theprogrammer, but it is not a mistake in the programminglanguage.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Tracing

I echo: display every statement as it is executed, as wellas results from the code. For script, simply type echo tocall this function.

I echo functionname on/off : calling echo for function.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Editor/Debugger

I Ways to set breakpoints in an m-file:I dbstop filename linenumber : set a breakpoint in

filename at the linenumber.I dbcont: continue execution.I dbquit: quit debug mode.I dbstep: step through the rest of the code one line at a

time.

I click on the breakpoint alley (in between the linenumbers on the left and the lines of code; which is athin gray strip) in the Editor. The red dot indicates abreakpoint.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Function stubs

I Function stubs: a place holder, used so that the scriptwill work even though that particular function hasn’tbeen written yet.

I Put in place so that the script can be executed andtested.

I Consist of proper function headers, followed by asimulation of what the function will eventually do.Then the functions can be written and debugged one ata time.

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Code Cells and Publishing Code

I Code cellsI Sections that break a code.I Run one cell at a time.I Can publish the code in an HTML format with plots

embedded and with formatted questions.

I How to break code into cells: Create comment linesthat begin with two % symbols (these becomes the celltitles).

MATLABPrograms

Nurul FarahainMohammad

User-DefinedFunctions Types

Return more thanOne Value

Returning No Values

Return Values VSPrinting

Passing Arguments toFunctions

MATLAB ProgramOrganization

Modular Programs

Subfunctions

Application:Menu-DrivenModular Program

Variable Scope

Persistent Variables

DebuggingTechniques

Types of Errors

Tracing

Editor/Debugger

Function stubs

Code Cells andPublishing Code

Code Cells and Publishing Code

I When viewing in Editor, the individual cells can bechosen by clicking the mouse anywhere within the cell.

I From Editor tab, you can choose:I Run Section: to run just that one code cell and remain

within that cell.I Run and Advance: to run that code cell and then

advance to the next.I Publish: to publish the code by default in HTML

document.