Graphics Programs Reference
In-Depth Information
[m,n]=size(A); %m=no.ofrows;n=no.ofcols.
ifm˜=n
error('Matrix must be square')
end
1.5
Functions
Function Definition
The body of a functionmust be precededbythe functiondefinition line
function [ output args ]
=
function name ( input arguments )
The input and output arguments must beseparatedbycommas. The number of
arguments may bezero. If there isonly oneoutput argument, the enclosing brackets
may beomitted.
To make the functionaccessible to otherprograms units, it must besaved under
the file name function name .m . This file may contain other functions, called subfunc-
tions . The subfunctionscan becalled onlybythe primary function function name or
other subfunctions in the file; theyare not accessible to otherprogram units.
Calling Functions
A function may becalledwith fewer arguments than appear in the functiondefini-
tion. The number of input and output arguments usedinthe function call can be
determinedbythe functions nargin and nargout , respectively. The following exam-
ple shows amodifiedversion of the function solve that involves two input and two
output arguments. The error tolerance epsilon is an optional inputthat may be used
to override the default value 1.0e-6 . The output argument numIter , which contains
the number of iterations, may also beomitted from the function call.
function [x,numIter] = solve(x,epsilon)
if nargin == 1
% Specify default value if
epsilon = 1.0e-6;
% second input argument is
end
% omitted in function call
for numIter = 1:100
dx = -(sin(x) - 0.5*x)/(cos(x) - 0.5);
x=x+dx;
if abs(dx) < epsilon
% Converged; return to
return
% calling program
end
Search WWH ::




Custom Search