DIGITAL SIGNALING PROCESSING
FUNCTIONS AND FUNCTION FILES IN MATLAB
OBJECTIVE:
To understand concept of Function in MATLAB and to make
function files.
Many functions are programmed inside MATLAB as built-in
functions, and can be used in mathematical expressions simply by typing their
name with an argument example
are sin (x), cos (x),
sqrt(x), and exp (x). Frequently, in computer programs, there is a need to
calculate the value of functions that are not built-in. When a function
expression is simple and needs to be calculated only once, it can be typed as
part of the program. However, when a function needs to be evaluated many times
for different value of arguments it is convenient to create a ''user
defined" function. Once the new function is created (saved) it can be used
just like the built-in functions.
CREATING A FUNCTION
FILE: Function files are
created and edited, like script files, in the Editor/Debugger Window. This
window is opened from the Command Window. In the File menu, select New, and
then select m-file.
FUNCTION
DEFINITION LINE: -
The first executable line in a function file must be the
function definition line. Otherwise the file is considered a script file. The
function definition line:
• Defines the file as a function file
• Defines the name of the function.
• Defines the number and order of the input and output
arguments.
Function [output arguments]
=function_name [input arguments]
INPUT AND
OUTPUT ARGUMENTS: -
The input and output arguments are used to transfer data into
and out of the function. The input arguments are listed inside parentheses
following the function name. Usually, there is at least one
input argument, although it is possible to have a function
that has no input arguments. If there are more than one, the input arguments
are separated with commas.
EXAMPLE:
Write a function
file for the function of the following expression. For the
input to the function is x and output is f(x).
Write the
function such that x can be a vector. Use the function to calculate:
a1)
f(x) for x =6.
a2) f(x)
for x = 1,3,5,7,9, and 11.
Open the Editor/Debugger Window. This window is opened from
the Command Window. In the File menu, select New, and then select m-file. Once
the Editor/Debugger Window opens write the following function in it
function [y] = exp4one(x)
y= (x.^4.*sqrt(3*x+5))./(x.^2+1.^2);
a 1)
Calculating
the function for x = 6 can be done by typing exp4one(6) in the Command
window
>>
exp4one(6)
ans
= 167.9837
2)
To calculate the function for several values
of x, a vector with the values of x is
first created, and then used for the
argument of the function.
>>
x = 1:2:11
x
=
1
3 5 7 9 11
>>
exp4one(x)
ans
= 1.4142 30.3074 107.5033
244.8549 452.6173 739.7802
EXERCISE:
Q.1:
Write down a function that takes value of frequency and at output gives a
cosine wave.
Q.2:
Write down a function that takes value of frequency and generate sawtooth,
square and triangular wave in on plot window. (Frequency =0.5)
function [y] = freq(x)
n=0:0.1:40;
y=sawtooth(2*pi*x*n);
subplot(311)
plot(y);
y=square(2*pi*x*n);
subplot(312)
plot(y);
t = 0:0.1:15;
y=sawtooth(t,x);
subplot(313)
plot(t,y);
Q.3: Write down a function which solve the numerical problems
of chapter 1 of your text book (detail of this question will be explained in
the lab)
function [y] = exp4one(x)
y= (x.^6.*sqrt(3))./(x.^2+x.^9);


No comments:
Post a Comment