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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Generating multiple arrays from a program

dcasanova-disab
1-Newbie

Generating multiple arrays from a program

Hi, I'm trying to write a simple program that creates two arrays by calculating each element one by one, here's the pseudo-code:

FOR N = 1 TO 10
A(0) = 1
B(N) = A(N-1)
A(N) = 2*A(N-1) + B(N)
NEXT N

I tried to perform this example with a "for" statement and "local definitions" (<-- left arrow) from the programming toolbar without success.

The Mathcad document looks roughly like this:

for i E 1 .. 10
| 1 <-- A0
| Bi <-- Ai-1
| Ai <-- 2 Ai-1 + Bi

So, is it possible to program this loop within Mathcad?

Thanks.
19 REPLIES 19

Get inspired from Fobonacci.



jmG

"last" is 1469, the highest Fibonacci number that can be displayed quick.

jmG

On 10/19/2009 12:24:09 PM, dcasanova wrote:
Hi, I'm trying to write a simple program that creates two arrays by calculating each element one by one, here's the pseudo-code:

FOR N = 1 TO 10
A(0) = 1
B(N) = A(N-1)
A(N) = 2*A(N-1) + B(N)
NEXT N

I tried to perform this example with a "for" statement and "local definitions" (<-- left arrow) from the programming toolbar without success.

The Mathcad document looks roughly like this:

for i E 1 .. 10
| 1 <-- A0
| Bi <-- Ai-1
| Ai <-- 2 Ai-1 + Bi

So, is it possible to program this loop within Mathcad?


See attached. It's pretty much like your pseudo-code, but I've taken the initialization of A outside the loop and swapped the assignment order (you can't assign a variable to a number).

Stuart

... whereas you made nothing dependent,
make it simpler programmed/not-programmed.



jmG

You don't have to program.


Fred Kohlhepp
fkohlhepp@sikorsky.com

On 10/19/2009 3:29:55 PM, fkohlhepp wrote:
>You don't have to program.
>
>
>Fred Kohlhepp
>fkohlhepp@sikorsky.com

I just noticed that this worksheet won't work if I set the "Multiple assignment" option from the "Compatibility" tab of Mathcad to "MC12", can it be rewritten to the newer versions without losing functionality?

On 10/20/2009 3:44:01 PM, dcasanova wrote:
>On 10/19/2009 3:29:55 PM, fkohlhepp
>wrote:
>>You don't have to program.
>>
>>
>>Fred Kohlhepp
>>fkohlhepp@sikorsky.com
>
>I just noticed that this worksheet won't
>work if I set the "Multiple assignment"
>option from the "Compatibility" tab of
>Mathcad to "MC12", can it be rewritten
>to the newer versions without losing
>functionality?
>_________________________

Try what I just posted, as is [Fibonacci03.gif]

jmG



On 10/20/2009 4:54:21 PM, jmG wrote:
>Try what I just posted, as is
>[Fibonacci03.gif]
>
>jmG

I tried that example but it also depends on the "MC11" compatibility option, as soon as I switch it to "MC12" it stops working.

On 10/20/2009 5:01:03 PM, jmG wrote:
>If that one works, why to re-code ?
>Multiple array should do something
>useful.
>
>jmG

Because sequential assignments won't be supported in future versions, there's even a "Migration guide" that explains the following:

"Please rewrite worksheets that depend on these switches to a version-neutral construction, as backwards-compatible switches will be maintained only through the Mathcad 14 release."

"The Compatibility tab allows you to retain certain behaviors from previous versions of Mathcad until your worksheets have been migrated to version-neutral expressions. When Mathcad 11 or older worksheets are opened, the switches are set to mimic Mathcad 11 behavior. The same is true for Mathcad 12 worksheets. The Compatibility switches will be deprecated after several versions; they are intended to give you time to migrate your worksheets to version-neutral expressions."

So, what if I'm actually interested in performing the calculations on a sequential basis? Tom's suggestion of applying algebra is not attractive for more complex scenarios, e.g. given

A(0) = 0
FOR N = 1 TO 10

B(N) = A(N-1)
C(N) = some long function of B(N)
D(N) = yet another long function of C(N)
.
.
.
Z(N) = function of Y(N)
A(N) = function of Z(N)

NEXT N

Solving this with the multiple array scheme on a parallel assignment basis ("MC12" option) would require substitution of Z(N),Y(N)...D(N),C(N),B(N) into A(N) which in turn would result in a extremely large expression.

Seems like I'll resort to Stuart's procedure, unfortunately it doesn't support combination of different units.

Regards.

By reading your comments, you seem (your Mathcad version) to indicate that version past 14 will not support recursive construct. I don't believe, but I don't believe in God(s) either ! Why do you want to carry units in Fibonacci for instance ? The Fibonacci sunflower is only Cartesian !
I didn't visit Stuart proposal, it might be adapted. Many of the main responders don't visit much between themselves to avoid bias and open more interpretations, to the benefit of the demanding visitor.

jmG

You can use local variables within an array constructor to reuse previously calculated results.
__________________
� � � � Tom Gutman

On 10/22/2009 1:39:06 PM, Tom_Gutman wrote:
>You can use local variables
>within an array constructor to
>reuse previously calculated
>results.
>__________________
>� � � � Tom Gutman

Thanks, that seems to work regardless of the compatibility mode, the downside is that it requires rewriting of all equations in order to introduce the local variables, equation legibility is also affected unless I use literal subscripts to represent the old array subscripts.

Why is the sequential assignment feature being removed anyway? Can't PTC just leave it there as a toggle button inside the matrix properties dialog?

The sequential assignment was always a bug. It did not conform to either the documentation nor earlier Mathcad versions. The compatibility mode was added because some people used it a lot, some of them very vocal.
__________________
� � � � Tom Gutman

>...the downside is that it requires rewriting of all equations in order to introduce the local variables ...<<br> _________________________

You don't understand something: a sequence is unique, Neil Sloane & Simon Plouffe have collected 2 millions of them (not sure exactly). So you will have to code each one you want ! and vis the user local, you have to consider the programmed version which you had from myself and surely from more responders as well. You should code all the suggestions and see what it does for you. Do you have a specific application or just curious, generally ?



jmG

On 10/22/2009 4:07:23 PM, dcasanova wrote:
>...the downside is that it requires rewriting
>of all equations in order to introduce the local variables...<<br> __________________________

You mean the "local arguments".

If you have a scientific application, you better specify and source in order for collabs to size the interest and time to spare. You may need a backward ForLoop capable of carrying arguments, again a working version may be dead in other versions. If you have some terms of the sequence, plug that in Neil's web sequence. You will know in < 1 s if that sequence exists and the format of it and lot more.

jmG

On 10/20/2009 3:44:01 PM, dcasanova wrote:
>... can it be rewritten to the
>newer versions without losing functionality?
>_____________________________

If that one works, why to re-code ?
Multiple array should do something useful.

jmG



With a bit of applied algebra it can easily be re-written to be compatible with the proper parallel assignment.
__________________
� � � � Tom Gutman

Thanks for all the replies, I learned something new from each one. Stuart's procedure works great as long as both variables have the same units (different units are not supported in nested arrays, I think), in this regard Fred's algorithm hits right on the spot as it creates two separate arrays. The lack of programming simplifies the document and is a welcomed bonus.

Regards.

The matter is not about programming or not , rather about the end use of the recursive. You had no real use expressed as such, look at some applications attached.

jmG

Compact the project, more intelligible, no programme .



jmG
Top Tags