CS 99

Summer 2002: Lab 4                                                                                07.11

Colon notation, introduction to the for loop

Reading Assignment: 4.2

1.    Objectives

Completing all tasks in this assignment will help you:

First skim, then carefully read the entire assignment before starting any tasks.

 

2.    Constructing arrays using the colon notation
Earlier in the course, we learned to construct arrays in two ways: by using the linspace function and by typing individual elements:

>>x = linspace(0, 2*pi, 1000); %create an array with 1000 elements with values from 0 to 2π
>>M = [1 –8 23 45 99]; %create an array with 5 elements, whose values are 1, -8, 23, 45, 99


MATLAB also has a facility for creating arrays using the
colon notation:

>>x = 1:100;     %create an array with 100 elements from 1 to 100
>>y = 98:-2:0;   %create an array with 50 elements with values 98, 96, …, 2, 0


The notation is (
starting_value):(step_size):(ending_value). This way of creating arrays can be very convenient when we want to create large arrays with particular properties.  When we combine the colon notation with the dot notation we can make all kinds of interesting arrays.

To demonstrate your understanding, see if you can make the following arrays:
 
A)    an array with elements from
0 to 30π in steps of 0.01
B)     an array with 100 elements, alternating the values
1 and 0, starting with 1.
         Hint: think
mod function. Can you make it start with 0 too?
C)    an array with 100 elements whose values are
0, 4, 3, 2, 1, 0, 4, 3, 2, 1, 0, … etc.
         Same hint
D)    an array with the values
13, 23, … , 413.
         Hint: what happens if you raise an array to the power of a number? Don’t forget your dot notation.
E)    the first 51 powers of 2 in order, e.g.,
20, 21, 22, …, 250
         Hint: what happens if you raise a number to the power of an array?  Same warning.

F)     an array containing the values 1, -2, 3, -4, 5, … , -30
         Hint: if we could get an array with alternating 1’s and –1’s, we could multiply our way here.  How to get
         that… could we perhaps raise –1 to some quantity?


Finally, use the colon and dot notations as well as function
sum (help sum) to find the values:

G)   
200 – 199 + 198 – 197 + … + 2 – 1.
H)   
2 + 1 + 13 + 19 + 97 + … + ( 310 + (-2)10 )

Cut and paste the commands you used for Parts A) to H) in a Word file called
LAB4Part1.doc. Label each set of commands with the correct letter, but do not include output. Label the entire section Question 1 in bold.


3.    The for loop
for loops allow a group of commands to be repeated a predetermined number of times.  The general form of a for loop in MATLAB is

    
for index = [ array ]
      statement1
       ….
      statementn
    end

The statements between the for and end are executed once for every column in array.  At each iteration, index is assigned the next column of array.  After the loop has finished executing, index is equal to the last column of array. 

array can be anything you want; however, you will find arrays created with the colon notation useful for nearly all circumstances you will encounter.

Example 1.  Display the values from 10 to 1 in descending order.
>>
for ii = 2:9:23, disp( ii ), end

Does the ending value for ii make sense to you in this example? Practice with your own arrays for
index. Do any variables get created in the workspace when you run a for loop? Can you predict how many iterations your for loop will make?

Here’s an example of a completely general array for
index:

Example 2.  Any
index will work (index can also be a matrix).
>>
for k = [‘Down we gooooo.....’], disp( k ), end

We’ll discuss
for loops more in Monday’s lecture.

Required Task:
Write a short program that accepts the input values
x and n from the user, and returns the value
    
           
res = (1 + x) + (1 + x)2 + (1 + x)3 + ... + (1 + x)n.

Use a
for loop in your solution.

Save your program as the M-File,
LAB4Part2.m.
 

4.    Submitting Your Work
Type your name (and your partner’s name if you have one), student ID, and the date at the top of each document.  Print each file and sign them along with your partner.  Give the signed documents to the teaching assistant at the end of the lab session.