1 / 18

Function M-File

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.

malana
Download Presentation

Function M-File

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.

  3. 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

  4. 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))

  5. 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)

  6. Exercise 1: Polar Function • Write a function m-file for • Use the function to calculate: • Use the function to plot (polar plot) for

  7. Solution 1 • Function m-file • Commands and Screenshot

  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. global SPEED_OF_LIGHT;

  9. Script and Function M-Files

  10. 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])

  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<5with a=-b= 1, 1.5, 2, 2.5

  12. Solution 2 • Script and Screenshot parabolas.m

  13. 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.

  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.

  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.

  16. Solution 3 • Function m-file det3x3

  17. Solution 3 • Commands and Screenshot

  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

More Related