You are on page 1of 45

KNJ2332 Engineering Programming

Week 6 & 7 Functions and Functions File

Outline
Elementary Mathematical Functions User Defined Functions Working with Data Files

Elementary Mathematical Functions


Exponential exp(x) sqrt(x) Exponential; e x Square root; x

Logarithmic log(x)
log10(x)

Natural logarithm; ln x Common (base 10) logarithm; log x = log10 x

Elementary Mathematical Functions


Complex Number abs(x) Absolute value. angle(x) Angle of a complex number. conj(x) Complex conjugate. imag(x) Imaginary part of a complex number. real(x) Real part of a complex number.

Elementary Mathematical Functions


Example:

5 3iis a complex number.

Can be entered in MATLAB in different ways:

Elementary Mathematical Functions


Numeric Functions ceil(x) Round to nearest integer toward . fix(x) Round to nearest integer toward zero. floor(x) Round to nearest integer toward -. round(x) Round toward nearest integer. sign(x) Signum function: +1 if x > 0; 0 if x = 0; -1 if x < 0.

Elementary Mathematical Functions


Example:

Elementary Mathematical Functions


Trigonometric functions: cos(x) cosine; cos 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.

Elementary Mathematical Functions


Inverse Trigonometric Functions: 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.

Elementary Mathematical Functions


Hyperbolic Functions: cosh(x) Hyperbolic cosine Hyperbolic cotangent. Hyperbolic cosecant

coth(x)
csch(x) sech(x) sinh(x) tanh(x)

Hyperbolic secant
Hyperbolic sine Hyperbolic tangent

Elementary Mathematical Functions


Inverse Hyperbolic Functions: acosh(x) acoth(x) acsch(x) Inverse hyperbolic cosine Inverse hyperbolic cotangent Inverse hyperbolic cosecant

asech(x)
asinh(x) atanh(x)

Inverse hyperbolic secant


Inverse hyperbolic sine Inverse hyperbolic tangent;

Elementary Mathematical Functions


Example: For x in the range 0 x 2, confirm that tan(2x) = 2 tan x/(1 - tan2x)

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.

Function files are commonly the building blocks of larger programs.

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.

Output from Example 2:

User-Defined Functions

User-Defined Functions
Example 3: Create a function that converts degrees to radians:

Output from Example 3:

User-Defined Functions

Output from Example 3:

User-Defined Functions

User-Defined Functions
A function may have no input arguments and no output list. For example:

Examples of Function Definition Lines


1. One input, one output:
function [area_square] = square(side)

2. Brackets are optional for one input, one output:


function area_square = square(side)

3. Two inputs, one output:


function [volume_box] = box(height,width,length)

4. One input, two outputs:


function [area_circle,circumf] = circle(radius)

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

It returns a value of x that is near x0.

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

Working with Data Files


Importing Spreadsheet Files
Some spreadsheet programs store data in the .wk1 format. You can use the command M = wk1read(filename) to import this data into MATLAB and store it in the matrix M.

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 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 text header? What character is used as the delimiter, that is, the character used to separate the data items in each row? The delimiter is also called the column separator. (continued )

The Import Wizard (continued)


You can use the Import Wizard to import many types of ASCII data formats, including data on the clipboard. When you use the Import Wizard to create a variable in the MATLAB workspace, it overwrites any existing variable in the workspace with the same name without issuing a warning. The Import Wizard presents a series of dialog boxes in which you: 1. 2. 3. Specify the name of the file you want to import, Specify the delimiter used in the file, and Select the variables that you want to import.

The Import Wizard (continued)

The End

You might also like