Вы находитесь на странице: 1из 17

# Spring 2015

MATLAB Chapter 3
Functions and Files

##  Table 3.1-1 Some common mathematical

functions Examples
Attention  exp(x) exponential, ex
 log(x) natural logarithm, ln(x)
 log10(x) base 10 logarithm, log(x)
 ceil(x) Round to the nearest integer
towards .
 imag(x) Imaginary part of a complex
number x
 etc…
2

1
Spring 2015

##  Table 3.1-2 Trigonometric

functions Examples
 cos(x) cosine of x
 sin(x) sine of x
 atan(x) Inverse tangent of x
 etc…

##  Table 3.1-3 Hyperbolic

functions Examples
 cosh(x)  hyperbolic cosine of x;
cosh(x) = (ex + e-x)/2
 sinh(x) hyperbolic sine of x;
 sinh(x) = (ex - e-x)/2
 acosh(x)  Inverse hyperbolic cosine of x
 etc…

2
Spring 2015

Function File

##  Function files are useful when you need to repeat a set

of commands several times.

##  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.

3
Spring 2015

Function File

##  The function_name should be the same as the file name in which it is

saved (with the .m extention), i.e. if you name a function drop, it should be
saved in the file: drop.m

 The function can be used by typing its name (e.g. drop) at the command
line. The word function in the function definition line must be lowercase.

 Before choosing a function name, use the exist function to see if another
Matlab built-in function has the same name.

Function File

##  Example1: Write a function to calculate the the volume of a cube,

given its height, width and length.

volume.m

4
Spring 2015

Function File

##  Example1: Write a function to calculate the the volume of a cube,

given its height, width and length.

volume.m
function vol=volume(height,width,length)
vol=height*width*length;

In Command Window

>> V=volume(2,2,3)
no need to use
brackets when having V =
only one variable
12

## 3.2 User Defined Functions

Function File
 Example2: Write a function to calculate the area and the volume of a
cube, given its height, width and length.

10

5
Spring 2015

## 3.2 User Defined Functions

Function File
 Example2: Write a function to calculate the area and the volume of a
cube, given its height, width and length.
cube.m
function[area,volume]=cube(height,width,length)
area=width*length;
volume=height*width*length;

In Command Window
>> [A,V]=cube(2,2,3) >> cube(4,5,6)
>> A=cube(2,2,3)
A = ans =
A =
6 30
6
Here we only get value of
V = first variable, i.e. area
12

11

## 3.2 User Defined Functions

Function File
 Example3: Write a function to calculate the area and circumference
of a circle given its radius.

12

6
Spring 2015

## 3.2 User Defined Functions

Function File
 Example3: Write a function to calculate the area and circumference
of a circle given its radius.
circle.m

In Command Window
Dr.
>> r=4; >> [A,C]=circle([4,2,3])
>> [A,C]=circle(r)
A =
A =
50.2655 12.5664 28.2743
50.2655

C =
C =
25.1327 12.5664 18.8496
9
25.1327
13

Function File

##  Example4: Write a function to show date in Matlab when typing

“showdate” in the command window.

14

7
Spring 2015

Function File

##  Example4: Write a function to show date in Matlab when typing

“showdate” in the command window.

showdate.m
function showdate
today=date

In Command Window
“date” is a built-in
Matlab function that >> showdate
shows current date
today =

23-May-2009
15

## 3.2 User Defined Functions

Function File

 It is possible to use the same variable in the input and the output
variables fields of a function if it needs to be updated. For example,
the function area_change returns the radius of a circle after its
original area has increased by a factor f.
area=f*area;

>> [r_new,A_new]=area_change(10,2)

r_new =

2.5231

A_new =
16
20

8
Spring 2015

Global Variables

##  The global var1 command declares the variable

var1 global, and therefore its value is available to the
basic workspace and to other functions that declare this
variable global.

##  For example, constants in Physics and Chemistry

problems can be declared as global variables.

17

## 3.2 User Defined Functions

Global Variables
 For example, we can write a function to solve for the pressure from
the equation P = R*T/V.
%Without using global variables and letting the user enters
%the value of R
function P = ideal_1(T,V,R)
P= R*T/V;

## %Without using global variables and defining the value of R

%in the function itself. Problem: units are always fixed.
function P = ideal_2(T,V)
R=0.08206;
P= R*T/V;

## %Defining R as a global varibale

function P = ideal_3(T,V)
global R
P= R*T/V;

18

9
Spring 2015

## 3.2 User Defined Functions

Global Variables
%Defining R as a global varibale
function P = ideal_3(T,V)
global R
P= R*T/V;

In Command Window
>> global R
>> R=0.08206;
>> pressure=ideal_3(300,20)

pressure =

1.2309

19

## 3.2 User Defined Functions

Finding the zeros of a function
 The fzero function can be used to find the zero of a function of a
single variable x.
 First, the y=f(x) function needs to be written in a function M-file.
 Second, in the command window, the function
fzero(‘function_name’,x0) can be written to find the zero
crossing closest to the initial guess x0. For example
function y = f_1(x)
y=x+2*exp(-x)-3;
In Command Window
Dr. ayeh
>> fzero('f_1',-1) >> fzero('f_1',3)

ans = ans =

-0.5831 2.8887

20

10
Spring 2015

## 3.2 User Defined Functions

Finding the zeros of a function

##  If the function has more

than one zero, it is
better to plot it first, as
the fzero function
only returns one zero
closest to x0.

##  Note: If a function only

touches the zero axis
(tangent), no solution is
returned.

y=x+2*exp(-x)-3

21

## 3.2 User Defined Functions

Finding the zeros of a function

##  Other ways to write the fzero function:

 [x,vlaue]=fzero(‘function_name’,x0)
returns the value of the function at the solution x.

[x,vlaue,sol]=fzero(‘function_name’,x0)
returns the value of the function at the solution x and
returns if the function found a zero or not.
If the variable sol is positive, then there is a solution, if
it is negative, the function did not find a zero.

22

11
Spring 2015

## 3.2 User Defined Functions

Minimizing a Function of one variable
 The fminbnd(‘function_name’,x1,x2) finds the minimum of a
function in the interval [x1,x2].
 For example consider the function y=x+2*exp(-x)-3.
 The function y has a
minimum at x=0.6932.

23

## 3.2 User Defined Functions

Minimizing a Function of one variable
 The function minimum can be found as follows:
function y = f_1(x)
y=x+2*exp(-x)-3;

In Command Window
>> fminbnd('f_1',0,1)

ans =

0.6932

19

12
Spring 2015

## 3.2 User Defined Functions

Minimizing a Function of one variable
 Other ways to write the fminbnd function:
 [x,vlaue]=fminbnd(‘function_name’,x0) returns the
value of the function at the solution x.

## [x,vlaue,sol]=fminbnd(‘function_name’,x0) returns the

value of the function at the solution x and returns if the function
converged to a solution or not.
If the variable sol is 1, then there is a solution, if it is different
than 1, the function did not converge to a solution.

 Note:
 To find the maximum of a function, use the fminbnd function
with the negative of the function of interest.

29

## 3.2 User Defined Functions

Minimizing a Function of several variables
 The fminsearch(‘function_name’,x0) finds the minimum of a
function of more than one variable, where x0 is a vector containing the initial
guess of the variables.
 For example consider the function z=x*exp(-x2-y2), we first define it in an M-
file, using the vector x whose elements are x(1)=x and x(2) = y.
function z = f(x)
z=x(1).*exp(-x(1).^2-x(2).^2);

In Command Window
>> fminsearch('f',[0,0])

ans =

-0.7071 0.0000
26

13
Spring 2015

## 3.2 User Defined Functions

 Minimizing a Function of several variables
 Other ways to write the fminsearch function:
 [x,vlaue]=fminsearch(‘function_name’,x0) returns
the value of the function at the solution x.

 [x,vlaue,sol]=fminsearch(‘function_name’,x0)
returns the value of the function at the solution x and returns if the
function converged to a solution or not.
 If the variable sol is 1, then there is a solution, if it is different than 1,
the function did not converge to a solution.

27

28

14
Spring 2015

## 3.4 Working with Data Files

Importing Data Files

##  Easiest way: Use Import Wizard under Files/Import Data. Both

numeric and text data can be imported to Matlab.

 In Command Window:
 If the data file contains only numbers separated by space, tabs,
or commas, and file is saved under the Matlab search path, use
the load command followed by file name and extension to
import file. The data inside the file will be saved in an mxn array
having the same name as the file.
 For example, load filename.txt

29

##  Type [A,B]=xlsread(‘filename’) in the command window to

import Excel files (or wk1read for some other spreadsheet
programs).
 Numeric data are saved in array A and text data in array B.

30

15
Spring 2015

files

command:

## where filename.out is the file where the data will be

stored in the current directory, and arrayname is the
name of the Matlab array. By default, data will be space
delimited.

31

Problems

32

16
Spring 2015

Problems

33

17