Biomedical Engineering Reference
In-Depth Information
problems. First, the programmer must be familiar with the
tools available in a programming environment such as
MATLAB.
All three of these statements have one path in and one
path out;
this
standardizes program design and
development.
2.1b.3.1 Sequences of statements
2.1b.3 Programming language
tools and techniques
A sequence of instructions is delimited by special sym-
bols or keywords. These delimiters indicate the single
beginning and end to the block.
There are three classes of programming languages in use
today: the imperative, or state transition class; the object-
oriented class; and the functional class. In each class,
there are a large number of programming languages. How
can an engineer decide which class and which language
to use?
The best examples of imperative or state-transition
languages are C and Fortran. Programs written in these or
other imperative languages are executed one statement
at a time, in the order that the statements are written.
Object-oriented programming languages constitute
the largest and most widely used class of languages today
and include C þþ , Java, Python, and Smalltalk, the
programming language that started the object-oriented
revolution. Object-oriented programs execute much dif-
ferently than imperative programs; the operations exe-
cuted are dictated by the type of the data object and the
desired action. The implementation of an action will
depend on the type of the object (think about the dif-
ference between adding two integers and adding two
vectors) although the name of the operation (e.g., add )
may be the same in both cases. The examples in this
chapter are written in MATLAB, an object-oriented pro-
gramming language and environment. The user not fa-
miliar withMATLAB is strongly encouraged to be familiar
with Appendix A, an introduction to the MATLAB pro-
gramming language and environment.
Functional programming languages are written as a set
of definitions of functions and a composition of these
functions to be executed. The execution of this compo-
sition can be sequential, like the imperative languages, or
parallel. Some common examples are Scheme, Sisal, or
Caml. Functional languages often include object-oriented
properties. This class of languages will not be considered
here.
Both imperative and object-oriented languages have
common programming language constructs that are
designed to help the programmer develop readable and
testable programs efficiently. These programming lan-
guage constructs (statements) allow the programmer to
manage the computation being performed and the flow
of execution. Modern programming languages limit the
control flow statements to one of three types: a sequence
of statement; a statement for conditional execution; and
a control structure for repeating a sequence of statement.
These languages are called block-structured languages.
Example 2.1b.1 Programs
that are sequences of
statements.
Create a vector of the even whole numbers between 31
and 75.
Solution
The even whole numbers between 31 and 75 are the even
numbers 32 to 74. There are two ways to create
the vector: First, by typing the list of numbers or second,
by using the MATLAB shorthand notation first:
increment:last.
The solution using the first method is left to the
reader. The solution using the second method is as
follows:
1. The first number in the vector is 32.
2. The increment is 2, since the vector is to contain only
the even numbers.
3. The last number in the vector is 74.
Therefore, the vector is specified as [32:2:74]. The
MATLAB output is:
>> x ¼ [32:2:74]
x ¼
Columns 1 through 11
32 34 36 38 40 42 44 46 48 50 52
Columns 12 through 22
54 56 58 60 62 64 66 68 70 72 74
2.1b.3.2 Conditional execution
A conditional statement is required if there is a need
for an execution path that depends on the values of the
data input, or external input. These conditional state-
ments also have delimiters: generically, if.then or
if.then.else. There is still only one entrance to the
block at the if keyword and one exit where the
branch(es) return to the main line of the control flow.
2.1b.3.2.1 If-then statements
The simplest form of conditional execution is the
if.then statement, which in MATLAB has
the form if expression statements end. If the
expression is evaluated to be true, then the statement
Search WWH ::




Custom Search