Written by Dave Martin
  • 4/4/2018

Programming in PTC Mathcad: Uncover Hidden Data Trends

$name

As an engineer, I use PTC Mathcad for two main purposes: integration with Creo Parametric, and data analysis. With regard to the former, my favorite part of PTC Mathcad is Solve Blocks for optimization. With regard to the latter, my favorite part is programming. If you’ve read my previous columns, you’ve seen that I make extensive use of programming to identify trends. In this post, we’ll dive deeper into what makes programming so powerful.

Loops to Process Spreadsheet Data

Often when I’m analyzing data, I start with spreadsheet data in CSV or Excel files. With PTC Mathcad’s for-loops and while-loops, I can quickly evaluate large data sets. The power of computers lies in iterating operations – so why not let PTC Mathcad take care of the heavy lifting and tedious tasks?

PTC Mathcad also has a try-on-error loop, so programs can continue running when errors occur. Here’s a program that uses both a for-loop and try-on-error to evaluate how many times each seed has appeared in the Final Four over the past 33 years:

Program to count seeds

Depicted graphically, the results look like:

Seeds graphed

I can quickly determine that seeds 12 through 16 have never made the Final Four, and 11 has made it more times than 9 or 10. Another simple program allows me to figure out the average seed is about 2.8:

Seed average program

Conditionals

PTC Mathcad has over 400 built-in functions, and I can define my own. But often, I want to evaluate a compound situation, or branch depending on the results. With PTC Mathcad’s conditional constructs, such as if-then-else, else-if, and also-if, programs can make decisions.

When analyzing the NCAA Final Four, I wanted to figure out how many times all #1 seeds made it to the Final Four. Or how many times it was all #1 or #2 seeds, or #3 and higher, or #4 and higher, and so on. Here’s the program:

Number 1 seeds program

Here are the results in table and plot form:

Table from Number 1 Seeds program

Plot from Number 1 Seeds program

All #1 seeds have made it to the Final Four only once. Almost half the time, all the Final Four seeds are #4 and higher, and three-fourths of the time, they are #7 or higher.

Debugging

I’ve taken software programming classes before, and while I’m not terrible at it, I’m not particularly good at it either. I’ll figure out an algorithm to tackle my program, write the code, and execute it… only to get either no results or outlandish ones.

PTC Mathcad has a live calculation engine, so it points out errors in my program as I’m writing it. By evaluating the program in real time, I can identify my mistakes immediately, and fix them.

For example, I was analyzing Final Four data, and wanted to generate a matrix that reported how many times each seed appeared in the Final Four for each region. I evaluated the results as I wrote the program:

Debugging in PTC Mathcad

I had the wrong number of columns and too many zeroes. This made me realize I had my matrix subscripts flipped. A couple quick edits later, my program was debugged.

Generating New Data

Rather than report a single value, when I’m analyzing data, I like to generate vectors and matrices that I can plot. Trends are easier to recognize when the data is represented pictorially. With programming in PTC Mathcad, I can generate a matrix “on the fly.” I don’t have to “declare” the matrix or specify its rows or columns. Defining any element in a vector or matrix automatically creates a vector or matrix of the appropriate size.

I refined the previous program to calculate the number and percentage of each seed for each region, and generate a matrix of the results:

Refined program

Here are the results:

Results matrix

That’s a little hard to interpret. But since I generated a matrix, I can plot the results:

Final plot

Here I can tell that the #1 seed has the best chance of making it to the Final Four in all regions. In the West, if it’s not #1, then #2 has the best chance – but in the South, #4 is more likely to win the region than #2 or #3. In the East, the #7 seed dominates as a wildcard, but in the South, it’s #8.

Conclusions

Learning how to program in PTC Mathcad is surprisingly easy, and greatly expands your ability to harvest your data for insights and trends. The power of loops, conditionals, debugging, and generating matrices can transform and elevate your productivity with data analysis.

If you want to try programming in PTC Mathcad, download PTC Mathcad Express.  The software provides full PTC Mathcad functionality, including programming, for the first 30 days.

Download PTC Mathcad Express free

Tags:

About the Author

Dave Martin is a Creo, Windchill, and PTC Mathcad instructor and consultant. He is the author of the books “Top Down Design in Creo Parametric,” “Design Intent in Creo Parametric,” and “Configuring Creo Parametric,” all available at amazon.com. He can be reached at dmartin@creowindchill.com.

Dave currently works as the configuration manager for Elroy Air, which develops autonomous aerial vehicles for middle-mile delivery. Previous employers include Blue Origin, Amazon Prime Air, Amazon Lab126, and PTC. He holds a degree in Mechanical Engineering from MIT and is a former armor officer in the United States Army Reserves.