Download presentation
Presentation is loading. Please wait.
Published byMolly York Modified over 9 years ago
1
Function M-File Numerical Computing with. MATLAB for Scientists and Engineers
2
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
3
Anatomy of Function 3 mmempty.m function d = 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 function d = 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 keyword H1 Line (lookfor) input arguments output arguments function name = file name 1~63 lower case alphanumeric characters function name = file name 1~63 lower case alphanumeric characters Help Message Function Body
4
Demo: Simple Function Write a function m-file 'func1.m' for the following expression. Run the function in the command window. 4 func1.m >> func1(4) >> a = 0:0.1:5; >> plot(a, func1(a)) >> a = 0:0.1:5; >> plot(a, func1(a))
5
Input – Output Arguments Various forms of function definitions 5 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)
6
Exercise 1: Polar Function Write a function m-file for Use the function to calculate: Use the function to plot (polar plot) for 6
7
Solution 1 Function m-file Commands and Screenshot 7
8
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. 8 global SPEED_OF_LIGHT;
9
Script and Function M-Files ItemScript m-fileFunction m-file extension.m 1 st lineanyfunction definition variable recognized in command window local to the function usagebatch jobfunction, procedure I / Othrough variablesthrough arguments file nameanythe function name 9
10
Anonymous Function Simple one-line user-defined function Examples Usage 10 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])
11
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<5 with a=-b= 1, 1.5, 2, 2.5 11
12
Solution 2 Script and Screenshot 12 parabolas.m
13
Sub-functions 1/2 A function m-file may contain several function definitions. The 1 st function is the primary function and the others are sub-functions, which are available only to the primary function. 13 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)... 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. Why sub-functions? Divide and conquer.
14
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
15
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. 15
16
Solution 3 Function m-file 16 det3x3
17
Solution 3 Commands and Screenshot 17
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.