Range Variables MathCad Help

A range variable is a variable that takes on a range of values each time you use it. This chapter describes range variables and shows how to use them to perform iterative calculations, display tables of numbers, and facilitate the entry of many data values into a table.

The following sections make up this chapter.

Range variables
How to step through a range of numbers by defining a range

variable.
Output tables
How to display a table of numbers.

Entering a table of numbers
How to use range variables to enter a table of numbers.

Iterative calculations
How to perform iteration with one or two range variables.

Seeded iteration
How to perform iteration when values in one step depend on the values in the previous step. Recursive techniques such as this provide the foundation for solving difference equations with Mathcad.
Vector or subscript notation
When to use the “vectorize” operator rather than subscripts.

Range variables

Iterative processes in Mathcad worksheets depend on range variables. Except for the way it’s defined, a range variable looks just like a conventional variable. The difference is that a conventional variable takes on only one value. A range variable, on the other hand, takes on a range of values separated by uniform steps. For example, you could define a range variable to go from -4 through 4 in steps of 2. If you now use this range variable in an expression, Mathcad evaluates that expression five times, once for each value taken by the range variable.

Range variables are crucial to exploiting Mathcad’s capabilities to their fullest. This section shows how to define and use range variables to perform iteration. For a
description of more advanced iterative operations made possible by the programming operators in Mathcad Professional, turn to Chapter 18, “Programming.”

Defining and using range variables

To define a range variable, type the variable name followed by a colon and a range of values. For example, here’s how to define the variable} ranging from 0 to 15

• Type j and then press the colon key The empty placeholder indicates that mathcad expects  a definition for j At this point mathcad does not know  whether j is to be a conventional variable or a range variable

Defining and using range variables 1

Defining and using range variables 1

This definition indicates that} now takes on the values 0,1,2 … 15. To define a range
variable that changes in steps other than 1, see the section “Types of ranges” on page 216.

Once you define a range variable, it takes on its complete range of values every time you use it. If you use a range variable in an equation, for example, Mathcad must evaluate that equation once for each value of the range variable.

You must define a range variable exactly as shown above. There must be:

• a variable name on the left,
• either a “:=” or a “=” in the middle, and
• a valid range on the right.

In particular, you cannot define a variable in terms of a range variable. For example, if after having defined} as shown you now define i=i + 1 , Mathcad assumes you are trying to set a scalar variable equal to a range variable and marks the equation with an appropriate error message.

One application of range variables is to fill up the elements of a vector or matrix. You can define vector elements by using a range variable as a subscript. For example, to define Xj for each value of}:

• Type x [ j : j A2[Space]+1.

Defining and using range variables 2

Defining and using range variables 2

Figure 11-1 shows the vector of values computed by this equation. Since} is a range variable, the entire equation is evaluated once for each value of}. This defines Xj for each value of} from 0 to 15. The effect is exactly the same as if you had typed

Defining and using range variables 3

Defining and using range variables 3

Using a range variable to define the values of the vector x.

Using a range variable to define the values of the vector x.

To understand how Mathcad computes with range variables, keep in mind this fundamental principle:

If you use a range variable in an expression, Mathcad evaluates the expression once for each value of the range variable.

This principle sums up the difference between expressions with and without range variables. Expressions that involve no range variables have only one value. Expressions that involve range variables take on many values, one for each value of each range variable.

If you use two or more range variables in an equation, Mathcad evaluates the equation once for each value of each range variable. The section “Iterative calculations” on page 222 discusses this in more detail.

Mathcad takes longer to compute equations with ranged expressions since there are many computations for each equation. While Mathcad is computing, the mouse pointer changes its appearance. To learn how to interrupt a calculation in progress, see the section “Interrupting calculations” on page 146.

Types of ranges

The definition ofj in the previous section, ranging from 0 to 15, is the simplest type of range definition. Mathcad permits range variables with values ranging from any value to any other value, using any constant increment or decrement.

To define an arbitrary range variable, type an equation of this form:

k:1,1.1;2

This appears in your document window as

K= 1, 1.1 .. 2

In this range definition:

• The variable k is the name of the range variable itself. It must be a simple name. No subscripts or function definitions are allowed.
• The number 1 is the first value taken by the range variable k.
• The number 1.1 is the second value in the range. Note that this is not the step size. The step size in this example is 0.1, the difference between 1.1 and 1. If you omit the comma and the 1.1, Mathcad assumes a step size of one in whatever direction (up or down) is appropriate.
• The number 2 is the last value in the range. In this example, the range values are constantly increasing. If instead you had defined k := 10 .. 1 , then k would count down from 10 to 1. Even if the third number in the range definition is not an even number of increments from the starting value, the range will not go beyond it. For example, if you define k := 10, 20 .. 65 will take values 10, 20, 30 … 60.

You can use arbitrary scalar expressions in place of 1, 1.1, and 2. However, these values must always be real numbers. Complex numbers do not make sense in range variable definitions because given two complex numbers, there is an infinite number of paths connecting them. Figure 11-2 shows the results of various range variable definitions.

Some valid definitions for range variables.

Some valid definitions for range variables.

Note that if you use a fractional increment for a range variable, you will not be able to use that range variable as a subscript. This is because subscripts must be integers.

Posted on November 20, 2015 in Range Variables

Share the Story

Back to Top
Share This