Добавил:
kostikboritski@gmail.com Выполнение курсовых, РГР технических предметов Механического факультета. Так же чертежи по инженерной графике для МФ, УПП. Писать на почту. Дипломы по кафедре Вагоны Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
41
Добавлен:
12.08.2017
Размер:
2.3 Mб
Скачать

88 / Chapter 8 Calculating in Mathcad

Defining and Evaluating Functions

You define a function in much the same way as you define a variable. The name goes on the left, followed by a definition symbol, then an expression 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) that returns the distance between the point (x, y) and the origin.

To make a function definition:

1. Type the function name.

2.Type a left parenthesis followed by one or more names separated by commas. Complete this argument list by typing a right parenthesis.

Note 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.

3.Press [:] or click on the Calculator toolbar to enter the definition symbol (:=).

4.Type an expression to define the function. The expression can contain a name or any previously defined functions and variables.

Once you have defined a function, you can use it anywhere below and to the right of the definition.

When you evaluate an expression containing a function, as shown in Figure 8-5, Mathcad:

1.evaluates the arguments you place between the parentheses,

2.replaces the dummy arguments in the function definition with the actual arguments you place between the parentheses,

3.performs the computation specified by the function definition,

4.returns the result as the value of the function.

Figure 8-5: A user-defined function. First define the points. Next define the function to compute distances to the origin. Then insert arguments.

Defining and Evaluating Functions / 89

Note As shown in Figure 8-5, if you type only the name of a function without its arguments, Mathcad returns the type signature of the function. In this case, the function takes two arguments of the same units (see “Units and Dimensions” on page 90) and returns a result with the units of the arguments. Any unit is allowed here, hence the term “any.”

The arguments of a user-defined function can represent scalars, vectors, or matrices.

For example, you could define the distance function as dist(v) := v 2

+ v 2

. This is

0

1

 

an example of a function that accepts a vector as an argument and returns a scalar result. See Chapter 5, “Range Variables and Arrays” for more information.

Note User-defined function names are font and case sensitive. The function f(x) is different from the function f(x) and SIN(x) is different from sin(x). Mathcad’s built-in functions, however, 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.

Variables in User-Defined Functions

When you define a function, you don’t have to define any of the names in the argument list since 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 the types of the arguments — whether the arguments are scalars, vectors, matrices, and so on. It is only when Mathcad evaluates a function that it needs to know the argument types.

However, if in 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. See the example in Figure 8-6.

Figure 8-6: The value of a user function depends on its arguments. The value of f depends on its argument, but not on the value of a. Since a is not an argument of f, the value of f depends on the value of a only at the point where f is defined.

If you want a function to depend on the value of a variable, you must include that variable as an argument. If not, Mathcad just uses that variable’s fixed value at the point in the worksheet where the function is defined.

Соседние файлы в папке doc