210 likes | 420 Views
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. Anatomy of Function. keyword.
E N D
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.
Anatomy of Function keyword H1 Line (lookfor) mmempty.m functiond = mmempty(a,b) %MMEMPTY Substitute Value if EMpty % MMEMPTY(A,B) returns A if A is not empty, % otherwise B is returned. % if isempty(a) d = b; else d = a; end input arguments output arguments Help Message function name = file name 1~63 lower case alphanumeric characters Function Body
Demo: Simple Function • Write a function m-file 'func1.m' for the following expression. • Run the function in the command window. func1.m >> func1(4) >> a = 0:0.1:5; >> plot(a, func1(a))
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)
Exercise 1: Polar Function • Write a function m-file for • Use the function to calculate: • Use the function to plot (polar plot) for
Solution 1 • Function m-file • Commands and Screenshot
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;
Anonymous Function • Simple one-line user-defined function • Examples • Usage name = @ (arglist) expr cube = @ (x) x^3 ex3r = @ (x, y) sqrt(x.^2+y.^2) y = cube(23.4) z = ex3r( [1 2], [2 0])
Exercise 2: Set of Parabolic Curves • Define an anonymous function for 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<5with a=-b= 1, 1.5, 2, 2.5
Solution 2 • Script and Screenshot parabolas.m
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; function k = f2( x) ... Why sub-functions? Divide and conquer.
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.
Exercise 3 Determinant of 3x3 Matrix • Write a function m-file, det3x3.m, for calculating the determinant of 3x3 matrix using the following formula. Use d = det3x3(A), where A is a 3x3 matrix. Use a sub-function that calculates the 2x2 determinant. • Calculate the determinant of the matrices on the right.
Solution 3 • Function m-file det3x3
Solution 3 • Commands and Screenshot
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