This section shows how to use range variables to perform iteration

**Iteration over a range**

The simplest kind of iteration in Mathcad is just a generalization of scalar calculations. Any calculation you can perform once, you can perform over a range of values.

For example, suppose you want to create a list of x and y values for points on the polar curve r = cos(8) + 1 . The basic idea is as follows:

• 8 should take on values between 0 and 2

• For each 8, the corresponding r is given by r = cos (8) + 1 .

• For each rand 8, there is a corresponding x and y given by x = r- cos(8)-and y = r· sin (8) .

The strategy for solving this problem is simple: create a range variable i and then compute e, r, x, and y for each value of i. The formula for ei defines e to run from 0 to 21t in steps of 21t/ N. To create the other formulas, just put the subscript i on each variable in the formula. Figure 11-6 shows the result.

Notice that in this example i, not e, is defined as the range variable. Since i takes on only whole-number values, it is a valid subscript. On the other hand, e takes on fractional values. It therefore cannot be used as a subscript. In many cases, you can avoid this extra step by using functions instead of vectors. Figure 11-7 shows how to generate the cardioid shown in Figure 11-6 with functions instead of vectors.

By using vector notation and the vectorize operator, you can eliminate the use of a subscript in the last three equations in Figure 11-6. Figure 11-8 shows an example of how to do this

Equations that use vector notation instead of subscripts typically compute much more quickly. For more information, see Chapter 10, “Vectors and Matrices.”

**Multiple range variables and double subscripts**

If you use two range variables in an equation, Mathcad runs through each value of each range variable. This is useful for defining matrices. For example, to define a 5 x 5 matrix whose i,jth element is i +j , type these equations:

i:O;4

j:O;4

x[i,j:i+j

Note that you don’t need to type [Space] to leave the subscript in this case. Typing:leaves the subscript and creates the definition symbol

Figure 11-9 shows the result of typing the above equations. It is usually best to display the matrix in the form shown in Figure 11-9. If instead of typing x= you were to type x [i, j =, Mathcad would show one long output table with 25 numbers. Such a table is often difficult to interpret. A similar problem arises when you use a pair of range variables in a graph.

The Xi,j equation is evaluated for each value of each range variable, for a total of 25 evaluations. The result is the matrix shown at the bottom, with 5 rows and 5 columns.The element in the ith row andjth column of this matrix is i +j .

Note that if the two range variables have m and n values, respectively, then an equation using both range variables will calculate m . n results. If you try to use two range variables in an output table, Mathcad will show these m . n results in a long table with one entry for each result. If you use two range variables in a graph, Mathcad will plot one point for each of the m . n results