MLP Unit-2 Functions in MATLAB
MLP Unit-2 Functions in MATLAB
Syntax
function [y1,...,yN] = myfun(x1,...,xM)
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
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:
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 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.
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.