w6 7 functions and functions file
TRANSCRIPT
-
8/2/2019 W6 7 Functions and Functions File
1/45
KNJ2332Engineering Programming
Week 6 & 7
Functions and Functions File
-
8/2/2019 W6 7 Functions and Functions File
2/45
Outline
Elementary Mathematical Functions
User Defined FunctionsWorking with Data Files
-
8/2/2019 W6 7 Functions and Functions File
3/45
Elementary MathematicalFunctions
Exponentialexp(x)
sqrt(x)
Logarithmiclog(x)
log10(x)
Exponential; e x
Square root; x
Natural logarithm; ln x
Common (base 10) logarithm; log x = log10 x
-
8/2/2019 W6 7 Functions and Functions File
4/45
Elementary MathematicalFunctions
Complex Numberabs(x)
angle(x)conj(x)imag(x)real(x)
Absolute value.
Angle of a complex number.Complex conjugate.Imaginary part of a complex number.Real part of a complex number.
-
8/2/2019 W6 7 Functions and Functions File
5/45
Elementary MathematicalFunctions
Example : is a complex number .
Can be entered in MATLAB in different ways:
i35
-
8/2/2019 W6 7 Functions and Functions File
6/45
Elementary MathematicalFunctions
Numeric Functions
ceil(x)fix(x)
floor(x)round(x)sign(x)
Round to nearest integer toward . Round to nearest integer toward zero.
Round to nearest integer toward - . Round toward nearest integer.Signum function:
+1 if x > 0; 0 if x = 0; -1 if x < 0.
-
8/2/2019 W6 7 Functions and Functions File
7/45
Elementary MathematicalFunctions
Example:
-
8/2/2019 W6 7 Functions and Functions File
8/45
Elementary MathematicalFunctions
Trigonometric functions:
cos(x)
cot(x)
csc(x)
sec(x)
sin(x)
tan(x)
cosine; cos x .
cotangent; cot x .
cosecant; csc x .
secant; sec x .
sine; sin x .
tangent; tan x .
-
8/2/2019 W6 7 Functions and Functions File
9/45
Elementary MathematicalFunctions
acos(x)acot(x)acsc(x)asec(x)asin(x)
atan(x)atan2(y,x)
Inverse cosine; arccos x .
Inverse cotangent; arccot x .Inverse cosecant; arccsc x .
Inverse secant; arcsec x .Inverse sine; arcsin x .
Inverse tangent; arctan x .Four-quadrant inverse tangent.
Inverse Trigonometric Functions:
-
8/2/2019 W6 7 Functions and Functions File
10/45
Elementary MathematicalFunctions
Hyperbolic Functions:
Hyperbolic cosine
Hyperbolic cotangent.
Hyperbolic cosecant
Hyperbolic secant
Hyperbolic sine
Hyperbolic tangent
cosh(x)
coth(x)
csch(x)
sech(x)
sinh(x)
tanh(x)
-
8/2/2019 W6 7 Functions and Functions File
11/45
Elementary MathematicalFunctions
Inverse Hyperbolic Functions:
acosh(x)
acoth(x)
acsch(x)
asech(x)
asinh(x)
atanh(x)
Inverse hyperbolic cosine
Inverse hyperbolic cotangent
Inverse hyperbolic cosecant
Inverse hyperbolic secant
Inverse hyperbolic sine
Inverse hyperbolic tangent;
-
8/2/2019 W6 7 Functions and Functions File
12/45
Elementary MathematicalFunctions
Example:For x in the range 0 x 2 , confirm that
tan(2x) = 2 tan x/(1 - tan 2x)
-
8/2/2019 W6 7 Functions and Functions File
13/45
User-Defined Functions
Function file is another type of M-file.
Function files are useful when you need torepeat a set of commands several times.
Function files are commonly the building blocksof larger programs.
-
8/2/2019 W6 7 Functions and Functions File
14/45
User-Defined Functions
The first line in a function file must begin with a function definitionline that has a list of inputs and outputs. This line distinguishes afunction M-file from a script M-file. Its syntax is as follows:
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 should be the same as the file name in which itis saved (with the .m extension).
-
8/2/2019 W6 7 Functions and Functions File
15/45
User-Defined Functions
Rules of naming user-defined functions are similar to namingvariables:
The function name must start with a letter.It can consists of letters, numbers and underscore.Reserved names cannot be used.Any length is allowed although long names are not goodprogramming practice.
-
8/2/2019 W6 7 Functions and Functions File
16/45
User-Defined Functions
Example 1:
-
8/2/2019 W6 7 Functions and Functions File
17/45
User-Defined Functions
Output from Example 1:
-
8/2/2019 W6 7 Functions and Functions File
18/45
User-Defined Functions
Example 2:Create a function that computes the area and circumference of acircle, given its radius as input.
-
8/2/2019 W6 7 Functions and Functions File
19/45
User-Defined FunctionsOutput from Example 2:
-
8/2/2019 W6 7 Functions and Functions File
20/45
User-Defined Functions
Example 3:Create a function that converts degrees to radians:
-
8/2/2019 W6 7 Functions and Functions File
21/45
User-Defined FunctionsOutput from Example 3:
-
8/2/2019 W6 7 Functions and Functions File
22/45
User-Defined FunctionsOutput from Example 3:
-
8/2/2019 W6 7 Functions and Functions File
23/45
User-Defined Functions
A function may have no input arguments and nooutput list.
For example:
-
8/2/2019 W6 7 Functions and Functions File
24/45
-
8/2/2019 W6 7 Functions and Functions File
25/45
User-Defined Functions
Local Variables
The names of the input variables given in the function
definition line are local to that function.
This means that other variable names can be used when youcall the function.
All variables inside a function are erased after the functionfinishes executing, except when the same variable namesappear in the output variable list used in the function call.
-
8/2/2019 W6 7 Functions and Functions File
26/45
User-Defined Functions
Global Variables
The global command declares certain variables global, andtherefore their values are available to the basic workspace andto other functions that declare these variables global.
The syntax to declare the variables a , x , and q isglobal a x q
-
8/2/2019 W6 7 Functions and Functions File
27/45
User-Defined Functions
Global Variables
In general, it is not recommended to define globalvariables.
Because, it becomes available to other functions and can bechanged by those functions, unintentionally.
-
8/2/2019 W6 7 Functions and Functions File
28/45
User-Defined Functions
Global Variables
Example of function with global variable:
-
8/2/2019 W6 7 Functions and Functions File
29/45
User-Defined Functions
Some Applications of Functions
Finding the Zeros of a FunctionMinimizing a Function of One VariableMinimizing a Function of Several
VariablesDesign Optimization
-
8/2/2019 W6 7 Functions and Functions File
30/45
User-Defined Functions
Finding the Zeros of a Function
Recall that we use the built-in functionroots to find the zeros of polynomialfunctions: 065213 23 x x x
-
8/2/2019 W6 7 Functions and Functions File
31/45
User-Defined Functions
Finding the Zeros of a Function
For any function of a single variable, we canuse fzero function to find the zero.
The syntax is fzero (function, x0) where function is the name of the function
and x0 is a user-supplied guess for the zeroIt returns a value of x that is near x0 .
-
8/2/2019 W6 7 Functions and Functions File
32/45
User-Defined Functions
Example: For function y = x +2e -x -3
-
8/2/2019 W6 7 Functions and Functions File
33/45
User-Defined Functions
Minimizing a Function of One Variable
Function fminbnd finds the minimum of afunction of a single variable at certain xinterval.
Common syntax is fminbnd (function,x1, x2)where x1 and x2 is the lower and upperlimit of the interval.
-
8/2/2019 W6 7 Functions and Functions File
34/45
User-Defined Functions
Example:For function y = 1 xe -x, find the value of x thatgives a minimum of y for 0 x 5.
-
8/2/2019 W6 7 Functions and Functions File
35/45
User-Defined Functions
Minimizing a Function of Several Variables
The fminsearch function is used to find theminimum of a function of more than onevariable.
Common syntax isfminsearch (function, x0) where x0 is the vector that a user input basedon a guess.
-
8/2/2019 W6 7 Functions and Functions File
36/45
User-Defined Functions
Example:For function , find the value of x andy that gives a minimum of f.
22 y x xe f
First define it in an M-file, using the vector x whose elements are x(1)=x and x(2)= y .
function f = f4(x)f = x(1).*exp(-x(1).^2-x(2).^2);
Suppose we guess that the minimum is near x = y = 0 :>>fminsearch (f4,[0,0]) ans =
-0.7071 0.000
Thus the minimum occurs at x = 0.7071, y = 0.
-
8/2/2019 W6 7 Functions and Functions File
37/45
User-Defined Functions
Design Optimization
Design optimization is an approach that findways to improve engineering designs byformulating the mathematical equationsdescribing the minimization/ maximizationof the problem.
Examples, minimise energy consumption ordesign cost; maximize efficiency or productcapacity.
-
8/2/2019 W6 7 Functions and Functions File
38/45
User-Defined Functions
Design Optimization
Example:A fenced enclosure has the followingmeasurement. An area A of 1600 ft 2 isrequired, and fencing cost is RM40/footfor the curved part and RM30/foot onthe straight sides. Determine the value of
R and L to minimize the total cost of thefence.
A = 1600 ft 2
-
8/2/2019 W6 7 Functions and Functions File
39/45
Design Optimization
A = 1600 ft 2
-
8/2/2019 W6 7 Functions and Functions File
40/45
Working with Data Files
Importing Spreadsheet Files
Some spreadsheet programs store data in the .wk1 format. You can use the commandM = wk1read(filename) to import this datainto MATLAB and store it in the matrix M.
The command A = xlsread (filename)
imports the Microsoft Excel workbook filefilename.xls into the array A. The command [A,B] = xlsread (filename) imports all numericdata into the array A and all text data into the cell array
B.
-
8/2/2019 W6 7 Functions and Functions File
41/45
The Import Wizard
To import ASCII data, you must know how the data in the fileis formatted.
For example, many ASCII data files use a fixed (or uniform)format of rows and columns.
(continued )
-
8/2/2019 W6 7 Functions and Functions File
42/45
The Import Wizard (continued)
For these files, you should know the following.
How many data items are in each row?
Are the data items numeric, text strings, or a mixture of
both types?
Does each row or column have a descriptive textheader?
What character is used as the delimiter, that is, thecharacter used to separate the data items in each row?The delimiter is also called the column separator .
(continued )
-
8/2/2019 W6 7 Functions and Functions File
43/45
The Import Wizard (continued)
You can use the Import Wizard to import many types of ASCII data formats, including data on the clipboard. Whenyou use the Import Wizard to create a variable in theMATLAB workspace, it overwrites any existing variable in theworkspace with the same name without issuing a warning.
The Import Wizard presents a series of dialog boxes in whichyou:
1. Specify the name of the file you want to import,2. Specify the delimiter used in the file, and3. Select the variables that you want to import.
-
8/2/2019 W6 7 Functions and Functions File
44/45
The Import Wizard (continued)
-
8/2/2019 W6 7 Functions and Functions File
45/45
The End