Graphics Programs Reference
In-Depth Information
variables that are local to themselves and do not appear in the main
workspace. This is an example of a function:
function x = quadratic(a,b,c)
% QUADRATIC Find roots of a quadratic equation.
%
% X = QUADRATIC(A,B,C) returns the two roots of the
% quadratic equation
%
%
y = A*x^2 + B*x + C.
%
% The roots are contained in X = [X1 X2].
% A. Knight, July 1997
delta = 4*a*c;
denom = 2*a;
rootdisc = sqrt(b.^2 - delta); % Root of the discriminant
x1 = (-b + rootdisc)./denom;
x2 = (-b - rootdisc)./denom;
x = [x1 x2];
Function m-files must start with the word function , followed by
the output variable(s), an equals sign, the name of the function, and
the input variable(s). Functions do not have to have input or output
arguments. If there is more than one input or output argument, they
must be separated by commas. If there are one or more input arguments,
they must be enclosed in brackets, and if there are two or more output
arguments, they must be enclosed in square brackets. The following
illustrate these points (they are all valid function definition lines):
function [xx,yy,zz] = sphere(n)
function fancyplot
function a = lists(x,y,z,t)
Function names must follow the same rules as variable names. The file
name is the function name with “ .m ” appended. If the file name and
the function name are different, matlab uses the file name and ignores
the function name. You should use the same name for both the function
and the file to avoid confusion.
Following the function definition line you should put comment lines
that explain how to use the function. These comment lines are printed in
the command window when you type help followed by the m-file name
at the prompt:
>> help quadratic
QUADRATIC Find roots of a quadratic equation.
Search WWH ::




Custom Search