Professional Documents
Culture Documents
MATLAB Chapter 3: 3.1 Elementary Mathematical Functions
MATLAB Chapter 3: 3.1 Elementary Mathematical Functions
MATLAB Chapter 3
Functions and Files
1
Spring 2015
2
Spring 2015
3
Spring 2015
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.
volume.m
4
Spring 2015
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
10
5
Spring 2015
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
12
6
Spring 2015
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
14
7
Spring 2015
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
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.
function [radius,area]=area_change(area,f)
area=f*area;
radius=sqrt(area/pi);
>> [r_new,A_new]=area_change(10,2)
r_new =
2.5231
A_new =
16
20
8
Spring 2015
17
18
9
Spring 2015
In Command Window
>> global R
>> R=0.08206;
>> pressure=ideal_3(300,20)
pressure =
1.2309
19
ans = ans =
-0.5831 2.8887
20
10
Spring 2015
y=x+2*exp(-x)-3
21
[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
23
In Command Window
>> fminbnd('f_1',0,1)
ans =
0.6932
19
12
Spring 2015
Note:
To find the maximum of a function, use the fminbnd function
with the negative of the function of interest.
29
In Command Window
>> fminsearch('f',[0,0])
ans =
-0.7071 0.0000
26
13
Spring 2015
[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
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
30
15
Spring 2015
31
Problems
32
16
Spring 2015
Problems
33
17