Function M-File Numerical Computing with . MATLAB for Scientists and Engineers You will be able to Write a function m-file, Tell the differences between local and global variables, Use sub-functions and nested functions. 2 Anatomy of Function mmempty.m keyword H1 Line (lookfor) function d = mmempty(a,b) %MMEMPTY Substitute Value if EMpty % MMEMPTY(A,B) returns A if A is not empty, input arguments % otherwise B is returned. output % arguments Help Message if isempty(a) function name = file name d = b; 1~63 lower case else alphanumeric characters d = a; Function Body end 3 Demo: Simple Function Write a function m-file 'func1.m' for the following expression. f ( x) e x cos x func1.m Run the function in the command window. >> func1(4) >> a = 0:0.1:5; >> plot(a, func1(a)) 4 Input – Output Arguments Various forms of function definitions function [mpay, tpay] = loan(amount, rate, years) function [A] = RectArea(a, b) function A = RectArea(a, b) function [V, S] = SphereVolArea(r) function trajectory(v, h, g) 5 Exercise 1: Polar Function Write a function m-file for r ( ) sin(3 ) cos( ) Use the function to calculate: r ( / 4), r (5 / 2) Use the function to plot (polar plot) r ( ) for 0 2 6 Solution 1 Function m-file Commands and Screenshot 7 Local and Global Variables All variables in a function m-file is local. Local variables are not shared between function files / the command window. Use global keyword for global variables. global SPEED_OF_LIGHT; 8 Script and Function M-Files Item Script m-file Function m-file extension .m .m 1st line any function definition variable recognized in command window local to the function usage batch job function, procedure I/O through variables through arguments file name any the function name 9 Anonymous Function Simple one-line user-defined function name = @ (arglist) expr Examples cube = @ (x) x^3 ex3r = @ (x, y) sqrt(x.^2+y.^2) Usage y = cube(23.4) z = ex3r( [1 2], [2 0]) 10 Exercise 2: Set of Parabolic Curves Define an anonymous function for f (a, b, x) ( x a)( x b) where a and b are scalar values while x can be a vector. Use the anonymous function to plot a set of parabolic curves for -5<x<5 with a=-b= 1, 1.5, 2, 2.5 11 Solution 2 Script and Screenshot parabolas.m 12 Sub-functions 1/2 A function m-file may contain several function definitions. The 1st function is the primary function and the others are sub-functions, which are available only to the primary function. f1.m funciton y = f1( a, b, c ) % f1 is the main funciton z = f2(a); w = f3(b,c); y = z .* w; Why sub-functions? Divide and conquer. function k = f2( x) ... 13 Sub-functions 2/2 Variables in each function are private to the function. Only the primary function may be called from outside. Each function can call each other in the function m-file. 14 Exercise 3 Determinant of 3x3 Matrix Write a function m-file, det3x3.m, for calculating the determinant of 3x3 matrix using the following formula. det( A) A11 A22 A23 A32 A33 A12 A21 A23 A31 A33 A13 A21 A22 A31 A32 Use d = det3x3(A), where A is a 3x3 matrix. Use a sub-function that calculates the 2x2 determinant. e 3 3 2 1 Calculate the determinant 6 5 7 e 2 e 9 4 8 of the matrices on the right. 5 e 15 Solution 3 Function m-file det3x3 16 Solution 3 Commands and Screenshot 17 Variable Number of Arguments 0+ Input Arguments / 0+ Output Arguments Can be called with fewer arguments nargin : # of actual input arguments nargout : # of actual output arguments 18