Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
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!
Fred's solution is obviously more concise. However, to use an approach more similar to what you had attempted:
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?
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.
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?
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.
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?
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.
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.
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.
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".
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.