Blogs Good, Better, Best Mathcad Use & Solving Iterative Problems

Good, Better, Best Mathcad Use & Solving Iterative Problems

August 4, 2022 Download Mathcad for Free

Brent Maxfield is from Salt Lake City, Utah. This is an ideal location for him because of his love for outdoor activities. He loves hiking and skiing in the nearby mountains, and also loves to explore the red rock canyons and deserts found in Southern Utah.

Brent Maxfield is a registered Professional Structural Engineer in the State of Utah. He graduated Magna Cum Laude from Brigham Young University with a degree in Civil Engineering and earned a Master of Engineering Management degree from BYU. He has been a practicing structural engineer for 36 years.

He was awarded the 2012 Utah Engineer of the Year by the Utah Engineers Council. He is active in professional associations having served on the Board of Directors of the Structural Engineers Association of Utah and the EERI Utah Chapter. He has also served on the Structural Advisory Committee to the Utah Uniform Building Codes Commission.

He has used PTC Mathcad extensively for 20 years. He is the author of “Essential PTC® Mathcad Prime® 3.0: A Guide for New and Current Users”, available on Amazon.com.

See All From This Author

Editor’s Note: This blog article was originally written as two PTC Mathcad Prime worksheets. Download both of the Mathcad Prime 8 worksheets here.

The Problems

My sons and I like to explore the western deserts of Utah. We often come across deep vertical mine shafts that have been covered by a gridwork of bars that allow you to walk out directly over the shaft. We like to drop a rock and time how long it takes for the rock to hit the bottom of the shaft. We use the physics equation, d = ½a*t² , to calculate the depth of the shaft. Because we are in the desert without a computer, we neglect the time it takes for the sound to get from the bottom of the shaft up to our ears, and we neglect the air resistance on the rock.

We know that the acceleration of gravity is 32.174 ft/s². Once we know the time, we can calculate the depth of the shaft. This blog will look at three different ways to solve this simple problem to look at the power of PTC Mathcad Prime: good, better, and best.

Then, the blog will look at different ways to use Mathcad Prime’s powerful solving features to directly calculate the depth of the shaft from the measured time. If you are standing at the surface recording the time, you must subtract the time it takes for the sound to get from the bottom of the shaft up to your ears. But, this is a function of the depth of the shaft, which you don’t know. That’s what makes it iterative.

Problem 1 (Simple):
Calculate the depth of the shaft if it takes 5.5 seconds to hear the rock hit the bottom of the shaft.

Problem 2 (Iterative):
Write an equation for how long it takes the sound to get to your ears as a function of depth. Calculate the time it takes for the rock to hit the bottom of the shaft, and then calculate how long it takes for the sound to get from the bottom of the shaft up to your ears.

Solving Problem 1

Problem 1: Mathcad as a Calculator (Good)

We will first use Mathcad as a calculator. Since this equation involves units, we will also attach units. Mathcad also has a built-in variable (g = 32.174 ft/s²) so we will use that as well.

mathcad evaluation operator calculator gravity speed length 486.632 ft

Problem 1: Assigning Variables in Mathcad (Better)

Next, let's assign variables to distance and time.

assign variables mathcad time = 5.5 seconds

Since Mathcad is unit aware, we can also display the distance in meters.

Now that we have the time assigned to a variable, we can change the variable and recalculate the distance.

assign variables mathcad time = 3.2 seconds redefinition warning

Note the green boxes around "t" and "Distance". Beginning with Mathcad Prime 8, Mathcad alerts you when you redefine a variable, as we just did in this example.

Problem 1: Creating User-Defined Functions with Mathcad (Best)

Finally, we will create a function for this problem. We will call the function "Depth" with a variable of time (t). The function is very simple to create. Simply define the variable and then list the inputs to the function in parenthesis, and then tell Mathcad the equation using the input variables.

user defined function depth mathcad prime

This function is "Depth" as a function of time. Because the function uses the input "t", it uses the value of "t" when it sees it in the equation; it does not use the defined value of "t" defined above.

Now that we have the function created, all we need to do is type the equation and then put the value of t in parenthesis. Remember to attach the units of seconds to the input.

This allows you to quickly test several variables of time.

depth at 5.5 3.2 8.0 seconds mathcad prime

You can also assign a variable to the results of the function.

assigning variables to different values mathcad prime

 

This example was rather simple, but hopefully it helps illustrate the ease and power of Mathcad, especially the power and ease of user-defined functions. So far, this blog shows how easy it is to use Mathcad as a simple calculator (good) and how to assign variables to equations (better), but the real power of Mathcad is illustrated by the use of user-defined functions (best).

Try creating a few functions for your most often used equations.

Solving Problem 2

Solving Problem 2 with Manual Iteration

Let's now make the problem a bit more complicated by considering how long it takes for the sound to come up from the bottom of the shaft to your ears.

The velocity of sound is about 1,125 ft/s (343 m/s) at 68 degrees F (20 C), so let's use this value.

We now have a problem of not knowing how deep the hole is, so we cannot calculate how long it takes for sound to get up to your ears. This is an iterative problem.

In order to do this, we need to subtract the time it takes for the sound to come up the shaft. We can do this in the function's definition of time.

manually iterating a solution in mathcad prime for how deep a hole is

Here, Brent compares the input depth versus the calculated depth across multiple guess values, changing the depth after looking at the results. This is for the time being 5 seconds, 3.2 seconds, and 8 seconds.

Note how the calculated depth of the shaft changed, once the time it takes for the sound to come up the shaft is considered, especially for the deeper shafts.

Introducing the Solve Keyword

Part 1: Calculate the time it takes for the rock to hit the bottom of the shaft.

We could solve for time by simple algebra, but I want to illustrate the symbolic solve keyword, which will be discussed in more depth in a future blog. (On the Math tab, in the Operators group, select Symbolics and select the solve keyword.) Type a "t" to solve for time.

displacement distance equation solve for time quadratic formula mathcad

Because this is a quadratic equation, we get two solutions, one of which results in negative time.

 

Part 2: Calculate the time it takes for the sound to get to the surface.

The time it takes for sound to get to the surface is the depth of the shaft divided by the velocity of sound. As in the previous example, we will use a velocity of sound of about 1125 ft/s (343 m/s) at 68 degrees F (20 degrees C).

velocity of sound in the air

With the symbolic solution for time in hand, let's look at two ways to solve the rest of the iterative problem.

Using a Solve Block

The first solution uses a solve block. Add a solve block from the Math tab, Regions group. There are three parts to the solve block. The first is a guess value. The second is a constraint section, and the third section is the solver section using the find function (additional functions available for use in the solver section are listed under the Functions/Solving category).

Input the measured time. (This is the time it takes for the rock to hit the bottom plus the time it takes for sound to get to the surface.)

A solve block iterates until it finds a solution for the requested variable(s) that satisfies the constraints.

Let's set Measured Time to 5.5 seconds. MeasuredTime:= 5.5s

mathcad prime solve block solving for depth measured time 5.5 seconds guess values constraints solver

Guess Values: This value of d is only a guess value to start the iteration.
Constraints: Constraints are the relationships between variables. In this case, it is the relationship
between measured time and depth.
For a solve block use ctrl + = to set the relationship
Solver: The find function solves for the variable in parenthesis.

The depth is equal to 422.451 feet, or 128.763 meters. This solution is consistent with the manual iteration from the previous section.

Now let's set Measured Time to 8 seconds. MeasuredTime:= 8s
When we do this, the solve block find function calculates the depth to 845.261 feet, or 257.636 meters. This solution is also consistent with the manual iteration from the previous section.

Creating a Function Using the Solve Keyword

The second method to solve this problem is to create a function that will provide a direct solution using the solve keyword. The solve keyword is part of Mathcad's symbolic engine.
Note: In this example I have customized the appearance of function names. This is an easy two-step process:

  1. On the Math Formatting tab in the "Label Styles" section, I selected Function from the dropdown choices to set my visual preferences for function names. I changed the color and applied italics and underlining.
  2. I put my cursor on the function name (Depth or ShaftDepth), and from the Math tab in the "Style" section, I selected "Function" from the Labels dropdown choices and applied that label style to the function name.
depth function using mathcad symbolic solve keyword

Test the function for a measured time of 8 seconds.

mathcad prime depth function gives two solutions

Note that there are two solutions. The larger value of depth is from a negative value of time and can be ignored.

To avoid seeing both values, modify the function to select the minimum value of depth by using the min function. Note that this calls for a new user-defined function definition, ShaftDepth, which incorporates the previous user-defined function definition, Depth.

shaft depth of time is the minimum value of depth

 

Now that you have created a function to calculate the depth of the shaft, you can simply enter the measured time and get a direct solution of shaft depth. Remember to assign the "Function" label and be sure to assign the units of seconds to the input.

shaft depth function at 3.2 5.5 and 8 seconds in mathcad prime

This blog illustrates the power of creating user-defined functions. In this blog, we combined the power of Mathcad's powerful solving features, with the power of user-defined functions to allow a direct solution to an iterative problem.

Up Next

Download Mathcad Prime

Install the full Mathcad Prime for the power to easily solve your own iterative problems. Download Now
Brent Maxfield

Brent Maxfield is from Salt Lake City, Utah. This is an ideal location for him because of his love for outdoor activities. He loves hiking and skiing in the nearby mountains, and also loves to explore the red rock canyons and deserts found in Southern Utah.

Brent Maxfield is a registered Professional Structural Engineer in the State of Utah. He graduated Magna Cum Laude from Brigham Young University with a degree in Civil Engineering and earned a Master of Engineering Management degree from BYU. He has been a practicing structural engineer for 36 years.

He was awarded the 2012 Utah Engineer of the Year by the Utah Engineers Council. He is active in professional associations having served on the Board of Directors of the Structural Engineers Association of Utah and the EERI Utah Chapter. He has also served on the Structural Advisory Committee to the Utah Uniform Building Codes Commission.

He has used PTC Mathcad extensively for 20 years. He is the author of “Essential PTC® Mathcad Prime® 3.0: A Guide for New and Current Users”, available on Amazon.com.

Continue Reading