Programming MathCad Help

With Mathcad Professional, you can write your own programs using
specialized programming operators. A Mathcad program has many
attributes associated with programming languages including conditional branching, looping constructs, local scoping of variables, error handling, the ability to use other programs as subroutines, and the ability to call itself recursively.

Mathcad programs make it easy to do tasks that may be impossible or very inconvenient to do in any other way.

This chapter contains the following sections:

Defining a program
How to create simple programs. Local assignment statements.

Conditional statements
Using a condition to suppress execution of a statement.

Looping
Using while loops and for loops to control iteration.

Controlling program execution
Using the break, continue, and return statements to modify the execution of a loop or an entire program.

Error handling
Using the on error statement to trap errors and the error string function to issue error tips.

Programs within programs
Using subroutines and recursion in a Mathcad program.

Evaluating programs symbolically
Using a Mathcad program to generate a symbolic expression.

Programming examples
A sampling of programs displaying some of the power of Mathcad’s approach to programming.

Defining a program

A Mathcad program is a special kind of Mathcad expression you can create in Mathcad Professional. Like any expression, a program returns a value-a scalar, vector, array, nested array, or string-when followed by the equal sign. In fact, as described in “Evaluating programs symbolically” on page 416, you can evaluate some Mathcad programs symbolically. And just as you can define a variable or function in terms of an expression, you can also define it in terms of a program.

The main difference between a program and an expression is the way you tell Mathcad how to compute an answer. When you use an expression, you have to describe how to compute the answer in one statement. But when you use a program, you can use as many statements as you want to describe how to compute the answer. You can think of a program as a “compound expression.” The following example shows how to make a simple program to define the function:

Defining a program

Defining a program

Although the example chosen is so simple as to render programming unnecessary, it does illustrate how to separate the statements making up the program and how to use the local assignment operator, “f-“.

• Type the left side of the function definition, followed by a”: =”. Make sure the place holder is selected.

Defining a program

Defining a program

• Open the Programming Palette by clicking on the appropriate button in the Math Palette. Then click on the “Add Line” button. Alternatively press] .

Defining a program

Defining a program

• The remaining placeholder is the actual value to be returned by the program. Type log (z).

Defining a program

Defining a program

You can now use this function just as you would any other function. Figure 18-1 shows this function along with an equivalent function defined on one line instead of two. Note that z is undefined everywhere outside the program. The definition of z inside the program is local to the program. It has no effect anywhere else.

A program can have any number of statements. To add a statement, click the “Add Line” button on the toolbar again. Mathcad inserts a placeholder below whatever statement you’ve selected. To delete the placeholder, click on it and backspace over it

A function defined both in terms of a program and in terms of

A function defined both in terms of a program and in terms of

Figure 18-2 shows a more complex example involving the quadratic formula. Although you can define the quadratic formula with a single statement as shown in the top half of the figure, you may find it simpler to define it with a series of simple statements as shown in the bottom half. This lets you avoid having to edit very complicated expressions.

more complexfunction defined in terms of both an expression

more complexfunction defined in terms of both an expression

A Mathcad program, therefore, i an expression made up of a sequence of statements, each one of which is an expression in itself. Like any expression, a Mathcad programmust have a value, This value is simply the value of the expression forming the last statement executed by the program. It could be a string expression or a single number, or it could be an array of numbers ( ee Figure 18-7, for example). It could even be a mixture of types, as described in the section “Nested arrays” in Chapter 10.

The remaining sections describe how to use conditional statements, how to use various looping structures to control program flow, how to handle errors, how to evaluate programs symbolically, and how to build more complicated programs.

Posted on November 24, 2015 in Programming

Share the Story

Back to Top