MATLAB Tutorial. Chapter 6. Writing and calling functions



Similar documents
Beginner s Matlab Tutorial

Introduction to Matlab

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Creating a Java application using Perfect Developer and the Java Develo...

Simple Programming in MATLAB. Plotting a graph using MATLAB involves three steps:

MATLAB Programming. Problem 1: Sequential

Appendix: Tutorial Introduction to MATLAB

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

1 Finite difference example: 1D implicit heat equation

MATLAB DFS. Interface Library. User Guide

Simple File Input & Output

Introduction. Chapter 1

PGR Computing Programming Skills

CD-ROM Appendix E: Matlab

Chapter 7: Additional Topics

MATLAB 7 Creating Graphical User Interfaces

The following matrix is obtained from using central finite differences to discretize the Laplacian operator in 1-D. x = 1:num_pts; grid of x-values

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

Parallel Computing using MATLAB Distributed Compute Server ZORRO HPC

Introduction to Modern Data Acquisition with LabVIEW and MATLAB. By Matt Hollingsworth

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Signal Processing First Lab 01: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

Athena Knowledge Base

CSC 120: Computer Science for the Sciences (R section)

Tutorial Program. 1. Basics

(!' ) "' # "*# "!(!' +,

(!' ) "' # "*# "!(!' +,

Unix Scripts and Job Scheduling

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Data Tool Platform SQL Development Tools

Using Example Projects, Code and Scripts to Jump-Start Customers With Code Composer Studio 2.0

Driving a Time-based Simulation from MATLAB for Custom Calculations and Control

WEEK #3, Lecture 1: Sparse Systems, MATLAB Graphics

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Computational Mathematics with Python

Basic Concepts in Matlab

Basics of STATA. 1 Data les. 2 Loading data into STATA

POISSON AND LAPLACE EQUATIONS. Charles R. O Neill. School of Mechanical and Aerospace Engineering. Oklahoma State University. Stillwater, OK 74078

Beginning Matlab Exercises

CISC 181 Project 3 Designing Classes for Bank Accounts

Appendix A Using the Java Compiler

Hypercosm. Studio.

Hands-On UNIX Exercise:

CET W/32 Application Builder Version 9

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Introduction to Minitab Macros. Types of Minitab Macros. Objectives. Local Macros. Global Macros

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

2+2 Just type and press enter and the answer comes up ans = 4

Computational Mathematics with Python

GUI Input and Output. Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University

SSH Connections MACs the MAC XTerm application can be used to create an ssh connection, no utility is needed.

Using ORACLE in the CSLab

CLC Server Command Line Tools USER MANUAL

1 Topic. 2 Scilab. 2.1 What is Scilab?

Scicos is a Scilab toolbox included in the Scilab package. The Scicos editor can be opened by the scicos command

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

Code::Blocks Student Manual

Moving from CS 61A Scheme to CS 61B Java

Automatic data recovery

2 Matlab Programming, IO, and strings

REXX-like Macro Language for UNIX and Windows

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Build standalone executables from MATLAB code

FIRST STEPS WITH SCILAB

Help File. Version February, MetaDigger for PC

Solving ODEs in Matlab. BP205 M.Tremont

Structural Health Monitoring Tools (SHMTools)

MATLAB Basics MATLAB numbers and numeric formats

Programming Languages & Tools

Tutorial 2: Reading and Manipulating Files Jason Pienaar and Tom Miller

Lecture 4: Writing shell scripts

Computational Mathematics with Python

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Managing Code Development Using CCS Project Manager

MATLAB and Big Data: Illustrative Example

CMPT 373 Software Development Methods. Building Software. Nick Sumner Some materials from Shlomi Fish & Kitware

EXCEL SOLVER TUTORIAL

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VI

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

1 Organizing time series in Matlab structures

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

File Transfer Protocols In Anzio

Glossary of Object Oriented Terms

Changing the Display Frequency During Scanning Within an ImageControls 3 Application

MOVES Batch Mode: Setting up and running groups of related MOVES run specifications. EPA Office of Transportation and Air Quality 11/3/2010

Advanced Bash Scripting. Joshua Malone

by NetMedia, Inc. All rights reserved. Basic Express, BasicX, BX-01, BX-24 and BX-35 are trademarks of NetMedia, Inc. 2.

Data Integrator. Pervasive Software, Inc B Riata Trace Parkway Austin, Texas USA

THE NAS KERNEL BENCHMARK PROGRAM

0 Introduction to Data Analysis Using an Excel Spreadsheet

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Before You Begin... 2 Running SAS in Batch Mode... 2 Printing the Output of Your Program... 3 SAS Statements and Syntax... 3

Transcription:

MATLAB Tutorial Chapter 6. Writing and calling functions In this chapter we discuss how to structure a program with multiple source code files. First, an explanation of how code files work in MATLAB is presented. In compiled languages such as FORTRAN, C, or C++, code can be stored in one or more source files that are linked together to form a single executable at the time of compilation. MATLAB, being an interpreted language, deals with multiple source files in a more open-ed manner. MATLAB code is organized into ASCII files carrying the extension.m (also known as m-files). MATLAB 6 has an integrated word processing and debugging utility that is the preferred mode of editing m-files, although other ASCII editors such as vi or emacs may also be used. There are two different kinds of m-files. The simplest, a script file, is merely a collection of MATLAB commands. When the script file is executed by typing its name at the interactive prompt, MATLAB reads and executes the commands within the m-file just as if one were entering them manually. It is as if one were cutting and pasting the m-file contents into the MATLAB command window. The use of this type of m-file is outlined in section 6.1. The second kind of m-file, discussed in section 6.2, contains a single function that has the same name as that of the m-file. This m-file contains an indepent section of code with a clearly defined input/output interface; that is, it can be invoked by passing to it a list of dummy arguments arg1, arg2,... and it returns as output the values out1, out2,... The first non-commented line of a function m-file contains the function header, which is of the form : function [out1,out2,...] = filename(arg1,arg2,...); The m-file s with the command return, which returns the program execution to the place where the function was called. The function code is executed whenever, either at the interactive command prompt or within another m-file, it is invoked with the command : [outvar1,outvar2,...] = filename(var1,var2,...) with the mapping of input to dummy arguments : arg1 = var1, arg2 = var2, etc. Within the function body, output values are assigned to the variables out1, out2, etc. When return is encountered, the current values of out1, out2,... are mapped to the variables outvar1, outvar2,... at the point where the function was called. MATLAB allows much latitude in writing functions with variable length argument and output variable lists. For example, the function could also be invoked by the command : outvar1 = filename(var1,var2,...) in which case only a single output variable is returned, containing on exit the value of the function variable out1. The input and output arguments may be strings, scalar numbers, vectors, matrices, or more advanced data structures. Why use functions? As is well known from every computer science course, splitting a large program into multiple procedures that perform each a single well defined and commented task, results in programs that are easier to read, easier to modify, and that are more resistant to error. In MATLAB, one writes first a master file for the program, either a script file or better yet a function m-file that returns a single integer (that might return 1 for program success, 0 for incomplete program execution, or a negative value to indicate a run-time error), that is the point of entry to the program. This program file then calls upon code in other m-files by invoking them as functions. But if there is no compilation process to link all of the source code files together, how does MATLAB know where to look for a function when it is called? MATLAB's program memory contains a search path list, the contents of which can be viewed with the command path, that stores the names of the directories it has been told contain function m-files. Initially, the path lists only the directories that hold the built-in MATLAB functions such as sin(), exp(), etc.. As demonstrated in section 6.2, one uses the command addpath to add to this list the name of each directory that contains a m-file for the present project. Then, when the MATLAB code interpreter encounters a function, say with the name filename, it starts at the top of the path list and works its way down searching in each directory for a file filename.m. When it finds it, it executes the file's code in the manner

described above. For this reason, it is imperative that the names of the m-file and of the function agree; in fact it is only the filename that counts. 6.1. Writing and running m-files While MATLAB can be run interactively from the command line, you can write a MATLAB program by composing a text file that contains the commands you want MATLAB to perform in the order in which they appear in the file. The standard file suffix for a text file containing a MATLAB program is.m. In the MATLAB command window, selecting the pull-down menu File - > New -> M-file opens the integrated MATLAB text editor for writing a m-file. This utility is very similar to word processors, so the use of writing and saving m-files is not explained in detail here. As an example, use this secion as a file "MATLAB_tutorial_c6s1.m" that has only the following executable commands. file_name = 'MATLAB_tutorial_c6s1.m'; disp(['starting ' file_name ]); j = 5; for i=1:5 j = j - 1; disp([int2str(i) ' ' int2str(j)]); disp(['finished ' file_name]); We can run this m-file from the prompt by typing its name >> MATLAB_tutorial_c6s1 If we type "whos" now, we see that the variables that are in the memory at the of the program also remain in memory after the m-file is done running. This is because we have written the m-file as a script file where we have simply collected together several commands in a file, and then the code executes them one-by-one when the script is run, as if we were merely typing them into the interactive session window. A more common use for m-files is to isolate a series of commands in an indepent function, as explained in the following section. 6.2. Structured programming with functions Unstructured programming approach File unstructured.m In this section, let us demonstrate the use of subroutines to write structured, well-organized programs. We do so for a particularly simple and familiar case, the simple 1-D PDE problem that we encounted in section 4.1. First, in this m-file, we solve the problem with a program the combines all of the commands into a single file. This "unstructured" approach is fine for very small programs, but rapidly becomes confusion as the size of the program grows. num_pts = 100; # of grid points x = 1:num_pts; grid of x-values We now set the values for the matrix discretizing the PDE with Dirichlet boundary conditions. nza = 3*(num_pts-2) + 2; # of non-zero elements A = spalloc(num_pts,num_pts,nza); allocate memory set values A(1,1) = 1; A(num_pts,num_pts) = 1;

for i=2:(num_pts-1) A(i,i) = 2; A(i,i-1) = -1; A(i,i+1) = -1; Next, we set the values of the function at each boundary. BC1 = -10; value of f at x(1); BC2 = 10; value of f at x(num_pts); We now create the vector for the right hand side of the problem. b_rhs = linspace(0,0,num_pts)'; create column vector of zeros b_rhs(1) = BC1; b_rhs(num_pts) = BC2; b_rhs(2:(num_pts-1)) = 0.05; for interior, b_rhs is source term Now, we call the standard MATLAB solver. f = A\b_RHS; Then, we make a plot of the results. figure; plot(x,f); title('pde solution from FD-CDS method (sparse matrix)'); xlabel('x'); ylabel('f(x)'); While this approach of putting all of the commands together works for this small program, it becomes very unwieldy for large programs. clear all Structured programming approach File structured.m NOTE: BEFORE RUNNING THIS FILE, THE OTHER M-FILES CONTAINING THE SUBROUTINES MUST ALREADY EXIST. First, we define the number of points num_pts = 100; We now create a vector containing the grid points. x = 1:num_pts; In MATLAB, each function is stored in a separate m-file of the same name. When you call the function at the interactive session prompt or in another script or funtcion m-file, MATLAB searches through a list of directories that it has been told contain functions until it finds an m- file with the appropriate name. Then, it executes the MATLAB code contained within that m- file. When we write m-files that contain a functions, before we can use them we have to tell MATLAB where they are; that is, we have to add the name of their directory to the search path. We can check the current contents of the search path with the command "path". path The command "pwd" returns the current directory. pwd

We use the command "addpath" to add the directory with our subroutines to this search list. We can remove a directory from the path using "rmpath". addpath(pwd); path The following function calculates the A matrix. A function call has the following syntax : [out1,out2,...] = func_name(in1,in2,...), where the input arguments are the variables in1,in2,... and the output from the function is stored in out1,out2,... In our case, the input is the dimension of the matrix A, num_pts, and the output variables are A and iflag, an integer that tells us if the code was performed sucessfully. [A,iflag] = c6s2_get_a(num_pts); if(iflag ~= 1) then error disp(['c6s2_get_a returned error flag : ' int2str(iflag)]); We also see from the code below that the existence of a local variable in the function named i does nothing to alter the value of i at the point of calling. i = 1234; [A,iflag] = c6s2_get_a(num_pts); i Next, we ask the user to input the function values at the boundaries. BC1 = input('input the function value at x = 1 : '); BC2 = input('input the function value at x = num_pts : '); source = input('input the value of the source term : '); We now call upon another subroutine that calculates the vector for the RHS. [b_rhs,iflag] = c6s2_get_b_rhs(num_pts,bc1,bc2,source); We now solve the system. f = A\b_RHS; Then, we make plots of the output. figure; plot(x,f); phrase1 = ['PDE solution with source = ' num2str(source)]; phrase1 = [phrase1 ', BC1 = ' num2str(bc1)]; phrase1 = [phrase1 ', BC2 = ' num2str(bc2)]; title(phrase1); xlabel('x'); ylabel('f(x)'); Then, to clean up, we clear the memory clear all File c6s2_get_a.m The first executable line of the m-file declares the name and input/output structure of the subroutine using the "function" command. function [A,iflag] = c6s2_get_a(ndim); iflag = 0; signifies job not complete If Ndim < 1, then we have an error, since we can't have a matrix with a dimension less than 1. if(ndim<1) signify error

A=-1; iflag = -1; we return control to the m-file that called this subroutine without executing the rest of the code. First, we declare A using sparse matrix format. nza = 3*(Ndim-2) + 2; # of non-zero elements A = spalloc(ndim,ndim,nza); allocate memory A(1,1) = 1; A(Ndim,Ndim) = 1; for i=2:(ndim-1) A(i,i) = 2; A(i,i-1) = -1; A(i,i+1) = -1; iflag = 1; signify job complete and successful return control to the m-file that called this routine File c6s2_get_b_rhs.m function [b_rhs,iflag] = c6s2_get_b_rhs(num_pts,bc1,bc2,source); iflag = 0; declares job not completed if(num_pts < 3) not enough points iflag = -1; b_rhs = -1; We allocate space for b_rhs and initialize to zeros. b_rhs = linspace(0,0,num_pts)'; Now, we specify the first and last components from the boundary conditions. b_rhs(1) = BC1; b_rhs(num_pts) = BC2; Next, we specify the interior points. for i=2:(num_pts-1) b_rhs(i) = source; iflag=1; signifies successful completion 6.3. Inline functions Sometimes, we do not want to go through the bother of writing a separate m-file to define a function. For these times, we can define an inline function. Let us say that we want to define the function f1(x) = 2*x + 3*x^2

We can define this function using f1 = inline('2*x + 3*x^2'); Then, we can call this function directly f1(1), f1(23) We can also define functions using vectors and matrices as input. invariant2 = inline('(trace(a)*trace(a) - trace(a*a))/2'); A = rand(3); invariant2(a) We can check the definition of the function by typing its name invariant2 While this is convenient, the execution of inline functions is rather slow. clear all; try invariant2 catch disp('we see that inline functions are cleared also'); 6.4. Functions as function arguments The function, trig_func_1, listed below, returns the value of f(x) = a*sin(x) + b*cos(x) for given values of a, b, and x. The function, plot_trig_1, listed below, plots a function on the domain 0 to 2*pi. The following code asks the user to input values of a and b, and then uses plot_trig to plot trig_func_1 by including the function name as an argument in the list. disp('plotting a*sin(x) + b*cos(x)...'); a = input('input a : '); b = input('input b : '); func_name = 'trig_func_1'; make sure current direction is in the path addpath(pwd) plot_trig_1(func_name,a,b); clear all File trig_func_1.m function f_val = trig_func_1(x,a,b); f_val = a*sin(x) + b*cos(x); File plot_trig_1.m function iflag = plot_trig_1(func_name,a,b); iflag = 0; signifies no completion

First, create an x vector from 0 to 2*pi num_pts = 100; x = linspace(0,2*pi,num_pts); Next, make a vector of the function values. We evaluate the argument function indirectly using the "feval" command. f = linspace(0,0,num_pts); for i=1:num_pts f(i) = feval(func_name,x(i),a,b); Then, we make the plot. figure; plot(x,f); xlabel('angle (radians)'); ylabel('function value');