The Islamic University of Gaza Faculty of Engineering...

Post on 31-May-2020

5 views 0 download

Transcript of The Islamic University of Gaza Faculty of Engineering...

The Islamic University of GazaFaculty of Engineering

Civil Engineering Department

Computer Programming (ECIV 2302)Chapter 6: User defined functions and function files

١

٢

6.1 Creating a function file

Function File

Function files are created and edited like script files.

Input data output

6.2 Structure of a function file

Function definition line

The H1 line

Help text

Function body

Assignment of values to output arguments

٣

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)

٤

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

٥

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. ٦

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;

٧

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

٨

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

٩

Example 1

١٠

١١

Example 2

١٢

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.

١٣

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

١٤

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)

١٥

Example (single input argument):

١٦

Example (several independent arguments):

See Sample problem 6.3 P 168

١٧

6.8.2 Inline Function

Note:inline function can not include preassigned variables.

Name= inline(‘ math expression typed as string’)

١٨

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:

١٩

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

٢٠

1. Passing a user defined function into a function function

٢١

٢٢

2. Passing an anonymous function into a function function

٢٣

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.

٢٤

Examples on using the feval command

٢٥

1. Writing a function function that accepts a function by typing itsname as an input function

٢٦

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

٢٧

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.

٢٨

Example: Average and standard deviation

٢٩

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

٣٠

• 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.

Example: Nested function

٣٢

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.

٣٣

Problem 6.5

٣٤

Problem 6.10

٣٥

Problem 6.21

٣٦

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