Transcript Functions
4. Week
04.March.2014
Use of M-File
• Editor/Debugger: text editor, debugger; editor
works with file types in addition to .m
(MATLAB “m-files”)
2
Use of M-File
• There are two kinds of M-files:
–Scripts, which do not accept input arguments
or return output arguments. They operate on
data in the workspace.
–Functions, which can accept input
arguments and return output arguments.
Internal variables are local to the function.
Click to create
a new M-File
3
A MATLAB SCRIPT FILE
• Never name your script the same as the name of a variable it computes
otherwise MATLAB will have problems accessing the file.
• Avoid writing anything in your script that clashes with built-in functions.
• Save your script as a m-file. Save it always in the ‘WORK’
directory/folder .Otherwise MATLAB will have problems in accessing
that file.
• Avoid any space between the letters or numbers when you name your
m-file. MATLAB doesn't like it and will create unnecessary problems.
M-File as script file
Save file as filename.m
Type what you want to
do, eg. Create matrices
If you include “;” at the
end of each statement,
result will not be shown
immediately
Run the file by typing the filename in the command window
5
RUNNING A SCRIPT FILE
Function Files in MATLAB
• A function file is also an m-file, like a script
file.
• Function files are like programs or subroutines
in FORTRAN, procedures in PASCAL and
functions in C or C++.
• A function file begins with a function
definition line, which has a well defined list of
inputs and outputs. Without this line the file
becomes a script file.
A function definition line may look slightly different depending on whether there is no
output, single output, or multiple outputs.
function [output variables] = function_name (input variables);
• Examples:
• Function Definition Line
File name
• function [rho, H, F] = motion (x, y, t);
motion.m
• function [theta] = angleTH (x, y);
angleTH.m
• function theta = THETA (x, y, z);
THETA.m
• function [] = circle (r);
circle.m
• function circle (r)
circle.m
Executing a Function
There are two ways in which a function can be executed, weather
it is in built or user written.
(1)With explicit output, (2) Without any output.
(1) With explicit output: This is the full syntax of calling a function.
Both the output and the input list are specified in the call.
Example: if the function reads:
Input variables are
mustalready
be
specified before hand
function [rho, H, F] = motion (x, y, t);
then all the following commands represent legal call (execution)
statements:
[r, h,
f] = motion
(rx,=3.5,
(2,
ry,
[0:100]);
0.001);
angmom,
force]
motion
(xt, yt, time);
Executing a Function-cont.
(2) Without any output:
The output list can be omitted entirely if the computed quantities are
not of immediate interest. This might be the case when the function
displays the desired result graphically.
FUNCTIONS
In addition to the basic operations, we can
also call the internal functions to use, such as
the trigonometry functions, logarithmic,
exponential functions etc. A function is use as
“functionname()”, where the input value to
the function is given inside the parentheses.
>> sqrt(4)
ans =
2
“sqrt()” is a function that takes the square root
of the input variable.
Trigonometry Functions
Note, when you use trigonometric functions, such as sine and cosine, the
input is an angle measured in radiance. If you know the angle measured in
degrees, you can do it as
>> sin(30*pi/180)
ans =
0.5000
Where pi (π) =3.1416 (built in constant). The above express converts the angle in
degrees to radiances first and then evaluated by the sine function. Similarly you
can do
>> cos(30*pi/180)
ans =
0.8660
>> tan(30*pi/180)
ans =
0.5774
EXAMPLE
Please find the value for the following function with a given
variable x.
Function is
1. Way
2. Way
Exponential Functions
we use exp(x) to calculate the xth power to e.
e =2.718.
>> exp(1)
ans =
2.7183
>> exp(2)
ans =
7.3891
Logaritmic Functions
For logarithms,
• the natural logarithm lnx in mathematics is
written log(x) in MATLAB, and
lnx=logex (in mathematics)
For x variable
Lnx
log(x)
Example
in mathematics
in MATLAB
log ten of x in mathematics is log10(x) in MATLAB
Example
We know that ln(1) and lg(1) are both 0!!! İn mathematics
round, floor, ceiling. Rounding of number
round(number) - rounding to the closest integer
floor(number)-rounding towards lesser integer
ceil(number) -rounding towards greater integer
Example
math:round(45.50) -Will equal 46
math:floor(45.60) -Will equal 45
math:ceil(45.20) -Will equal 46
math:round(-4.5) -Will equal -4
math:floor(-4.6) -Will equal -5
math:ceil(-4.20) -Will equal -4
Command-Line Help : List of MATLAB Topics
>> help
HELP topics:
matlab\general
matlab\ops
matlab\lang
matlab\elmat
manipulation.
matlab\elfun
matlab\specfun
matlab\matfun
algebra.
matlab\datafun
matlab\polyfun
matlab\funfun
matlab\sparfun
matlab\scribe
matlab\graph2d
matlab\graph3d
matlab\specgraph
matlab\graphics
…etc...
Intro MATLAB
-
General purpose commands.
Operators and special characters.
Programming language constructs.
Elementary matrices and matrix
-
Elementary math functions.
Specialized math functions.
Matrix functions - numerical linear
-
Data analysis and Fourier transforms.
Interpolation and polynomials.
Function functions and ODE solvers.
Sparse matrices.
Annotation and Plot Editing.
Two dimensional graphs.
Three dimensional graphs.
Specialized graphs.
Handle Graphics.
Command-Line Help : List of Topic Functions
>> help matfun
Matrix functions - numerical linear algebra.
Matrix analysis.
norm
- Matrix or vector norm.
normest
- Estimate the matrix 2-norm.
rank
- Matrix rank.
det
- Determinant.
trace
- Sum of diagonal elements.
null
- Null space.
orth
- Orthogonalization.
rref
- Reduced row echelon form.
subspace
- Angle between two subspaces.
…
Intro MATLAB
Command-Line Help : Function Help
>> help det
DET
Determinant.
DET(X) is the determinant of the square matrix X.
Use COND instead of DET to test for matrix
singularity.
See also cond.
Overloaded functions or methods (ones with the
same
name in other directories)
help laurmat/det.m
Reference page in Help browser
doc det
Intro MATLAB
break command –exits for loop ; used in case of
error.
continue command –ends one for loop iteration
; crudely equivalent to GOTO end
Homework 3
Prepare your script to obtain a table of a
formula for different input parameters.
Please deliver until 17.03.2014