Uploaded by omar saud

MATLAB User Defined Functions Presentation

advertisement
User Defined Functions
EE 201
C5-1
2020
1
Session Agenda
 Contact before work
5 min.
 User Defined Functions
70 min.
C5-1
2020
2
Class Learning Objectives
 Achieve Comprehension LOL of User
Defined Functions.
C5-1
2020
3
User can create the function
x=5; y= 𝑥 2
Normal Procedure;
y=x^2; % default MATLAB operation
y=25
Function created by user for same
application
function [y]=square1(x)
y=x*x;
end
y=square1(x)
y=25
% by User defined function
C5-1
2020
4
User Defined Functions
-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. It’s syntax is as
follows:
function [output variables]= name(input
variables)
C5-1
2020
5
 function [output variables]= name(input
variables)
 Note that the output variables are enclosed in square brackets [ ],
while the input variables must be enclosed with parentheses ( ).
The function name ( name) should be the same as the file name in
which it is saved (with the .m extension).
C5-1
2020
6
Writing User Defined Functions
You should write this command at the beginning of the m-file and
you should save the m-file with a file name same as the function
name
C5-1
2020
7
Example:
C5-1
2020
8
 Note :Brackets [ ] are optional for one
output
C5-1
2020
9
Example
Only the order of the arguments
is important , not the names
of the arguments:
C5-1
2020
10
 You can use arrays as input
arguments:
C5-1
2020
11
Examples of Function Definition Lines
1. One input, one output:
function [area_square] = square(side)
2. Brackets are optional for one input, one
output:
function area_square = square(side)
3. Three inputs, one output:
function [volume_box] =box(height,width,length)
C5-1
2020
12
Examples of Function Definition Lines
4. One Input, two outputs:
function [area_circle,circumf]=circle(radius)
5. No Input, No output:
C5-1
2020
13
Example
 Write a MATLAB program that
computes area and circumference of
a circle
C5-1
2020
14
 Area =π (radius)2
 Circumference =2π radius
C5-1
2020
15
Solution
C5-1
2020
16
Example
a) ِCreate a MATLAB user defined function
that computes area and circumference of
a circle.
b) Write a MATLAB program that:
-prompt the user to enter the radius in
meter .
- it computes area and circumference using
the function you defined in part (a)
17
C5-1
2020
C5-1
2020
18
Local variables
C5-1
2020
19
 The variables x and y are local to the
function S, so unless you pass their
values by naming them x and y, their
values will not be available in the
workspace outside the function. The
variable u is also local to the function.
For example:
C5-1
2020
20
C5-1
2020
21
Example:
function v=L(a)
N
b=2;
v=a+b ;
end
>> a=20;
>>b=5;
v=L(a)
v=
22
N
>>a
a=
20
>>b
b=
5
C5-1
2020
22
Example:
function v=L(a)
N
b=2;
v=a+b ;
end
>> x=20;
>>b=5;
z=L(x)
z=
22
>>x
x=
N
20
>>b
b=
5
>> a
??? Undefined function or variable
'a'.
C5-1
2020
23
Quiz
a) ِCreate a MATLAB user defined function that
computes the surface area and Volume of a
Box.
b) Write a MATLAB program that:
-prompt the user to enter the 3 sides of the
box .
- computes the surface area and Volume of
the Box using the function you defined in
part (a)
- Display the results
24
C5-1
2020
Quiz
function [V,SA]=V_S(L,W,H)
V=L*W*H;
SA=L*W*2+L*H*2+H*W*2;
end
L=input(‘Please enter the Length’);
W=input(‘Please enter the Width’);
H=input(‘Please enter the Height’);
[V,SA]=V_S(L,W,H);
disp([‘The Volume is’, num2str(V)])
disp([‘The Surface Area is’, num2str(SA)])
C5-1
2020
25
Download