Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

User-Defined Functions

 The function can be expressed in the form y = f(x) , where f(x) is usually a
mathematical expression in terms of x.
 A value of y (output) is obtained when a value of x (input) is substituted in the
expression.
 Many functions are programmed inside MATLAB as built-in functions, and can
be used in mathematical expressions simply by typing their name with an
argument examples are sin(x), cos(x), sqrt(x), and exp(x).
 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.
Function Definition Line
 The first line in a function file must begin with a function definition line that has a list of inputs
and outputs. This line distinguishes a function M-file from a script M-file. Its syntax is as
follows:

 Note that the output variables are enclosed in square brackets, while the input variables must be
enclosed with parentheses. The function name (here, name) should be the same as the file name
in which it is saved (with the .m extension).
Function definition line Comments
function [mpay,tpay] = loan(amount,rate,years) Three input arguments,
two
output arguments.
function [A] = RectArea(a,b) Two input arguments, one

out- put argument.


function A = RectArea(a,b) Same as above; one

output argument can be


typed without the brackets.
function [V, S] = SphereVolArea(r) One input variable, two

output variables.
function trajectory(v,h,g) Three input arguments, no

output arguments.
How to construct and use an external function

First, create the Matlab program for an external function as a separate file. The filename has to be

identical to the name of the user-defined function


myfunc1.m
function outcome = myfunc1(x)
outcome = 2*x^2 + 3*x + 7;

This program defines a function, f(x) ≡ 2 x2 + 3 x + 7. The "outcome" in the program is a dummy
variable that is used to indicate the relation between the input parameter ("x") and the outcome after the
evaluation of the function. After an external function is defined, it can be used in a main program that
calls it. For example: mainprog1.m

for n = 1:5 Output:


x = n*0.1; x = 0.10 f(x) = 7.3200
x = 0.20 f(x) = 7.6800
z = myfunc1(x); x = 0.30 f(x) = 8.0800
fprintf('x = %4.2ff(x) = %8.4f \r',x,z) x = 0.40 f(x) = 8.5200
end x = 0.50 f(x) = 9.0000
 A function with multiple input parameters
myfunc2.m

function outcome = myfunc2(x,a,b,c)


outcome = a*x^2 + b*x + c;

This program defines a function, f(x) ≡ a x2 + b x + c.


mainprog2.m
for n = 1:5 Output:
x = n*0.1;
z = myfunc2(x,2,3,7); x = 0.10 f(x) = 7.3200
x = 0.20 f(x) = 7.6800
x = 0.30 f(x) = 8.0800
fprintf('x = %4.2ff(x) = %8.4f \r',x,z); x = 0.40 f(x) = 8.5200
x = 0.50 f(x) = 9.0000
end
Anonymous Functions
 An anonymous function is a simple (one-line) user-defined function that is defined without creating a
separate function file (M-file).
 Anonymous functions can be constructed in the Command Window, within a script file, or inside a
regular user-defined function.

A simple example is: cube = @ (x) x^3, which calculates the cube of the input argument.
Example of an anonymous function with one independent variable:
If x is expected to be an array, with the function calculated for each element,
then the function must be modified for element-by-element calculations.
Example of an anonymous function with several independent variables:

 The function f(x, y) = 2x2 – 4xy + y2 can be defined as an anonymous function


by:

 Then the anonymous function can be used for different values of x and y. For
example, typing HA(2,3) gives:
1) Write the following functions in the form of an anonymous function.

a) f(x)= Use the function to calculate for x=6 and x=[ 2 4 7 9]

b) f(x,y) = 2x2-4xy+y2 Use the function to calculate for x=3 and y=4

2) Write a matlab program to create a user defined function for factorial of


number use function name as fact(),Do not use matlab built in function.
(use for / while )

You might also like