BASIC1. Function is defined in a text file, just like a script, exept that the first line has the following form:
function [output1, output2, ... ] = name ( input1, input2, ... )2. Each function is stored in a different M-file, which MUST HAVE THE SAME NAME AS THE FUNCTION.
i.e., a function called sind() must be defined in a file called sind.m
EXAMPLE 1. Sine in degreesfunction s = sind(x)%SIND(X) Calculates sine(x) in degreess = sin(x*pi/180);Line 1: Tells OCTAVE that this file defines a function.
- The function is called sind.
- It takes one argument, called x.
- The result is to be known, internally, as s.
- Whatever s is set to in this function is what the user will get when they use the sind function
Line 2: Is a comment line.
- The first set of comments in the file should discribe the function.
- This line is the one printed when the user types 'help sind'.
- It is usual to use a similar format to that which is used by Octave's built-in functions.
Line 3: Does the actual work in this function. ...
End of the function- Functions in Octave do not need to end with 'return'
- (although you can use the 'return' command to make Octave jump out of a function in the middile)
- B/c each function is in a separate M-file, once it reached the end of the file, Octave knows that it is the end of the function.
- The value that s has at the end of this function is the value that is returned.
EXTRA- The function AUTOMATICALLY works with vectors:
If you call the sind function with a vector, it means that the x parameter inside the function will be a vector.
In this case, the sin function knows how to work with vectors, so can give the correct response.
Example 2: Unit stepHere is a more sophisticated function which generates a unit step, defined as:
y = 0 if t < t0, 1 otherwise.
function y = ustep(t, t0)%USTEP(t, t0) unit step at t0% A unit step is defined as% 0 for t <>
% 1 for t >= t0
[m,n] = size(t);
% Check that this is a vector, not a matrix i.e. (1 x n) or (m x 1)
if m ~= 1 & n ~= 1
error('T must be a vector');
end
y = zeros(m, n); %Initialise output array
for k = 1:length(t)
if t(k) >= t0
y(k) = 1; % Otherwise, leave it at zero, which is correct
end
endLine 1: The first line says that
- this function is called ustep
- the user must supply two arguments (known internally as t and t0)
- the result of the function is one variable, called y
Line 2-5: Description of the function.
- This time the help message contains several lines.
Line 6-12:
- The error function print out a message and aborts the function if there is a problem.
- The length function tells us how many elements there are in the vector t
Extra:
We can use this function to create signal.
For example, to create a unit pulse of duration one second, starting at t = 0:
t = -1:0.1:4; % Define a time scalev = ustep(t,0) - ustep(t,1)plot(t,v)axis([-1 4 -1 2])This should display the pulse.. If we then type
whoWe'll get:
*** dynamically linked functions:dispatch*** currently compiled function:_ptl2_ _plt_ isscalar isvector rows_ptl2vv_ axis isstr plot ustep*** local user variables:t v