Arrays (includes sequences, and procedures)


This page is designed to explain the commands used in Dr. Ellerbruch's Fibonacci worksheet.

Maple has many commands related to generating tables and arrays. Only a few of them are considered here. The sequence command generates a sequence of values for a function. The procedure command (proc) allows us to enter a function and then retrieve values of that function.

array

Using the array command allows us to create an array or table of values on Maple. Typing v := array( parameters ); renders an array specified by the parameters and assigned to the variable v. (You may use other variables.) We can make arrays of both one and two dimensions.

The simplest one-dimensional array is one in which we type the command array followed by the elements in the array, and assign it to a variable as follows. Make sure to place the elements in square brackets enclosing the brackets with parentheses.

If we want to know the third element of the array, we type

However, if we ask for the fourth element, Maple returns an error message, because there is no fourth element in this array.

Another way to create a one-dimensional array is by specifying the length in the parameters. The following set of commands creates a one-dimensional array of length 5. Use a colon and press RETURN after the first statement. Then press ENTER after the semi-colon. Make sure to place TWO DOTS between the first and last element.

Typing A[ 5 ] := x: assigns the value x to the fifth element of the array A. Then A[ 5 ]; returns x. Use a colon and press RETURN after the first statement. Then press ENTER after the semi-colon.

Two-dimensional arrays are created in the same way that one-dimensional arrays are, except that two bounds are specified in the parameters. If we want Maple to print an array with two rows and three columns, we type

Assigning values to elements is a little more complicated, because both the row and column must be specified in the square brackets.

We can place values within an array by using a for-loop and the print command. The following array consists of the values of the function, f( i ) = 2*i for i = 1 to i = 3.

(Notice that Maple prints C4 as the fourth element, because the for-loop only plugs three values into the function for i from 1 to 3.)

sequence

Another function related to a for-loop is the sequence (seq). The sequence behaves like a for-loop, except that it constructs a sequence of values and is more efficient than a for- loop. In general the command

seq( f( i ), i = m..n );

generates the sequence

f( m ), f( m + 1), ..., f( n )

If we want to generate a sequence for f( i ) = i^2 from i = 0 to i = 5, we type

We can also assign a sequence of values to a variable such as L, but we must enclose the command seq and its parameters in square brackets.

proc

Maple will evaluate a function using familiar notation whenever we enter a procedure for the function. For example, if we would like Maple to calculate the function f( x ) = x^2 for various values of x, we type

(Note that we press RETURN after the first two lines of text and, then, press ENTER after the last line.)

After entering the procedure we can ask Maple to calcualate f( x ) for different values of x.


Link to Maple Array Handout

Link to Maple Home Page


Back to Ellerbruch Web Site Home

Link to Math Department Home

Link to Northern Michigan University


Jo Norby-Davis: jnorbyda@nmu.edu.