You are on page 1of 5

1.

2 Repeated commands and the up-arrow


key
A useful feature of the MATLAB® command line is that a command (or sequence of commands)
from a previously-entered line can be retrieved by pressing the up-arrow key. Using that key
makes repeated execution of a command easy to achieve. The following provides an example of
repeated commands.

A geometric series describes the distance travelled by a ball bouncing on a surface. Consider a
ball that is dropped from a height of 1 meter and then repeatedly bounces on the oor. After
each bounce, the ball reaches a height that is 3/4 of the height from which it previously fell. Let
bn be the height of the ball during bounce number n. Then bn = ¾bn-1. The initial condition is b0 =
1. The total distance after n bounces is: D = b0+ 2 × [b1+....+bn].

Example 1.2.1: Distance the ball will travel between the rst and
fourth bounces (does not include the initial drop or the fourth
bounce).

>> height = 1; bounce = 0; distance = 0;


>> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height
bounce = 1
distance = 1.5000
>> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height
bounce = 2
distance = 2.6250
>> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height
bounce = 3
distance = 3.4688

Feedback?

Note that the same line of commands are repeated. Rather than repeatedly typing or copy-
pasting each line, the up-arrow key can be used.

Try 1.2.1: Using the up-arrow key.

Calculate the distance the ball bounces from when the ball is dropped until the eighth
bounce starts. Use the code provided and the up-arrow key. Try calculating the distance
when the ball is dropped from different heights.
Feedback?

The following example illustrates the use of repeated expressions for calculating the sequence
of Fibonacci numbers. In 1202, Leonardo Pisano Fibonacci posed the problem on answering the
question: Given a pair of rabbits in an enclosed area, how many pairs of rabbits will there be in
one year? A newly-born pair of rabbits, one male and one female, are able to mate once they are
one month old. That pair of rabbits will produce a new pair of rabbits at the end of their second
month, and every month from then on. Rabbits never die. The number of rabbits at the end of a
month can be calculated as the number of rabbits at the beginning of the month plus the
number of rabbits produced by mature rabbit pairs. The sequence of numbers describing the
growth of the rabbit pairs is known as the Fibonacci sequence. The following animation shows
how fast the rabbits reproduce in one year. For more information on Fibonacci numbers, see
Fibonacci Numbers and Nature.

PARTICIPATION
ACTIVITY 1.2.1: Fibonacci rabbit growth during 11 months.

Start 2x speed

After month #11:


#1: 178
#2:
#3:
#4:
#5:
#6:
#7:
#8:
#9:
#10:2466
4
10
16
26
42
68
110
288
rabbits
rabbits
rabbits
rabbits

Captions 
1. The Fibonacci sequence is often illustrated by the growth of rabbit pairs in a one year
period, with 2 rabbits in month 1 and growing to 466 rabbits in month 11.

Feedback?

Fibonacci determined that the rate at which rabbits are produced can be expressed
mathematically:
Let fn be the number of pairs of rabbits after month n. Then fn = fn-1+fn-2. The initial conditions
are f1 = 1 and f2 = 2.

Using these relationships, the following code calculates how many rabbit pairs will exist after 8
months:

Figure 1.2.1: How fast will the rabbits reproduce?

>> fibonacci1 = 1
fibonacci1 = 1
>> fibonacci2 = 2
fibonacci2 = 2
>> fnminusTwo = fibonacci1; fnminusOne = fibonacci2; fibonacci = fnminusOne + fnminusTwo
fibonacci = 3
>> fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne + fnminusTwo
fibonacci = 5
>> fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne + fnminusTwo
fibonacci = 8
>> fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne + fnminusTwo
fibonacci = 13
>> fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne + fnminusTwo
fibonacci = 21
>> fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne + fnminusTwo
fibonacci = 34

Feedback?

The line
fnminusTwo = fnminusOne; fnminusOne = fibonacci; fibonacci = fnminusOne
rst evaluates the statement fnminusTwo = fnminusOne;, then the statement
fnminusOne = fibonacci;, and nally the statement
fibonacci = fnminusOne + fnminusTwo. The sequence of the statements in the line is
critical. The number in the variable fnminusOne is rst stored into the variable fnminusTwo,
replacing the previous number in that storage location. Then, the number in the variable
fnminusOne is replaced with the number stored in the variable bonacci. Finally, the variable
bonacci is assigned the sum of fnminusOne and fnminusTwo. If the
fnminusTwo = fnminusOne; and the fnminusOne = fibonacci; statements are
reversed, the value in fnminusOne will be lost before it can be used.

Try 1.2.2: How many rabbits will exist after two years?

Perform the rabbits calculation shown above. After typing (or copy-pasting) the
commands up to the line
fnminusTwo = fibonacci1; fnminusOne = fibonacci2; fibonacci = fnmin
use the up-arrow to retrieve and execute the last command repeatedly. How many pairs
of rabbits will exist after two years?
Feedback?

PARTICIPATION
ACTIVITY 1.2.2: Repeated commands.

1) Variable num is initially 2. The


command num = num * 2 is
typed and entered once; then the up-
arrow key is pressed followed by
enter, three times. What is num?

Check Show answer

2) What is the next value after 34 in the


Fibonacci rabbits example?

Check Show answer

Feedback?

How was this section?


 
Provide feedback

You might also like