Uploaded by Саша Биковець

Program Design and Algorithm Development

advertisement
Program Design and Algorithm Development
An algorithm is a systematic logical procedure for solving a
problem.
A structure plan is a representation of an algorithm in pseudocode.
А function M-file is a script file designed to handle a particular
task that may be activated (invoked) whenever needed.
MATLAB Functions and Data Import-Export Utilities
If no variable are listed the entire workspace is saved.
The extension .mat is the default— you can specify a
different extension.
Seek belp for all the save options.
Logical Vectors
When a relational and / or logical operator operates on a
vector expression, the operation is carried out element by
element. The гesult is a logical vector consisting of 0s (FAISE)
and 1s (TRUE).
A vector may be subscripted with a logical vector of the same
size. Only the elements corresponding to the 1s in the logical
vector are returned.
When one of the logical operators ("&) orates on an extra zero
value in an operand is regarded as TRUIE; zero is regarded as
FAISE. A logical vector is returned.
Arithmetic, relational, and logical operators may appеar in the
same expression. Great care must be taken in observing the
correct oregiog precedence in such situations.
Vectors in a logical expression must all be the same size.
If a logical expression is a vector or a matrix, it is considered
true in an "if" statement only if all its elements are non-zero.
The logical functions any and all return scalars when taking
vector arguments, and are subsequently useful in "if" states.
Logical vectors may often be used instead of the more
conventional elseif ladder. This provides faster more elegant
code, but request more ingenuity and the code may be less
clear to read later on.
Matrices and Arrays
A matrix is a 2-D array. Elements may be referenced in the
conventional way with two subscripts. Alternatively, one
subscript may be used, in which case the matrix is "unwound"
by columns.
The colon operator may be used to represent all the rows or
columns of a matrix, and also as a single subscript.
The keyword end refers to the last row or column of a matrix.
Use repmat to duplicate rows or columns of a matrix.
Use the empty array [ ] to delete rows or columns of a matrix.
Arrays may have more than two dimensions. In the case of a
3-D array the third subscript may be thought of as numbering
pages, where each page contains matrix defined by the first
two subscripts.
The matrix operations of multiplication and exponentiation
are implemented with the matrix operators * and ^.
The matrix left division operator \ is used for solving systems
of linear equations directly. Because the matrix division
operators \ and / can sometimes produce surprising results
with the least squares solution method, you should always
compute the residual when solving a system of equations.
If you work with large matrices with relatively few non-zero
entries you should consider using MATLAB's sparse matrix
facilities.
Function M-files
Good structured programming requires real problem-solving
programs to be broken down into function M-files.
The name of a function in the function definition line should
be the same as the name of the M-file under which it is saved.
The M-file must have the extension .m.
A function may have input and output arguments, which are
usually its only way of communicating with the workspace.
Input / output arguments are dummy variables
(placeholders).
Comment lines up to the first non-comment line in a function
are displayed when help is requested for the function.
Variables defined inside a function are local variables and are
inaccessible outside the function.
Variables in the workspace are inaccessible inside a function
unless they have been declared global.
A function does not have to have any output arguments.
Input arguments have the apprearance of being passed by
value to a function. This means that changes made to an
input argument inside a function are not reflected in the
actual input argument when the function returns.
A function may be called with fewer than its full number of
input / output arguments.
The functions nargin and nargout indicate how many input
and output arguments are used on a particular function call.
Variables declared persistent inside a function retain their
values between calls to the function.
Sub-functions in an M-file are accessible only to the primary
function and to other sub-functions in the same M-file.
Private functions are functions residing in a sub-directory
named private and are accessible only to functions in the
parent directory.
Functions may be parsed (compiled) with the pcode function.
The resulting code has the extension .p and is called a P-code
file.
The Profiler enables you to find out where your programs
spend most of their time.
A handle for a function is created with @. A function may be
represented by its handle. In particular the handle may be
passed as an argument to another function.
feval evaluates a function whose handle is passed to it as an
argument.
MATLAB first tries to use a name as a variable, then as a builtin function, and finally as one of the various types of function.
Command / function duality means that new commands can
be created with function M-files, and that command
arguments may be generated with string manipulations.
The Editor / Debugger enables you to work through a script or
function line-by-line in debug mode, examining and changing
variables on the way.
A function may call itself. This feature is called recursion.
Download