Mathcad: Using Vectors Instead of Range Variables

Much of the material below is from Chapters 1, 5, and 11 of the book Essential Mathcad ©2009, Elsevier Inc.

Note: For this worksheet, ORIGIN is set to 1

Vectors

A vector is simply a matrix with only one column.

Use the Insert Matrix dialog box to create a matrix. This dialog box can be accessed in three ways: Selecting Matrix from the Insert menu; typing the shortcut CTRL+ M.; or selecting the matrix icon (showing a three by three matrix) on the Matrix toolbar. For a vector, the columns field in the dialog box should be set to 1.

Range Variables

Range variables have a beginning value, an incremental value, and an ending value. To define a range variable:

  1. Type the variable name followed by a colon;
  2. In the placeholder, type the beginning value followed by a comma.
  3. In the new placeholder type the incremental value followed by a semicolon. This inserts two dots and adds a third placeholder.
  4. Now enter the ending value.

Range variables are best used to increment expressions, iterate calculations, and to set plotting limits. When you use range variables to iterate a calculation, it is important to understand that Mathcad begins at the beginning value and iterates every value in the range. You cannot tell Mathcad to only use part of the range variable. If you use a range variable as an argument for a function, the result is another range variable, which means that the result is displayed, but you cannot access individual elements of the result. Also, you cannot assign the result to a variable.

Even though it is possible to use a range variable as an argument for a function, it is best to use a vector, so that each element of the result can be assigned and accessed.

It is important to understand the difference between a range variable and a vector.

The following two examples illustrate the benefits of using vectors rather than range variables when you need to assign and access the results of a function or equation.

Example 1 Range variable vs vector in user-defined functions

This example compares the difference between using a range variable and a vector as input to a function.

1. Using a Range Variable

Create range variable RV

Create user-defined function

The range variable may be used as the argument of the function. Units may also be added to the function argument. The range variables do not need to be integers when using them in a function.

The results are displayed, but you cannot access or reuse individual results.

Mathcad gives you an error if you try to assign this result to a variable.

2. Using a Vector

In order to make each result accessible, you need to assign the results to a vector.

Create vector "v" similar to the range variable. Attach units of cm to the vector. Type CTRL+ M to start the vector.

The answers are the same as above, but by using a vector, each element of the result is now accessible.

Note: The above subscript is an array subscript typed by using the " [ " key. It is different from a literal subscript typed by using the " . " key. Array subscripts are used with vectors and matrices.

Example 2 Range variable vs vector in an expression

Use the range variable "RV" and the vector "v" from the previous example.

1. Using a range variable

Type the expression π *RV2 to use the previously defined variable "RV." The results are displayed, but are not accessible, and the expression cannot be assigned to a variable.

Mathcad gives you an error if you try to assign this result to a variable.

2. Using a Vector

Assign the variable CircleArea_1 to the expressions π *v2, where "v" is a previously defined vector variable.

Each element of the results is now accessible.

A user-defined function to create a range vector

Mathcad does not have a simple way to create a vector with a range of values.

The following function creates a "range vector" similar to creating a range variable. It is from Figure 11.8 of the book Essential Mathcad.

This function creates a vector with a range of values. This is based on a function in the February 2002 Mathcad Advisor Newsletter.

The for loop creates a range variable for iterating from the three input arguments. The vector "Vec" is a local variable.

Create a variable with range from 0 to 1 with 0.1 increment.

These two variables appear to be identical, but they behave very differently.

If you would like to have the above function always available in your worksheets, add the function to your default template "Normal.xmct."

Much of the material above is from Chapters 1, 5, and 11 of the book Essential Mathcad ©2009, Elsevier Inc.

Learn more with other tutorials...

Enhancing Student Learning via Mathcad 15.0's Live Mathematical Capabilities
Polar Plotting in Mathcad
  • Polar Plotting in Mathcad

  • |
  • 8919 Views
  • |
  • Intermediate Level
  • |
  • This tutorial will demonstrate how to plot a polar plot in Mathcad
Creating a Box Plot with Mathcad Prime 1.0
  • Creating a Box Plot with Mathcad Prime 1.0

  • |
  • 2163 Views
  • |
  • Introductory Level
  • |
  • This brief video illustrates how to use Mathcad Prime 1.0 to make a visual comparison of two data sets using a box-and-whisker plot.
Want more? Boost your PTC Mathcad skills!

Looking for PTC Mathcad Training?

Whether you're looking for specific training courses, or a personalized corporate learning program that meets your training schedule and budget requirements, PTC University can help you boost your productivity.
Contact us today   
Replay video? Resume video