Programming in MathCAD is simply establishing a complex function as a series of simple functions and laying them out in sequence. MathCAD also allows for conditional statements, looping and error control.

But before we get into the development of a program, we must first understand flow charts. A flow chart is a tool used by programmers to estalish a visual representation of the program they are going to write. But more than that, it develops the logic flow. If writing a program without a flow chart, it is very easy to become confused. In fact, large programs are virtually impossible to write without one. Also, since a flowchart is very English-like, it provides a tool to which one can return and to review and understand how the original program was written. In fact, a flow chart is to a computer program as an as-built drawing is to a construction project.

But flow charts are not used only by programmers. They can be used by any person in any discipline to outline the flow of a process or project. For example, construction managers use a modified form of a flow chart to establish construction timelines. Factories use flowcharts to outline the manufacturing process in which they are involved. Chances are, you've used some form of flow chart in your life, even if it was nothing more than a 'honey-do' list.

An example of a flow chart outlining the process of ordering lunch at McDonald's is shown below.
Each symbol or shape of the flow chart has a certain meaning. Most of the primary ones we will use are shown to the left. However, there are a number of others. Click on the icon below to open an MS Word document outlining a number of those used in this course.
Take, for example, the quadratic equation. Although on can argue it is not necessary to 'program' this in MathCAD, it is in excellent example of programming. The quadratic equation solves a function of the form f(x)=ax2+bx+c with coefficients a, b and c.
In this case, if
, then
. The two roots of the quadratic are shown below.
However, instead of writing the quadratic equation twice to find each root, we can use MathCAD's programming capability to define the quadratic equation and give us the roots in the form of a vector.
<= Double Click to view flowchart
<-- Define the function roots(a,b,c) where roots is the name of the function and a, b and c are the coefficients of the quadratic.
<-- Write the numerator of the quadratic equation as two separate equations and assign to y & z.
<-- Assign denominator to variable d.
<-- Display the results in a vector form. Note the quotient is calculated within the vector.
| Note that local variables x, y, z and d have no
V value outside of the program
<-- Use the program as you would any function to solve a quadratic such as f(x)
<-- A check of the results
MathCAD programs also support conditional statements. For example, the Darcy-Weisbach equation defines the pressure drop for fluid flow in a conduit by the equation:
where: Dpfr is pressure drop in feet of fluid
L is conduit length in feet
Dh is hydraulic diameter in inches
pv is velocity pressure in feet of fluid
f is a friction factor (dimensionless)
When solving this equation, all values are easily found except for friction factor 'f'. The Colebrook equation for the friction factor states the friction factor is a function of itself as shown below:
<-- Note that since f is a function of itself, the solution becomes iterative. We will learn how to write an iterative solution at a later date.
where: e is relative roughness of the conduit in feet
Re is Reynolds number (Dimensionless)
Since this equation is unwieldy, a simplified form developed by Altshul in 1963, and modified by Tsal in 1989 approximates the friction factor as:
^^ Double Click to view flowchart
Let's write this approximation as a program
Assume the following data:
Find the friction factor.
MathCAD programs can also support looping and can display textual messages. For example, let's program the factorial function.
<= Double Click to view flowchart
<-- Initiate y
<-- Check that x is an integer
<-- Perform the calculation
<= Double Click to view flowchart
<-- Initiate dummy variables y & i
<-- Check that x is an integer
<-- Calculate factorial. Note the while test is performed on x+1 rather than x as with the for loop. This is because 'while' checks the condition at the start of the loop, 'for' checks the condition at the end of the loop.
The statements 'return', 'break' and 'continue' are used to control the execution a program. You saw the use of 'return' above. As shown, 'return' is typically used with an 'if' statement to display some value based on a condition. Although the above use was to return a text statement, it can be used to return a numeric value as well.

The 'break' command is used to terminate program execution usually based on some condition. The 'continue' statement also interrupts calculation based on a condition, but allows the program to continue with the next statement. This allows the program to skip the calculation of an undesirable condition (ie: If you know your calculation will divide by zero thus forcing an error and halting the program, you can use continue to interrupt processing and skip the value of zero, but resume with the next value.)
We will write a program to sum the values of 1/i for all integer values of i from 1 to x. The value of i may be positive or negative. Since 1/0 is undefined, we will try to control program execution using the break statement and the continue statement. We will also invoke a conditional to check that the value of i is an integer value.
<= Double Click to view flowchart
<-- Initialize y
<-- Check that x is an integer
<-- Return x+y if x=0
<-- A for loop to calculate the sum of the reciprocal of i from i=1 to x. The break statement halts execution when i=0
Program Check
<-- Noninteger values not allowed
<-- Program returns x+y for f(0)
<-- f(5) as evaluated by program. Since i cannot equal zero for i=1 to 5, program calculates positive integers correctly for any positive value (ie: note f(1000))
<-- However, when the argument of the function is negative, the function returns one instead of -1.283. Note this function will always return the value of one for any negative argument. This is because i is incremented from positive one, thus it must pass through zero. The break statement forces the program to break when this happens.
<= Double Click to view flowchart
<-- A rewrite of the program using the continue statement instead of the break statement. The continue statement forces the program to skip the calculation when i=0 but continues with the next value of i.
Program Check
<-- This program evaluates x=0, positive values of x and noninteger values of x correctly as did the program above.
<-- But notice that when the argument is a negative value, the continue statement forces the program to skip the evaluation of 1/0 when i=0, but continues the evaluation with 1/-1 when x=-1.
<= Double Click to view flowchart
<-- Same program written using a while loop. Note the program is somewhat more cumbersome than when using the for loop.
Program Check
The return statement discussed above, when coupled with a boolean test, performs simple error trapping. However, for more complex situations, MathCAD provides error trapping through the use of the 'on error' command and the error(S) function. Unlike the return command which is used to assign a string (message) to a variable for display, the error(S) function is used to define custom error messages that will appear in the error dialogue box when an error occurs.
on error
This program will attempt to find the root of a function using zero as the default guess.
<-- Define a simple function. Note this function produces an error when x = 0
<= Double Click to view flowchart
<-- Write a program that allows the value of 'a' to be used in the function directly as the variable gr. However, if a = 0, the function produces an error. In this case, I've defined gw to equal infinity and that is substituted to provide an answer of 0.
This program determines the number of combinations of k selected from n. The program will provide different error messages based on some condition.
<= Double Click to view flowchart
<-- Result of a valid calculation
<-- Click on each one in turn to see the error message as defined in the program.
MathCAD supports the use of subroutines. This is accomplished quite simply by referring to any number of secondary programs from the primary program. An extensive example of the use of subroutines is in the program file I:\course\met260\week08\ Psychrometrics is the study of moist air. Given the dry bulb temperature (as read on a thermometer), the elevation of the site and either relative humidity, dew point or wet bulb temperature, this program will calculate and display dry bulb temperature, site elevation, average barometric pressure, enthalpy, density, specific volume, humidity ratio, degree of saturation, relative humidity, dew point and wetbulb.

It is not important to understand the equations used to calculate these parameters, you will get this in a thermodynamics course. Instead, study the programming structure. This program is comprised of a rather simple main program which makes a call to one of three subroutines depending on the information provided. Each of these subroutines makes a call to a fourth subroutine to calculate a common parameter required to complete the moist air calculations.