0% found this document useful (0 votes)
14 views

User Functions

This document discusses user-defined functions in MATLAB. It explains that user-defined functions allow for custom code to perform specific tasks and improve code reuse, organization, and performance. User-defined functions are defined using the function keyword and consist of a function statement, variable declarations, and function body. Functions can take arguments, have multiple statements and nested functions, and be called and nested within other functions. Examples are provided to demonstrate calculating math operations, solving equations, and nesting functions to find sums and averages.

Uploaded by

8ffnf5qmj8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

User Functions

This document discusses user-defined functions in MATLAB. It explains that user-defined functions allow for custom code to perform specific tasks and improve code reuse, organization, and performance. User-defined functions are defined using the function keyword and consist of a function statement, variable declarations, and function body. Functions can take arguments, have multiple statements and nested functions, and be called and nested within other functions. Examples are provided to demonstrate calculating math operations, solving equations, and nesting functions to find sums and averages.

Uploaded by

8ffnf5qmj8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

7

Lab
Matlab Language
User Functions in MATLAB
ALI ABDULRAZZAQ ABDALI
User Functions in MATLAB
● User-defined functions are custom-written code that can be used to perform a
specific task.
● They can be used to improve code reuse, organization, and performance.
● User-defined functions are defined using the function keyword.
● They consist of a function statement, variable declarations, and a function body.
● User-defined functions are called using the call keyword.
● It should be noted that when storing a file (M-file) that contains a function, its name
must be similar to the name of the function itself
Example 1
Write a program to add two numbers using When executed, it is written in the
matlab in function defined User command window

1- open the file (mfile) Call the function and it's like this

s = sums(4, 6);

function s = sums(num1, num2)

s = num1 + num2; Output:

end s = 10
Notes :
User-defined functions can have any number of arguments.

User-defined functions can have any number of statements.

User-defined functions can have any number of nested functions.

User-defined functions can be nested within other user-defined functions.

A function file that was recently stored can be called in any other program
How to find the area and circumference of a circle
In this example,use a user-defined function function [A,C] = area_circum (R)
to find the area and circumference of a A = pi * R^2;
circle. The function takes one argument, R, C = 2 * pi * R;
which is the radius of the circle. The
end
function body uses the following equations
to calculate the area and circumference: [A,C] = area_circum(4);

Area = pi * R^2 Output:


Circumference = 2 * pi * R A=
50.2655
The function returns two variables, A and C,
C=
which are the area and circumference of the
circle, respectively. 25.1327
Use function to solve the quadratic equation
In this example, use a user-defined function The function file is as follows:
to solve the quadratic equation. The
function takes three arguments, A, B, and C, function Z = find_z(A,B,C)
which are the coefficients of the quadratic Z = (-B + sqrt(B^2 - 4*A*C)) / (2*A);
equation. The function body uses the end
following formula to solve the quadratic
equation: The second file is as follows:
z = (-b ± sqrt(b^2 - 4ac)) / (2a) A = input('enter a:');
B = input('enter b:');
The function returns one variable, z, which
C = input('enter c:');
is the solution to the quadratic equation.
Z = find_z(A, B, C);
disp(['z = ' num2str(Z)]);
Example to use function nesting
● In this example, we will write two function s = summ(x,y,z)
functions to find the sum and average s = x + y + z;
of three numbers. end

● The first function, summ(), takes three


function av = av_sum()
arguments, x, y, and z, and returns the
s = summ(10, 20, 30);
sum of the three numbers.
av = s / 3;
● The second function, av_sum(), calls end
the summ() function to find the sum of
the three numbers, and then divides Output:
the sum by 3 to find the average. av = 20
Notes :
In this example, we will write a user-defined function to read three grades and then
calculate the sum and average of the grades.

The function takes three arguments, x, y, and z, which are the grades.

The function body uses the following code to calculate the sum and average of the grades:

s = x + y + z;

av = s / 3;

The function returns two variables, s and av, which are the sum and average of the grades,
respectively.
Example
In this example, we will use two files, one for The second file is as follows:
the function and one for reading the grades
and calling the function.
x = input('input x ');
y = input('input y');
The function file is as follows:
z = input('input z');
function [s,av] = avg_sum(x, y, z)
[s, av] = avg_sum(x, y, z);
s = x + y + z;
disp(['s = ' num2str(s)]);
av = s / 3; disp(['av = ' num2str(av)]);
end
Thank you for attention

You might also like