MATHCAD'S PROGRAMMING LANGUAGE The Loop Introduction In this section we look at the loop construct in Mathcad programming. A loop is a means of perming a sequence of similar operations repeatedly over some range of values. Looping outside of programs can be accomplished with a range variable. When a Mathcad document is treated as a program, the entire worksheet can be iterated by external scripting of variables. Range variables can be used in many ways, such as filling an array, i 2 X i sin() i accumulating values contained in a specific array, sum_of_cubes sum_of_cubes i sum_of_cubes i X i sum_of_cubes (.769 ) or perming more complicated iterations over large ranges, A B.5 j A j B j A j 2 j j B j exp mod A j B j Range variables can also be used in a programmatic sense with the until function, demonstrated earlier.
These computations may be part of a sequence of steps that lend themselves to a program definition. As we mentioned in the section on assignments, defining range variables is not allowed within a program. The construct there is the loop. The Loop Operator Clicking on the button in the Programming toolbar creates an operator that looks like Note: As with the if operator we emphasize that you cannot type this operator. You must use the button. Bee we give the details, here are some simple uses of the loop in a program. This function creates an array specifying N equal subintervals of the interval [a, b] Partition( ab N ) Δ b a N P i N P i a i Δ Partition( 5).2.4 Partition( 4 4).6.8 2 4
The next function adds the reciprocals of the nonzero elements in a vector: rsum( V) sum x V sum sum if x = sum x otherwise rsum 2.8 rsum So, there you see in action. Here are the details. The first placeholder in the operator, iteration variable is reserved the name of a variable that we'll refer to as the iteration variable: i k x
The second placeholder contains a set of values that the iteration variable will take on over the course of the loop. range, list of values, vector This set of values can be specified with a range i The range may be increasing, decreasing, have a specified increment, or use the default increment (decrement) of (-). Alternatively, the set of values may be specified simply as a list of numbers separated by commas. k 7 59 Lastly, the set of iteration variable values may be the elements of a vector V.2 9. 2. 8. x V Or some combination of the above may be used, separated by commas.
The final placeholder contains an expression or set of expressions, that is, a subprogram that you wish executed each value of the loop variable in the specified range. set of expressions These will usually be local assignment statements but do not have to be. Here are some examples of completed loops. i t i i k 7 59 sum sum k prod x V prod k sign sign if x sign sign if x sign sign otherwise Execution of a loop then perms the indicated computations in the set of expressions each value of the loop variable in the order specified. However, this execution is only of interest (or of use, that matter) within a program.
2.44.72 t i i i 4 5 6 7 2 2.26 2.449 2.646 8 2.828 9.62 You shouldn't need to use the loop outside of a program, mainly because the assignments inside a loop must be local. Range variables in conjunction with worksheet-level vector/matrix assignments should do what is required at the worksheet level. Note that the t assigned above is local only, and not available in the worksheet. t
Examples of programs using the loops above are r i r r 5 2.26 t t i i r Q sum prod k 7 59 sum sum k prod sum prod p sign p 2 x V prod k sign sign if x sign sign if x sign sign otherwise Q 24 2.295 In the case of a range specified with a vector, the order of values the iteration variable takes on is from the first element to the last in sequence.
The symbol is usually read as "element of" or simply "in" and loops such as i x.2 might be read as " i in the set to do..." and " x from to with steps of.2 do the following..." respectively. Note: The three methods described that define the range of iteration may be combined into a single list. Just separate the pieces with commas. For example, the loop in q w q 2..5. q q w 6 5 8 2 ( 2 2) perms the exact same sequence of iterations as 22.9 q w 2..5.5 8 6 2 24682 q q q w 22.9
or W 2..5. 5 8 6 2 2 4 6 8 2 T q w W q q q w 22.9 Note in specifying the values 2..5. 6 5 8 2 ( 2 2) we enclosed the range piece 2 2 in parentheses. Using parentheses is a practice we highly recommend. The comma that appears in a range definition means something different from the comma used to separate values in our iteration value list. These different meanings can lead to confusion. For example at a glance, the list 4 6 could be a list of the values and 4 followed by the range 6 meaning a total set of values: 4 6 7 8 9 or the list could mean the single value followed by the range 46 meaning the set: 4 6 8 Parentheses remove any ambiguity. 4 ( 6 ) ( 46 )
Examples Using Loops As you might guess, loops are especially useful when working with arrays. Our first function takes a function of two variables f as an argument and an integer N and returns an (N+) x (N+) grid of values of f over the square x y fgrid( f N ) S i N S ij j N f i j N N Fxy ( ) sin( x) cos( y) fgrid( F 5).99.89.565.77.84.98.79.69.545.697.822.92.2..486.68.76.825.24.25.9.54.667.697.895.86.26.44.58.54.79.9.5.258.82 Note in this program a loop lives inside another loop. There is nothing wrong with that. For the first value of the outer iteration variable i the innermost loop is executed in its entirety, that is S j j N f j N N
then the next value of the outer iteration variable is used, i S j j N f j N N and so on. Note that this construct means the array S is filled up a row at a time. Our next example locates all occurrences of a number in a vector returning a vector of indices or the number - if no occurrences are found. locate( xv ) count ilast( V) if x = V i I count i count count I if count otherwise 9 locate 2 6..4 locate... 2
Our next example computes the Cartesian product of two vectors. Given two vectors of any sizes, the Cartesian product is the set of all points (vectors of two elements) that can be med by taking an element from the first vector and an element from the second. CartProd( X Y ) ind x X P y Y P ind x y ind ind C CartProd..5.7 C T..5.7..5.7 The two previous examples illustrate how easy it is to define functions that operate on vectors and arrays of unknown size by using loops.
The following example demonstrates that you can iterate over a vector of matrices. The function Prod takes a vector of arrays V and returns the array that is the product of all the arrays in V. Prod( V) P identity rows V A V P P P A 2 2. V 6 8 5 V 2.. 5 5. 4 9.8.8 4.4 2. V 2 V ( 4).7 V V V 2 V 2.4 4. 95.5 2.75 5.29 6.2 2.99 286.5 6.225 5.87 2.4 4. 955. 2.75 52.9 48.6 7.2 82.4 8. 25.6 Prod( V) 2.4 4. 95.5 2.75 5.29 6.2 2.99 286.5 6.225 5.87 2.4 4. 955. 2.75 52.9 48.6 7.2 82.4 8. 25.6 More examples of loops appear in the section Examples of Programming in Mathcad.