W6 7 Functions and Functions File
W6 7 Functions and Functions File
Outline
Elementary Mathematical Functions User Defined Functions Working with Data Files
Logarithmic log(x)
log10(x)
cot(x)
csc(x) sec(x) sin(x) tan(x)
cotangent; cot x.
cosecant; csc x. secant; sec x. sine; sin x. tangent; tan x.
coth(x)
csch(x) sech(x) sinh(x) tanh(x)
Hyperbolic secant
Hyperbolic sine Hyperbolic tangent
asech(x)
asinh(x) atanh(x)
User-Defined Functions
Function file is another type of M-file.
Function files are useful when you need to repeat a set of commands several times.
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. 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 it is saved (with the .m extension).
User-Defined Functions
Rules of naming user-defined functions are similar to naming variables: 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 good programming practice.
User-Defined Functions
Example 1:
User-Defined Functions
Output from Example 1:
User-Defined Functions
Example 2: Create a function that computes the area and circumference of a circle, given its radius as input.
User-Defined Functions
User-Defined Functions
Example 3: Create a function that converts degrees to radians:
User-Defined Functions
User-Defined Functions
User-Defined Functions
A function may have no input arguments and no output list. For example:
5. No named output:
function sqplot(side)
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 you call the function. All variables inside a function are erased after the function finishes executing, except when the same variable names appear in the output variable list used in the function call.
User-Defined Functions
Global Variables The global command declares certain variables global, and therefore their values are available to the basic workspace and to other functions that declare these variables global. The syntax to declare the variables a, x, and q is global a x q
User-Defined Functions
Global Variables In general, it is not recommended to define global variables. Because, it becomes available to other functions and can be changed by those functions, unintentionally.
User-Defined Functions
Global Variables Example of function with global variable:
User-Defined Functions
Some Applications of Functions Finding the Zeros of a Function Minimizing a Function of One Variable Minimizing a Function of Several Variables Design Optimization
User-Defined Functions
Finding the Zeros of a Function
Recall that we use the built-in function roots to find the zeros of polynomial functions: x3 13x 2 52 x 6 0
User-Defined Functions
Finding the Zeros of a Function
For any function of a single variable, we can use 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 zero
User-Defined Functions
Example: For function y = x +2e-x -3
User-Defined Functions
Minimizing a Function of One Variable
Function fminbnd finds the minimum of a function of a single variable at certain x interval.
Common syntax is fminbnd(function, x1, x2) where x1 and x2 is the lower and upper limit of the interval.
User-Defined Functions
Example: For function y = 1 xe-x, find the value of x that gives a minimum of y for 0 x 5.
User-Defined Functions
Minimizing a Function of Several Variables
The fminsearch function is used to find the minimum of a function of more than one variable.
Common syntax is fminsearch(function, x0) where x0 is the vector that a user input based on a guess.
User-Defined Functions
Example: For function f xe x y , find the value of x and y that gives a minimum of f.
2 2
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.
User-Defined Functions
Design Optimization
Design optimization is an approach that find ways to improve engineering designs by formulating the mathematical equations describing the minimization/ maximization of the problem. Examples, minimise energy consumption or design cost; maximize efficiency or product capacity.
User-Defined Functions
Design Optimization
A fenced enclosure has the following measurement. An area A of 1600 ft2 is required, and fencing cost is RM40/foot for the curved part and RM30/foot on the straight sides. Determine the value of R and L to minimize the total cost of the fence.
Example:
A = 1600 ft2
Design Optimization
A = 1600 ft2
The command A = xlsread(filename) imports the Microsoft Excel workbook file filename.xls into the array A. The command [A, B] = xlsread(filename) imports all numeric data into the array A and all text data into the cell array B.
The Import Wizard To import ASCII data, you must know how the data in the file is formatted. For example, many ASCII data files use a fixed (or uniform) format of rows and columns.
(continued )
The End