Mathcad Programming
Examples and Important Facts
Introduction
Examples of Mathcad Programs:
If Operator Examples
For Loop Examples
While Loop Examples
Matrix Manipulations
Important Facts about Mathcad Programming
Introduction
A Mathcad program is essentially a multistep function. Programs can be very handy to make complex functions easy to write, and easier for others to comprehend.

In Mathcad, a program is made up of a sequence of statements using special operators to control the flow of evaluation. Thus, programs make it easy to do tasks that may be inconvenient to do any other way. Construct your program, then Mathcad will calculate through the sequence of statements and return the result of the last step as its answer. It can also return a value specified by the "return" statement.

Programs can be used to return every data type that you work with in Mathcad: scalars, arrays, and even text strings.

Mathcad program is created by using its program operators. Mathcad has 10 operators which are accessible from Programming toolbar:
All the programming operators can only be entered from the palette or by using the keyboard shortcuts. You cannot manually type the programming keywords.
"Add Line":
Operator to initiate a program or to add a line to a program. When you click on it, a vertical toolbar and 2 placeholders will be produced. The Add Line declares a program, it also declares multiple line expressions within other program building blocks.
Local assignment operator. Use the local assignment operator to assign a value to a variable. This is similar to the colon equal sign used in a worksheet; however, a local assignment operator is recognized only inside of the program. All assignments in a program must be made using this local assignment operator.
Conditional operator which can be used whenever you want a program statement to be executed only upon the occurrence of some condition.

In the right placeholder, enter a Boolean expression.

In the left placeholder, enter a statement to evaluate if the Boolean expression is true.

If necessary, use the "Add Line" operator to insert placeholders for additional statements.

See If Operator Examples.
The otherwise operator only works with the if operator; when the condition of the if is not met, Mathcad will execute the accompanying otherwise statement. If you use more than one if statement before an otherwise statement, the otherwise statement is executed only when all previous conditions are false.

Enter the otherwise statement in the left placeholder.
Use a for loop when you know exactly how many times you want the body of the loop to execute.

Type the iterative variable in the placeholder on the left, and the range of values on the right. Place all calculations to be contained in the loop immediately under the "for" so that they are indented.

If necessary, use the the "Add Line" operator to insert placeholders for additional statements.

See For Loop Examples.
Use a while loop whenever you want a set of statements to keep executing while a certain condition is true. Make sure you have a statement somewhere that at some point will make the condition false; otherwise, the loop will execute indefinitely and you will need to stop it by pressing [Esc].

In the placeholder to the right of the "while," type a Boolean expression.
In the placeholder below the "while," enter the statement you want to execute repeatedly.

If necessary, use the "Add Line" to insert placeholders for additional statements.

See While Loop Examples.
By default, a program returns the result of the last executed step. However, you can return a specific value from anywhere in the program by using the return operator. The return statement halts program execution and returns a specified value.

In the placeholder to the right of the "return," type whatever you would like to return.
To learn more about Mathcad programming see the following tutorials:
Introduction to Programming in Mathcad
Boolean Operators
Adding Lines in a Program
Interrupting Calculations

^ Top
Examples of Mathcad Programs

1.
This
is a simple Mathcad program to calculated the area of the circle:
assign the result to the variable area
return the variable area
and those are examples how the above program could be used:
or
If Operator Examples
2.
This is a Mathcad program which defines a function
Status. Status(temp) returns one of three possible text strings depending on the value of the variable temp:
assign "cold" to variable m
if temp < 50
and those are examples how the above program could be used:
or
See Vectorize Operator for help on applying functions to vectors.
3.
Here is a simple function:
A Mathcad program can be used to create piecewise functions.

g(x) is f(x) when f(x) > 0 and 0 otherwise:
4.
h(x) is x2 - 1 when x is greater or equal to 0 and -(x2 - 1) otherwise:
5.
k(x) is f(x) when -1 < x < 1, and -f(x) otherwise:
6.
l(x) is x2 - 1 when x > 1 or x < -1, and -(x2 - 1) otherwise:
7.
m(x) is x2 - 1 when x < -1, is -(x2 - 1) when x > 1, and 0 otherwise:

^ Top
For Loop Examples
8.
This is a Mathcad program which defines a function OddSum. The function OddSum(n) uses a for loop to compute the following sum, where n is an odd number:
1 + 3 + 5 + ... + n
assign 0 to the variable sum
<-- this instruction will be executed multiple times;
one time for each value of the range variable i
and this is how the above function could be used:
or
9.
This is a Mathcad program which defines a function SumEvenf. The function SumEvenf(k) uses a for loop to compute the following sum, where k is an even number:
... + ... +
and this is how the above function could be used:
10.
This is a Mathcad program which uses a for loop to compute the average of the odd numbers from p to q, where p an q are odd numbers such that
p < q. For example,

if p = 5 and q = 13, then the program computes the following expression:
use variable counter to count how
many numbers were added
and this is how the above function could be used:
or

^ Top
While Loop Examples
11.
This is a Mathcad program which defines a function
MySum. MySum(start,stop) uses a while loop to compute the sum of the integer values from start to stop:

start + (start +1) + (start + 2) + (start + 3) + ... + stop
and this is an example how the above program could be used:
12.
This is a Mathcad program which defines a function
SumOddf. SumOddf(k) computes the following sum, where k is an odd number:
... + ... +
and this is an example how the above program could be used:
13.
This is a Mathcad program which defines a function SumEvenf using a while loop. SumEvenf(k) computes the following sum, where k is an even number:
... + ... +
and this is how the above function could be used:

^ Top
Matrix Manipulation Examples
14.
This Mathcad program defines a function MyVector. MyVector(n) creates a vector of size n, in which the k-th element (k = 0, 1, 2, ..., n-1) is equal to:
The program uses a for loop. The value of the variable ORIGIN has been set to 0.
and this is how the above function could be used:
15.
This Mathcad program defines a function MyMatrix. MyMatrix(n,m) creates a two-dimensional matrix of size n x m, in which an element in the i-th row and k-th column (i = 0, 2, ..., n-1, and k = 0, 2, .., m-1) is equal to:
The program uses two for loops. The second loop is nested within the first one.
The value of the variable
ORIGIN has been set to 0.
and this is how the above function could be used:
16.
This Mathcad program defines a function VectorMaxPosition. VectorMaxPosition(V) finds the position of the largest element of a vertical vector V. The value of the variable ORIGIN has been set to 1. The function uses a for loop to compare each vector element, one by one, with the largest element (max) found so far.
compute the number of elements
in the vertical vector V
assume at first that the first
element is the largest (position = 1)
and this is how the above function could be used:
17.
This Mathcad program defines a function MatrixMax. MatrixMax(A, n, m) finds the maximum element in a two dimensional matrix A of size n x m. The value of the variable ORIGIN has been set to 1. The function uses two for loops to compare each matrix element, one by one, with the largest element (max) found so far. The second for loop is nested within the first one.
assume at first that the first
element is the largest
and this is how the above function could be used:
18.
This Mathcad program defines a function CreateXValues. CreateXValues(a,b,n) creates a vector x of n+1 elements, given an interval [a,b], and the number of subintervals n. It requires that the variable ORIGIN is set to 0. The values created (the elements of the vector x) are evenly spaced in the interval [a,b], such as:
The number of the elements of the vector x is n+1.
h is the with of one subinterval, the increment.
The generated elements of the vector x are the following:

x
0, x1, x2, ..., xn

The x values are evenly spaced within the interval [a,b]; x0 is equal to a, and the last element (xn) is equal to b.
compute the increment h
create n+1 elements of vector x, one by one
and this is how the above function could be used:
19.
This Mathcad program defines a function Tabulatef. Tabulatef(f,a,b,n) creates a matrix P of 2 columns and n+1 rows, given f(x), an interval [a,b], and the number of subintervals n. It requires that the variable ORIGIN is set to 0. The program creates and returns a matrix P such that the first column of P contains n+1 x values, and the second column of P contains the corresponding f(x) values. The x values are evenly spaced in the interval [a,b], such as:
The number of x values created is n+1.
h is the with of one subinterval, the increment.
The x values generated are the following:

x
0, x1, x2, ..., xn

The x values are evenly spaced within the interval [a,b]; x0 is equal to a, and the last x value (xn) is equal to b.
The Tabulatef function uses a Mathcad program to create the x and y values. It also uses the augment function. The augment function connects matrices side by side.
compute the increment h
create n+1 evenly spaced x values
create n+1 corresponding y values using
the function f
connect vectors x and y
to create one matrix P
This is how the function Tabulatef could be used:

^ Top
Important Facts about Mathcad Programming
The left arrow on the Programming toolbar is the local assignment operator. This is similar to the colon equal sign used in a worksheet; however, a local assignment operator is recognized only inside the program region.

All assignments in a program must be made using this local assignment operator. The colon equal is not recognized when used within a program.


The Add Line and local assignment operators provide the basic building blocks for all programs. The
Add Line declares a program, it also declares multiple line expressions within other program building blocks.

The
local variables (variables defined within a program region) will only exist in the program. The rest of the worksheet will not know that a local variable has been created. All local variables disappear once program stops running.

Mathcad program cannot modify any variable in a worksheet.


All the data required by the program can be passed to it via the
parameter list. While you are defining the program, the values in the parameter list are dummy variables. They are simply there to indicate how the various parameter values should be manipulated by the program. We do not need to assign any initial values to those parameters before we use them to define a program.

By default, the last value assigned in a program is the program's
return value. You can use a return statement to override the default and specify a different value to be returned by the program.

You can
return multiple values from a program by returning them as an array. This is the only way to return multiple values from a Mathcad program.

The
if, otherwise, for, while, break, continue, return, and on error programming statements have to be entered either by clicking on an icon from the Programming toolbar or by typing a keyboard shortcut key. You cannot manually type the operator name in your program.

If you use more than one if statement before an otherwise statement, the otherwise statement is executed only when all if statements immediately preceding it are false.

One danger of while loops is the
infinite loop. It is quite possible to define a loop where the condition is never met. In this case, the loop will calculate forever. In order to interrupt a calculation, hit the ESC key.

The
break statement is used to halt execution of a for or while loop.

The
return statement halts program execution and returns a specified value. It is often used in conjunction with an if statement such that the program terminates when a particular condition is met. It is important to note that the break and return statements behave very similarly. The only difference is that return allows you to return a particular value whereas break always returns the last value computed by default.

Mathcad program cannot change any value on the worksheet, it can only return a result.



^ Top