474 Chapter 15 MATLAB
The Loop Control — forand whilecommands
When writing a computer program, often it becomes necessary to execute a line or a block of your
computer code many times. MATLAB providesforandwhilecommands for such situations.
The forLoop Using the forloop, you can execute a line or a block of code a specified (defined)
number of times. The syntax of a forloop is
for index = start-value : increment : end-value
a line or a block of your computer code
end
For example, suppose you want to evaluate the function yx
2
10 for xvalues of 22.00
22.50, 23.00, 23.50, and 24.00. This operation will result in corresponding yvalues of 494.00,
516.25, 539.00, 562.25, and 586.00. The MATLAB code for this example then could have the
following form:
x = 22.0;
for i = 1:1:5
y=x^2+10;
disp([x',y'])
x = x + 0.5;
end
■Figure 15.6 MATLAB’s
Command Window for
Example 14.4 (Revisited).
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀