cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Creating A Vector of Functions - Possible?

aguter
1-Newbie

Creating A Vector of Functions - Possible?

I am somewhat new to Mathcad and I was wondering if this was possible to do.

I basically want to create a vector with stored functions.

See the attached mathcad v15 file with comments.

Thanks in advance for your comments!

12 REPLIES 12
Fred_Kohlhepp
23-Emerald I
(To:aguter)

You're close!

MJG
18-Opal
18-Opal
(To:aguter)

Fred's solution is obviously more concise.  However, to use an approach more similar to what you had attempted:

aguter
1-Newbie
(To:MJG)

Thanks Mark.

Still kinda new to the programming of MathCAD.

Within the for loop, is the name "result" meaningless unless used in the for loop again?

Also would you mind explaining what is wrong in my syntax/what MathCAD is interpreting my input as in the file I uploaded?

MJG
18-Opal
18-Opal
(To:aguter)

Austin,

Yes, the variable "Result" could be anything you want.  The main difference is that it's a variable, and not a function.  To make the program even more similar to what you had proposed, you could use the variable "PLF" and just get rid of the "(x)" in the two "if" lines.  However, the other difference is that the last line of your program must be the entirety of what you want the result to be.  If you left out the last line of my code, PLF(x) would just return a single value: Result[LastRow.

MJG
18-Opal
18-Opal
(To:MJG)

Hmm.. I'm wrong about the last line of the code.  however, I'm not sure why.  Anyone care to explain why this works?

The last line executed is Result[4, so shouldn't PLF(x) be equal to Result[4?

Fred_Kohlhepp
23-Emerald I
(To:MJG)

I believe that since you're executing a loop (for . . .), the program doesn't exit until the loop is finished and Result is complete.  I'm kind of surprised it returns anything, but Result is the last variable addressed.

MJG
18-Opal
18-Opal
(To:MJG)

I was playing around with this and found that the "Local assignment result" option affects the results:

My default is set to MC11, and this provides the results I expected:

Austin's worksheet was set to MC12.  This results in:

Mathcad's "Help" says the following about this option:

In Mathcad 11, 13, 14, and 15, local assignments return the value of the right-hand-side. In Mathcad 12, the left-hand-side is returned.

To me, this does not explain the observed results.  Can anyone provide a better explanation?

MJG
18-Opal
18-Opal
(To:MJG)

This tutorial is linked in the Help file.  I can't say I understand any better, but it seems to be directly addressing the topic at hand.

To me, the difference is not the left-hand vs. right-hand side.  The difference is evaluating x vs x[i.

RichardJ
19-Tanzanite
(To:MJG)

The difference is rhs vs lhs. The lhs is x, the rhs is i. The last value of i is 3. The last value of x is a 4 element vector.

MJG
18-Opal
18-Opal
(To:RichardJ)

But isn't the LHS x[i ?

I guess I understand what is meant, I just don't think LHS/RHS is the best way to describe it.

RichardJ
19-Tanzanite
(To:MJG)

The program returns the value of the last variable referenced in the program. That's why it's good practice to have a final line with that variable on it. You know what it will return. In your example there is no variable x[i. The variable is x, and i just indexes into that variable. So a better description of what is returned would be "In Mathcad 12 local assignments returned the value of the variable on the left-hand side of the expression. Mathcad 13 and higher return the value of the variable on the right-hand side by default".

Fred_Kohlhepp
23-Emerald I
(To:aguter)

Your program is defining a function "PLF(x)" not a variable.  I believe you could use that as a function inside the program, but it will not pass the result back from the program.  The program returns a numeric result, so when Mark directed the same code to "Result," you got what you wanted.

Please note that you don't need to step thru the vector one parameter at a time; you can specify the entire vector with one statement.

Top Tags