Defining variables and functions MathCad Help

Whenever you type an equation into a worksheet, you are doing one of two things:
• You could be typing an expression and asking Mathcad to give you the answer. This is discussed in the next section, “Evaluating expressions.”
• You could be typing a variable or function name and assigning some value to it. The remainder of this section discusses how to do this. Defining a variable A variable definition defines the value of a variable everywhere below the definition. To define a variable, you must follow these three steps:
• Type the variable name to be defined. I Chapter 8, “V~ables and Constants,” IKEII contains a description of valid variable names.
• Press the colon (:) key. The definition symbol (:=) appears

• Type an expression to complete the notion. This expression can include num- KE=m· v
bers and any previously defined variables 2 .
and functions. ‘—————-
Figure 7-1 shows several examples of a variable definition. The left hand side of a “:=”
can contain any of the following:
• A simple variable name like x.
• A subscripted variable name like vi’
• A matrix whose elements are any of the above. For example:
• A function name with an argument list of simple variable names. For example, f{x, y, z). This is described further in the next section.
• A superscripted variable name like M

Figure 7-1: Defining variables.

Figure 7-1: Defining variables.

Defining a function

YQU can also define your own functions in Mathcad. Unlike a variable, the value of a function depends on the values of its arguments. You define a function in much the same way you define a variable. The name goes on the left, a “:=” goes in the middle, and an expression goes on the right. The main difference is that the name includes an argument list. The example below shows how to define a function called dist(x, y) which returns the distance between the point (x, y) and the origin. To type such a function definition:
• Type the function name.

• Type a left parenthesis followed by one or more names separated by commas. Complete this argument list by typing a right parenthesis. It makes no difference whether or not the names in the argument list have been defined or used elsewhere in the worksheet. What is important is that these arguments must be names. They cannot be more complicated expressions.
• Press the colon (: ) key. You see the nition symbol (:=).

• Type an expression to define the function. In this example, the expression involves only the names in the argument list. In general though, the expression can contain any previously defined functions and variables as well. dist(x.y)

Once you have defined a function, you can use it anywhere below the definition, just
as you would use a variable.
When you use a function in an equation, Mathcad:
• evaluates the arguments you place between the parentheses,
• replaces the dummy arguments in the function definition with the actual arguments you place between the parentheses,
• performs whatever arithmetic is specified by the function definition, and

• returns the result as the value of the function. Figure 7-2 shows an example

Figure 7-2: A user-defined function to compute the distance to the coordinate origin.

Figure 7-2: A user-defined function to compute the distance to the coordinate

The arguments of a user function can represent scalars, vectors, or matrices. For example, you could define the distance function asThis is an example of a function that accepts a vector as an argument, and returns a scalar result. See the section “Arrays and user-defined functions” in Chapter 10 for more information. Note that function names are font sensitive. This means that the function f(x) is different from the function f(x). Figure 7-3 shows an example.
Mathcad’s built-in functions are defined for all fonts (except the Symbol font), sizes, and styles. This means that sin(x), sin(x), and sin(x) all refer to the same function .

Figure 7-3: Function names are font sensitive. Undefined variables are marked in reverse video.

Figure 7-3: Function names are font sensitive. Undefined variables are
marked in reverse video.

Variables in user-defined functions

When you define a function, you don’t have to define any of the names in the argument list. This is because when you define a function, you are telling Mathcad what to do with the arguments, not what they are. When you define a function, Mathcad doesn’t even have to know whether the arguments are scalars, vectors or matrices. All it needs to know is how many arguments there are and what to do with them. It is only when Mathcad actually uses a function that it needs to know what the arguments really are. However, if in the process of defining a function you use a variable name that is not in the argument list, you must define that variable name above the function definition. The value of that variable at the time you make the function definition then becomes a permanent part of the function. This is illustrated in Figure 7-4. When you evaluate a function, Mathcad:
• evaluates the arguments

Posted on November 21, 2015 in Equations and Computation

Share the Story

Back to Top
Share This