0% found this document useful (0 votes)
30 views5 pages

Lab No. 4 Me

Uploaded by

Zainab Ashraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Lab No. 4 Me

Uploaded by

Zainab Ashraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB EXPERIMENT 4

FUNCTIONS AND FUNCTION FILES


PURPOSE
To learn
1. how to structure, define and call a function file
2. how to access the global and local variables within function file
3. how to save data values from function file
4. how to send and receive data values from function file
EQUIPMENT
Personal computer (PC) with windows operating system and MATLAB software
THEORY
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). Frequently, in computer programs, there is a need to calculate the
value of functions that are not built-in. When a function expression is simple and needs to be
calculated only once, it can be typed as part of the program. However, when a function needs to
be evaluated many times for different values of arguments it is convenient to create a ''user
defined" function. Once the new function is created (saved) it can be used just like the built-in
functions.
1) Creating a Function File:-
Function files are created and edited, like script files, in the Editor/Debugger Window. This
window is opened from the Command Window. In the File menu, select New, and then select
M-fIle.
2) Structure of a Function File:-
The structure of a typical function file is shown in Figure below.
2.1) Function Definition Line:-
The function definition 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.
Input and Output Arguments:-
Data is passed into and out of the function using the input and output arguments. The
function name is followed by a list of the input arguments enclosed in parentheses.These
function definition lines with various combinations of input and output arguments are
examples.
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 output argument.
function A = RectArea( a, b)
Same as above, one output argument can be typed
without the brackets.
2.2)The H1 Line and Help Text Lines
The H1 line and help text lines are comment lines (lines that begin with the percent% sign)
following the function definition line
2.3) Function Body
The function body contains the computer program (code) that actually performs the
computations.
3) Inline Functions
Function files can be used for simple mathematical functions, for large and complicated math
functions that require extensive programming, and as subprograms in large computer programs.
Inline functions can be defined in any part of MATLAB.
Inline functions are created with the inline command according to the following format:
name = inline('math expression typed as a string')
PROCEDURE
Execute the following example in MATLAB
1) The function:-
Example:-

Write a function file for the function the input to the function is x
and the output is f(x). Write the function such that x can be a vector. Use the function to
calculate:
a) f(x) for x =6.
b) f(x) for x = 1,3,5,7,9, and 11.
Open the Editor/Debugger Window. This window is opened from the Command Window. In
the File menu, select New, and then select M-fIle. Once the Editor/Debugger Window opens
write the following function in it

Function definition function


[y] = exp4one(x)

y= (x.^4.*sqrt(3*x+5))./(x.^2+1).^2;
Assignment to output
a) Calculating the function for x = 6 can be done by
typing exp4one(6) in the Command window
>> exp4one(6)
ans = 4.5401
To calculate the function for several values of x, a vector with the values of x is first created,
and then used for the argument of the function.
>> x = 1:2:11
x=
1 3 5 7 9 11
>> exp4one(x) ans =
0.7071 3.0307 4.1347 4.8971 5.5197 6.0638
The inline Function

The function can be defined (in the Command Window) as an inline


function for x as a scalar by:
>> FA = inline('exp(x^2)/sqrt(x^2+5)')
FA =
Inline function:
FA(x) = exp(x^2)/sqrt(x^2+5)
Then the value of f(x) at different value of x can be calculated as
>> FA(2) ans =
18.1994 >>
FA(3) ans =
2.1656e+003
If there are two variables then the f(x, y) = 2x2- 4xy+y2 can be defined as an inline function by:
>> HA = inline('2*x^2-4*x*y+y^2')
HA =
Inline function:
HA(x,y) = 2*x^2-4*x*y+y^2
MATLAB arranges the arguments in alphabetical order. The function can be used for different
values of x and y. For example,
HA(2,3)gives:
>> HA(2,3) ans
=
-7
EXERCISES
Converting temperature units
Write a user-defined function (name it FtoC) that converts temperature in degrees F to
temperature in degrees C. Use the function to convert
a) 32 degrees F to degrees C.

32, 35,40,60,80 degrees F to degrees C.


A) 32 degrees F to degrees C.
>> FtoC(32)
ans = 0

B) 35 degrees F to degrees C
>> FtoC(35)
ans =1.6667
C) 40 degrees F to degrees C
>> FtoC(40)
ans =4.4444

D) 60 degrees F to degrees C
>> FtoC(60)
ans =15.5556

E) 80 degrees F to degrees C
>> FtoC(80)
ans =26.6667

Analysis:
The formula to convert Fahrenheit to Celsius is To convert temperatures in degrees
Fahrenheit to Celsius, subtract 32 and multiply by .5556 (or 5/9).So, in this we apply above formula by
using function F to C which means Fahrenheit to Celsius and write the given value in brackets and the
answer is obtained.

CONCLUSION
In this experiment, we define and uses of function, create a function file in Matlab and to save values in
the function file. Inline functions which are those functions that can be defined in one line and they are
also called one liner as well.
To inline a function, place the keyword inline before the function name and define the function before
any calls are made to the function.

You might also like