Lecture 5 User Defined Functions
Lecture 5 User Defined Functions
(Chapter 5—Riggs)
Upcoming assignments:
Assignment 3 (Matrices and arrays): due today (01/28/25), 11:59 PM
Assignment 4 (Functions): due January 31, 11:59 PM
Outputs
Use an m-file function (multiple input / multiple outputs and not limited to a single
expression) to break code into easy to develop subdivisions.
Similar to script files, but all variable are local variables (variables within the function are
erased after execution).
Basic Syntax:
The first executable line of the function file must begin with a function definition line (it can
start with % comments).
end
Notes:
• All variable assignments in the function file are local.
• The function file should be in the same working directory as the main code (can explicitly
change directory but need to tell MATLAB).
User-Defined Functions
(Some Examples)
Example 1:
Compute the area of a circle where the radius R is an input value prescribed by the user.
[Area_Circ] = circle(R) Could replace Area_Circ with any other name (local variables)
Create a new script for the function file (function_circle.m), consisting of:
Example 1:
Compute the area of a circle where the radius R is an input value prescribed by the user.
[Area_Circ] = circle(R) Could replace Area_Circ with any other name (local variables)
Create a new script for the function file (circle.m), consisting of:
Once created, call the function in a script (create a new m-file, Example_2.m)
to compute the following:
a. f(x) when x = 3
b. f(x) when x = [1, 2, 3, 4]
In-Class Exercises
Example 3. Create a function “function_max_number” that given three numbers as input returns the
maximum number of the three input variables. The input numbers are: 250, 10 and -15.
Example 4. Create a function “function_operations” that given a vector as input returns: 1) the
minimum value, 2) the sum of all elements in the vector, and 3) the norm of the vector.
Solve this exercise for v1 = [1, 10, -2, 5] and v2 = [17, -8, 4, 7, 18].
Anonymous Functions
(Simple Functions)
An Anonymous Function:
• Multiple input/single output function in a single expression.
• Enable you to create a simple function without having to create, name, and save an m-file
function.
Syntax:
func_name = @ (arglist) expr
Example 5:
xsquared = @ (x) x^2;
Thus, to execute in MATLAB code, you only need to specify:
xsquared (5)
Note: Anonymous functions can have multiple
ans =
inputs but only one output (scalar, vector, or matrix).
25
Example 6:
Cyl_Volume = @ (Radius, Height) pi*Radius^2*Height;
Thus, in MATLAB code:
Cyl_Volume (0.1, 1)
xsquared and Cyl_Volume are the function
ans =
handles/names.
0.0314
Anonymous Functions
(Some Examples)
Example 7:
vec_squared = @ (x) x.^2;
Thus, in MATLAB code:
x= [0 2 4 6 8 10]
y = vec_squared (x)
y=
0 4 16 36 64 100
Example 8:
FA = @ (x) exp(x.^2)./sqrt(x.^2 + 5);
Thus, in MATLAB code:
FA ([1 0 .5 2])
FA =
1.1097 0.5604 18.1994
More Exercises
Example 9.
a) Using MATLAB built-in functions, create a 10x10 matrix where all elements = 1 and a 10x10
matrix where the diagonal elements = 3 and off-diagonal elements = 0. Do not manually
create the matrices.
c) Extract a submatrix from the result that contains rows 5-10 and columns 5-10.
Functions as Arguments to Functions
(Passing Functions to Functions)
The function handle/name (subfunction) can be passed as an input argument to the primary
function:
primary_function_name (@subfunction_name, …. )
Functions as Arguments, Cont’d.
(An Example)
Example: