Programming With Mathcad Prime
Programming With Mathcad Prime
Programming With Mathcad Prime
All rights reserved under copyright laws of the United States and other countries.
PTC, the PTC Logo, Mathcad and all PTC product names and logos are trademarks or registered
trademarks of PTC and/or its subsidiaries in the United States and in other countries.
Conditions of Use
Copying and use of these materials for educational purposes is fully authorized for any person(s).
Acknowledgments
The authors gratefully acknowledge the support of the Mathcad Business Unit and the PTC Academic
Program.
Questions or Corrections
Please direct inquiries or questions regarding the contents of this tutorial to the Mathcad Academic
Program at MathcadEducation@ptc.com. Suggestions for improvement or further development will be
gladly accepted.
Programming with Mathcad Prime
Mathcad Prime is an environment for Computational Thinking – an approach to calculation, data
analysis and problem solving that uses the capabilities of a computer to construct better solutions. When
used appropriately computational thinking:
Mathcad Prime possesses a simple and easy to use Programming capability that allows users to write
multistep functions directly on a worksheet alongside equations, tables, graphs, and text.
Using Mathcad’s programming capabilities in math and science coursework can help students understand
concepts more deeply. It also helps introduces them to some computer programming basics.
In this tutorial we will focus on two different ways to use Mathcad’s Programming capabilities to Go
Beyond the Basics in math and science courses:
Each example will introduce a small number of Mathcad’s Programming capabilities in order to help you
to build up your proficiency in writing Functions on a Mathcad worksheet.
In a Mathcad worksheet it would be typical to use formulas to calculate the Area and Circumference of a
circle using assignment statements such as those shown below.
Writing a computer program on a Mathcad worksheet is a lot like defining and evaluating formulas like
those above. Once defined, the formulas above instruct Mathcad to perform all of the steps in the
definition and return a result. Similarly, Mathcad programs report the last result calculated or assigned
in the program. Returning this result is a lot like reporting the final result after following order of
operations in a numerical expression.
We can use Mathcad’s Programming tools to create the formulas above as Mathcad programs. Although
we do not need to write programs to perform these calculations, we will use this simple example to
introduce the Local Assignment () operator and the Program Structure (|) symbol on Mathcad’s
Programming Toolbar.
In the box below, a simple one-line Mathcad program is used to calculate the circumference of a circle
using the measure of a diameter. This example demonstrates how the Local Definition operator works in a
Mathcad worksheet.
Follow these steps to create a Mathcad program that calculates the area of a circle given its
Diameter as an input parameter:
Step 1: Type the following keystrokes to give the program a name and a parameter:
Step 2: Mathcad shows that it is waiting for the program to be defined on the right side of the assignment
statement by leaving the blue cursor in the placeholder. Follow these steps to insert the first line of the
program:
Type: D/2 in the placeholder. Hit the Right Arrow key once to
advance the cursor and then hit the ENTER key. A new Program
line will appear as shown.
Left-Click on the symbol for π to insert the value for this constant
Type *radius^2 to finish the definition of the formula for Area and then hit the ENTER key
Left-Click outside of the Math region to complete the definition of the program
A(2m=
The presentation of A in this tutorial highlights the ways that Mathcad’s programming capabilities allow
for both clear communication and efficient notation. In this program the use of local variables for radius
Task: Use Mathcad’s Programming capabilities to create a function called HeroF that takes the lengths of
the 3 sides of a triangle as input parameters as shown at left below. The program should return the
calculation of the area of the triangle as shown in the two examples at right.
In this part of the tutorial I will introduce the steps necessary to create the following function:
Averaginator is a function that follows the informal definition of a mathematical average in the above
paragraph. One goal of this part of this tutorial is to show how a program can be used to mimic a process
in a step-by-step manner.
Type Averaginator(Terms,List: to create the left side of the assignment statement for the program
Left-Click on the Math ribbon and then Left-Click on the Programming icon
Step 2: Define a local variable count to control the execution of the loop
Left-click on the Programming menu and then left-click on to insert a Local Assignment symbol
Type 0 and hit Enter to add a new line to the function definition
Left-click on the Programming menu and then left-click on to insert a Local Assignment symbol
Type 0 and hit Enter to add a new line to the function definition
Step 4: Create a for loop in the program to calculate the average of the terms in List
With the blue cursor in the placeholder on the third line Left-Click on the Programming icon
Left-Click on for in the Programming toolbar. Mathcad will insert a template for defining a for loop
Step 5: Complete the values in the template for the for loop
In the first placeholder type count, the name given to our local variable for controlling the loop
Step 7: Calculate the average (mean) as the sum divided by the number of terms
Use the right arrow key to move the blue cursor to the right until it extends to the full height of the for
loop template. You should only need to hit it twice. If necessary, you can place the cursor outside of the
for loop with your mouse.
Step 8: In the main body of the Mathcad worksheet define a variable containing a 5x1 matrix
Type DataList:
Left-Click on the Insert Matrix icon and drag select a 5x1 matrix
Beneath the definition of the Averaginator function and the DataList variable type:
Averaginator(5,DataList)=
Averaginator(n,Matrix Name)
Task Notes:
1. A while loop is a conditional loop. It is created in a similar way as you create a for loop. In the
IsFactor program above the while loop runs until product is greater than or equal to target.
2. Inside the while loop an if statement is used to determine if the product is currently equal to the
target value. You need to use the if command from the Mathcad Prime programming palette to
insert an if statement into a program.
3. The return command is a special command that ends the execution of the program and returns
the value given in the return statement. In this case we use return to return the value of the other
factor or the value 0 if the given term is not a factor of the target.
Task: Create a Mathcad program that determines if a given number is a factor of a target number. You
may recreate the program above for practice, or write your own program from scratch.
1. In this tutorial you have learned some basic Mathcad Programming capabilities. Mathcad makes
it easy to include computer programs in a worksheet alongside text, numerical calculations,
tables, and graphs.
2. The following skills were emphasized in this tutorial:
o Defining a Mathcad program to perform a mathematical calculation or process
o Using local variable assignment to create self-documenting programs
3. The following Mathcad Programming tools were introduced in a tutorial:
o Program Structure “|”
o Local Assignment “”
o For loop
4. The following Mathcad Programming tools were shown in a demonstration:
o While loop
o if statement
o return statement
5. To learn more about writing programs using Mathcad Prime see the Programming Tutorial that
is available from the Getting Started tab on the Mathcad Prime ribbon.