The Islamic University of Gaza Faculty of Engineering...
Transcript of The Islamic University of Gaza Faculty of Engineering...
![Page 1: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/1.jpg)
The Islamic University of GazaFaculty of Engineering
Civil Engineering Department
Computer Programming (ECIV 2302)Chapter 6: User defined functions and function files
١
![Page 2: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/2.jpg)
٢
6.1 Creating a function file
Function File
Function files are created and edited like script files.
Input data output
![Page 3: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/3.jpg)
6.2 Structure of a function file
Function definition line
The H1 line
Help text
Function body
Assignment of values to output arguments
٣
![Page 4: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/4.jpg)
6.2.1 Function Definition Line
The first line in a function file must be the functiondefinition line.
• Defines the file as a function file• Defines the name of the function• Defines the number and order of the input and output
arguments.
function [output arguments] = function_name(input arguments)
٤
![Page 5: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/5.jpg)
6.2.2 Input and output arguments
In order for the function file to work, the output arguments must beassigned values in the computer program that is in the function body.
Function definition line Comments
function[mpay,tpay] = loan(amount,rate,years) 3 input arguments, 2 output arguments
function [A] = RectArea(a,b) 2 input arguments, 1 output argument
function A = RectArea(a,b) 1 input argument can be typed withoutbrackets.
function [V,S] = SphereVolArea(r) 1 input argument, 2 output variables.
function trajectory (v,h,g) 3 input arguments, no output arguments
٥
![Page 6: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/6.jpg)
6.2.3 The H1 line and Help text lines• They are optional, but used to provide information about
the function.
6.2.4 Function Body• The function body contains the code that performs the
computation.• The code can use all MATLAB features. ٦
![Page 7: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/7.jpg)
6.3 Local and Global Variables
If you want more than one function to share a singlecopy of a variable, simply declare the variable asglobal in all the functions. The global declaration mustoccur before the variable is actually used in a function.
Example:function h = falling(t)global GRAVITYh = 1/2*GRAVITY*t.^2;
٧
![Page 8: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/8.jpg)
6.4 Saving a function File• A function file must be saved before it can be used.• It is recommended that the file is saved with the name that
it is identical to the function name in the definition line.
Function definition line Function namefunction[mpay,tpay] = loan(amount,rate,years) loan.m
function [A] = RectArea(a,b) RectArea.m
function A = RectArea(a,b) RectArea.m
function [V,S] = SphereVolArea(r) SphereVol.m
function trajectory (v,h,g) Rajectory.m
٨
![Page 9: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/9.jpg)
6.5 Using a user defined function• Two of the ways that a function can be used:
• First input argument is loan amount• Second input argument is interest rate,• And third input argument is the number of years
٩
![Page 10: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/10.jpg)
Example 1
١٠
![Page 11: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/11.jpg)
١١
![Page 12: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/12.jpg)
Example 2
١٢
![Page 13: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/13.jpg)
6.7 Comparison between script files and function files• M files (both)
• The first line in a function file is the function definition line.
• The variables in a function file are local. The variables in a script fileare recognized in the command window.
• Script files can use variables that have been defined in theworkspace.
• Function files can accept data through input arguments and canreturn data through output arguments.
• When a function file is saved, the name of the file should be thesame as the name of the function.
١٣
![Page 14: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/14.jpg)
6.8 Anonymous and inline functions6.8.1 Anonymous function
Is a simple (one line) a user defined function that is defined andwritten within the computer code (not in a separate function file).
• It can be defined in any part of MATLAB in: Command window Script file Inside user defined functions.
Name= @ (arglist) expr
١٤
![Page 15: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/15.jpg)
Examples:
>> Cube =@(x) x^3;
>> Circle = @(x,y) 16*x^2+9*y^2;
>> a, b, and c are defined (assigned numerical values)>> parabola = @(x) a*x^2+b*x+cNote:
anonymous function can include preassigned variables(e.g a, b)
١٥
![Page 16: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/16.jpg)
Example (single input argument):
١٦
![Page 17: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/17.jpg)
Example (several independent arguments):
See Sample problem 6.3 P 168
١٧
![Page 18: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/18.jpg)
6.8.2 Inline Function
Note:inline function can not include preassigned variables.
Name= inline(‘ math expression typed as string’)
١٨
![Page 19: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/19.jpg)
Name= inline(‘ math expression typed as string’,’arg1’,’arg2’,’arg3’)
Note:• This format shows the order of the arguments to be used
when calling the function.• If the independent variables are not listed in the command,
MATLAB arranges the arguments in alphabetic order.
Inline Function has two or more independent variables:
١٩
![Page 20: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/20.jpg)
6.9 Function Functions• A (function A) works on uses another function (function B)
6.9.1 Using function handles for passing a function intoa function function
For built-in and user-defined functions:• A function handle is created by typing the symbol @ in
front of the function name. For example (@cos , @FtoC)• The function handle can be assigned to a variable name.
The next example shows writing a function function that acceptsa function handle as an input argument
٢٠
![Page 21: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/21.jpg)
1. Passing a user defined function into a function function
٢١
![Page 22: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/22.jpg)
٢٢
![Page 23: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/23.jpg)
2. Passing an anonymous function into a function function
٢٣
![Page 24: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/24.jpg)
6.9.2 Using a function Name for Passing a Function into aFunction
• By typing the name of the function that is being imported as astring in the input argument of the function.
• This method for user defined functions.
• Function handles are easier and more efficient.
• When a user defined function is imported by using its name,the value of the imported function inside the function function hasto be calculated with the feval command.
• There is a difference in the way that the code in the functionfunction is written that depends on how the imported function ispassed in.
٢٤
![Page 25: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/25.jpg)
Examples on using the feval command
٢٥
![Page 26: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/26.jpg)
1. Writing a function function that accepts a function by typing itsname as an input function
٢٦
![Page 27: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/27.jpg)
0.5 1 1.5 2 2.5 3 3.5 4-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
0.5
1
x
y
٢٧
![Page 28: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/28.jpg)
6.10 Subfunctions
• A function file contain more than one user defined function.
• The functions are typed one after the other.
• The first function is called the main function and the rest of thefunctions are called subfunctions.
• The subfunctions can be typed in any order.
• The name of the function file correspond the name of themain function.
• Inside the function file, each of the functions can call any ofthe other functions.
٢٨
![Page 29: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/29.jpg)
Example: Average and standard deviation
٢٩
![Page 30: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/30.jpg)
6.11 Nested functions• A nested function is a user-defined function that is written inside
another user defined function.
• An end statement is required at the end of all functions.
• The variables defined in any function can be read and redefined inthe rest functions and vise versa.
• The functions can call each other.
• One Nested function
function y = A(a1,a2)………..
function z = B(b1,b2)…………end
………….end
٣٠
![Page 31: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/31.jpg)
• Two or more Nested function
function y = A(a1,a2)………..
function z = B(b1,b2)…………end
function w = C(c1,c2)…………end
………….end
٣١
• The three functions canaccess the workspace of eachother.
• The three functions can calleach other.
![Page 32: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/32.jpg)
Example: Nested function
٣٢
![Page 33: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/33.jpg)
Two levels of nested function
function y = A(a1,a2)………..
function z = B(b1,b2)…………
function w = C(c1,c2)…………end
……..end
function u = D(d1,d2)…………
function h = E(e1,e2)…………end
……..end
………….end
• A nested function can becalled from a level above it.
• A nested function can becalled from a nested function atthe same level within a primaryfunction.
• A nested function can becalled from a nested function atany lower level.
٣٣
![Page 34: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/34.jpg)
Problem 6.5
٣٤
![Page 35: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/35.jpg)
Problem 6.10
٣٥
![Page 36: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/36.jpg)
Problem 6.21
٣٦
![Page 37: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/37.jpg)
Example
Given:• An object thrown vertically with a speed v0 reaches a height
h at time t where:
Required• Write a function to compute h given v0 and t. It should allow
t to be a vector.• Use the function to PLOT h versus t for t between 0 and 10s
and v0 = 50 m/s.
20
1
2h v t gt
![Page 38: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/38.jpg)
![Page 39: The Islamic University of Gaza Faculty of Engineering ...site.iugaza.edu.ps/esilmi/files/Matlab-Chapter6.pdf• This method for user defined functions. • Function handles are easier](https://reader034.fdocuments.in/reader034/viewer/2022042311/5ed97117eb09c069957f624f/html5/thumbnails/39.jpg)