Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

function

Declare function name, inputs, and outputs

Syntax
function [y1,...,yN] = myfun(x1,...,xM)

function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts


inputs x1,...,xM and returns outputs y1,...,yN. This declaration statement must be the first
executable line of the function. Valid function names begin with an alphabetic character, and can contain
letters, numbers, or underscores.

You can save your function:


 In a function file which contains only function definitions. The name of the file must match the name of
the first function in the file.
 In a script file which contains commands and function definitions. Functions must be at the end of the
file. Script files cannot have the same name as a function in the file.
Examples
Define a function in a file named calculateAverage.m that accepts an input vector, calculates the
average of the values, and returns a single result.
function ave = calculateAverage(x)
ave = sum(x(:))/numel(x);
end
Call the function from the command line.
z = 1:99;
ave = calculateAverage(z)
ave =
50

Function with Multiple Outputs


function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end
Call the function from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];


[ave,stdev] = stat(values)
ave =
47.3400
stdev =
29.4124

Multiple Functions in a Function File


function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end

Local Functions
In a function file, the first function in the file is called the main function. This function is visible to functions
in other files, or you can call it from the command line. Additional functions within the file are called local
functions, and they can occur in any order after the main function. Local functions are only visible to other
functions in the same file. They are equivalent to subroutines in other programming languages, and are
sometimes called subfunctions.

function b = myfunction(a)
b = squareMe(a)+doubleMe(a);
end
function y = squareMe(x)
y = x.^2;
end
function y = doubleMe(x)
y = x.*2;
end

Nested Functions
A nested function is a function that is completely contained within a parent function. Any function in a
program file can include a nested function.

function parent
disp('This is the parent function')
nestedfx

function nestedfx
disp('This is the nested function')
end

end

Example 2:

function main
nestedfun1
nestedfun2

function nestedfun1
x = 1;
end

function nestedfun2
x = 2;
end
end

User Defined Inline Functions:


function_name = inline(‘expression’, ‘variable’)

Example: func = inline(' x^3 + x^2 + x','x')

Anonymous Functions Without a File


Anonymous functions allow you to define a function without creating a program file, as long as the
function consists of a single statement. a function that accepts a function handle as an input.

s = @(x) sin(1./x);
You can use the function handle to evaluate the function for particular values, such as

y = s(pi)
y = 0.3130

Example 2:

sqr = @(x) x.^2;


a = sqr(5)
a =
25
Example 3:

a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c;
Local Variables

The names of the input variables given in the function definition line are local to that function. This
means that other variable names can be used when you call the function. All variables inside a function
are erased after the function finishes executing, except when the same variable names appear in the
output variable list used in the function call.

Global Variables

The global command declares certain variables global, and therefore their values are available to the
basic workspace and to other functions that declare these variables global. The syntax to declare the
variables A, X, and Q is global A X Q. Use a space, not a comma, to separate the variables.

Function Handles

A function handle is a way to reference a given function. First introduced in MATLAB 6.0, function
handles have become widely used and frequently appear in examples throughout the MATLAB
documentation. You can create a function handle to any function by using the @ sign before the
function name.

Debugging methods in MATLAB:

Debugging a program is the process of ending and removing the “bugs,” or errors, in a program. Such
errors usually fall into one of the following categories.

1. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly.
MATLAB usually detects the more obvious errors and displays a message describing the error and its
location.

2. Errors due to an incorrect mathematical procedure, called runtime errors. They do not necessarily
occur every time the program is executed; their occurrence often depends on the particular input data.
A common example is division by zero.

To locate an error, try the following:

1. Always test your program with a simple version of the problem, whose answers can be checked by
hand calculations.
2. Display any intermediate calculations by removing semicolons at the end of statements.

3. Use the debugging features of the Editor/Debugger, which are

You might also like