Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

** Community Tip** - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts!
X

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Oct 19, 2021
05:09 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 19, 2021
05:09 PM

Piecewise functions & input vector definition

Relatively new user of MathCAD here. I am trying to define a series of piecewise functions using if functions, then plot them by evaluating the function over a vector with a simple programming loop. When I define the input vector (i.e. the x points) using elipses like so:

x:= 0 ft, 0.05ft .. 9.25 ft

, I get an error. When I manually define an input vector ("Matrix/Tables" tab, "Insert Matrix") it works. Two questions:

1) Why is this? What is different between the two definitions of the input x vector?

2) Is there a more efficient way to define the input vector than manually defining a matrix? Is there a more efficient way to be doing this in general?

Okay, so I lied about only 2 questions. My worksheet is attached.

Thanks,

- Kevin

Labels:

7 REPLIES 7

Oct 19, 2021
05:38 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 19, 2021
05:38 PM

@KA_9974117 wrote:

Relatively new user of MathCAD here. I am trying to define a series of piecewise functions using if functions, then plot them by evaluating the function over a vector with a simple programming loop. When I define the input vector (i.e. the x points) using elipses like so:

x:= 0 ft, 0.05ft .. 9.25 ft

, I get an error. When I manually define an input vector ("Matrix/Tables" tab, "Insert Matrix") it works. Two questions:

1) Why is this? What is different between the two definitions of the input x vector?

2) Is there a more efficient way to define the input vector than manually defining a matrix? Is there a more efficient way to be doing this in general?

Okay, so I lied about only 2 questions. My worksheet is attached

Hi Kevin,

It's important in Mathcad usage to distinguish between a vector (which is an array) and a range variable (or, to be more precise, a range).

The one thing that a range variable is *not* is a vector. A vector is a list of values, whereas a range variable is more like a "promise" of values.

A range variable is a variable that holds a range specification. A range specification is a data type in its own right. It contains 2 or 3 pieces of information: the starting value of the range, the (optional) second value in the range, and the end value. The values can only be numeric. The first value is normally less than the end value, but but may be greater than it. If the second value is missing, Mathcad assumes a unit step, whose sign is end value minus first value.

It is normal to assign a range to a variable, which is then known as a range variable. When Mathcad encounters a range variable, it iterates through the values implied by the sequence. Think of a range variable as holding the information you'd normally put in a for loop, rather than as the values themselves.

This helps explain why you get an error when you try to take the length of range variable x - a range has no length, only a vector has length.

TLDR: A range variable is *not* a vector and you can't treat it like one.

Stuart

Oct 19, 2021
05:49 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 19, 2021
05:49 PM

Forgot to add,

2) Is there a more efficient way to define the input vector than manually defining a matrix? Is there a more efficient way to be doing this in general?

Efficiency is often in the eye of the beholder. People tend to do what they find cognitively easiest.

However, I generally write a function that generates test data.

Others prefer to go the range variable route, using range variables in their array indexing role.

The first half of the image below gives one method of generating data using range varaible.

If you've already defined your range variable, then it should be possible to use it directly in a for loop. I'm not sure if the program below will work. I think it should, but I've only got Prime Express. The Express variant of Mathcad has no programming functionality, so I can't check.

Stuart

Oct 20, 2021
12:53 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 20, 2021
12:53 AM

Put the xplot:=xplot= after the range definition. "=" is required. then sheet can do all.

Oct 20, 2021
05:26 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 20, 2021
05:26 AM

You can also use the following routine to move from a ranged variable to a vector:

Oct 20, 2021
07:46 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 20, 2021
07:46 AM

All that said, there's one more thing you need to be aware of. Prime has this ~~miraculous~~ ** dangerous** (and undocumented) feature that if you evaluate a range in its definition, then it becomes a vector.

See below:

r is defined as a range, it's not possible to index it (You cannot address an individual element of a range).

v is defined as a range, but immediately evaluated, that makes v a vector. In his post, ttokoro did essentially the same, but with a previously defined range. It is shown here, to the far right also.

It's very important that you (learn to) understand the difference between a range and a vector, because there are certain things that one can do, and the other not.

You cannot use a vector to index into a vector. But a range can index into a vector, it's one of the means to construct a vector. On the other hand, you cannot index into a range, as was shown above already.

Success!

Luc

Oct 20, 2021
09:33 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 20, 2021
09:33 AM

@LucMeekes wrote:

All that said, there's one more thing you need to be aware of. Prime has this miraculous feature that if you evaluate a range in its definition, then it becomes a vector.

I use this method myself, too, but one has to be aware that its an undocumented feature and so may be subject to change without notice in a future version.

Oct 20, 2021
10:37 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator

Oct 20, 2021
10:37 AM

@LucMeekes wrote:

All that said, there's one more thing you need to be aware of. Prime has this

~~miraculous~~feature that if you evaluate a range in its definition, then it becomes a vector.dangerous

Fixed it for you, Luc! 😈

As I've commented before, I tend to avoid putting evaluation operators at the end of definitions, precisely because of such side effects. I don't think that it's entirely clear from the rhs of the evaluation what the definition will actually assign to the variable.

PTC would have been better off implementing a *vec* function that converts any argument to a vector.

See:

https://community.ptc.com/t5/PTC-Mathcad/The-vec-function/m-p/170273

Stuart