Вы находитесь на странице: 1из 4

Group Project Rajsekhar Nalitham Ashin Maniar Krupal Patel USER-DEFINED FUNCTION CHAPTER 5 In this chapter we learn about

making user defined functions. A user-defined function is a Matlab program that is created by the user, saved as a function file, and then can be used like a built-in function. To make a function one must begin by making an mfile. This involves defining the function first by naming the function and including input and output arguments. Each Matlab function should be placed in the file with the same name as the function with the proper Matlab style. The input argument lists the values the user wants to insert into the variables. These are often called dummy arguments. Following the function header is the H1 line. The H1 line tells Matlab keywords that describe the function, so when the user searches the command line, Matlab can easily locate the correct function. Following the H1 line is the user defined executable code. In this code the user can define what the function will do, and the result attained from the function. After the executable code is placed into the function a return statement followed by an end statement concludes the functions purpose. The following example is a setup of what a function looks like:
Function [output1, output2, . . . ] = functionname(input1, input2,. . . ) ... (Executable Code) ... end Example Slope Function: Function [ y] = slope(x, m, b) %Calculates the y value of a line y = mx + b end

This function is created so the user can enter the slope, x-value, and the y-intercept to get a y value of a line. This function holds the code to calculate the slope of a line. User-Defined Functions can be broken down to many parts. The most important, however is the variables being pass in and out of Matlab. Matlab works on a Pass-byValue scheme. Meaning when a user calls the function with an input value, Matlab makes a copy of that input value so that it cant interfere with the change that occurs within the function. By creating a copy of the input variable, Matlab assures the user that at any given moment in the function; the input value will remain the same and not change accordingly to the workspace.
Example: Function output = sample(A,B,C) A=(B+2)*C^2 B=A*3.14+360 OUT=A*B End

In this function, we see that the user calls a function called sample and entered the input value A, B, and C. The first code takes the value of (B+2)*C^2 and puts it in A. However when A*3.14+360 is called on the second line, Matlab uses the Pass-by-Value scheme and takes the value of the A from the original input argument than the A computed by the code above. There are many things Matlab does on its own to help the user. For example, Matlab can know how many input and output argument the user passes in a function. By know this piece of information; Matlab can easily decide what values to output when the output arguments defined by the user exceed two. To do this, Matlab uses build in function to gather such information. There are 6 total build in functions we learn about in this chapter. nargin- this function returns the number of actual input arguments that were used to call the function
This example shows portions of the code for a function called plot, which accepts an optional number of input arguments: function plot(x, y, npts, angle, subdiv) if nargin < 5, subdiv = 20; end if nargin < 4, angle = 10; end if nargin < 3, npts = 25; end plot(x, y) end

narout- this function returns the number of actual output arguments that were used to call the function
This example shows portions of the code for a function called myplot, which accepts an optional number of output arguments: function [x0, y0] = myplot(x, y, npts, angle, subdiv) if nargout == 0 plot(x, y) else x0 = x; y0 = y; end

nargchk- this function returns a standerd error message if the function is called with two few or too many argument
Syntax: Message= nargchk(min_args,max_args,num_args)

error- displays error message and abort the function producing the error. This function is used if the argument errors are fatal warning- displays warning message and continues the function execution. This function is used if the argument errors are not fatal and execution can continue. Inputname- this function returns the actual name of the variable that corresponds to a particular argument number A related topic to optional argument is Preserving data between calls to a function. Unlike the optional argument where the data is copied, the Matlab deletes the local

arguments created when running a function, But sometimes it is useful to have the local arguments saved so that they can be called by the user later on. To do this a new argument MUST be followed by the work persistent. This insures that that particular argument is not deleted when the function is done, so that the user can call it again.
Example of what a persistent argument should look like Persistent testing1 testing2 testing3 Where the persistent variables will be placed in the persistent memory location

In addition to User defined functions, there are Subfunctions, Private functions, Nested functions. A subfunction is a dunction within a function. Each subfunction begins with its own function definition line. The functions immediately follow each other. The various subfunctions can occur in any order, as long as the primary function appears first. A Private function is Private Functions are functions that reside in subdirectories with the special name private. These functions are called private because they are visible only to M-file functions and M-file scripts that meet these conditions:

A function that calls a private function must be defined in an M-file that resides in the directory immediately above that private subdirectory. A script that calls a private function must itself be called from an M-file function that has access to the private function according to the above rule.

You can define one or more functions within another function in MATLAB. These inner functions are said to be nested within the function that contains them. You can also nest functions within other nested functions. To write a nested function, simply define one function within the body of another function in an M-file. Like any M-file function, a nested function contains any or all of the components described in Basic Parts of an M-File. In addition, you must always terminate a nested function with an end statement
Example of a subfunction: function [avg, med] = newstats(u) % Primary function % NEWSTATS Find mean and median with internal functions. n = length(u); avg = mean(u, n); med = median(u, n); function a = mean(v, n) % Subfunction % Calculate average. a = sum(v)/n; function m = median(v, n) % Subfunction % Calculate median. w = sort(v); if rem(n, 2) == 1 m = w((n+1) / 2);

else m = (w(n/2) + w(n/2+1)) / 2; end The subfunctions mean and median calculate the average and median of the input list. The primary function newstats determines the length of the list and calls the subfunctions, passing to them the list length n.

Example of a Nested function: function x = A(p1, p2) ... function y = B(p3) ... end ... end

In book Programming in Matlab for Engineers, author Stephen J. Shapman did a great job presenting the Matlab language. The way the chapters were presented, it made it easy for the students to follow along. Also the idea of giving the background and following it by an example for each section and subsection made it clear to what the purpose was. Also, the examples provided in the chapter were really helpful because they give an insight to how to code properly as well as give the student an understanding of the material. And finally the quizzes and the problems provided in the back of each chapter were very helpful because they are a good practice as to how one might expert at coding. It also helped understand the chapter better.