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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

3-Visitor

## For loop and matrices

Hello. I am looking for some assistance with for loops and matrices within Mathcad. I have a relatively basic knowledge of mathcad so this may be a straightforward solution.

I am trying to return a value for m11 for each of the sigma(11) values. However, the way i have it set up, it only returns the m11 value when i = 2.

I know i could list the equation out 3 times, changing the "i" with 0, 1, and 2 each time; however i had thought i may be able to do the same thing using the for loop function.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
23-Emerald III
(To:FC_10063827)

A Mathcad program returns ONLY the value of the last statement executed. That's why you see a single element instead of an array.

Here's what you could do (simplified):

(I could have typed just R, instead of return R, but it makes clear what result the program should return. Note that a return statement put anywhere in the program makes the program stop there and return the argument of the return. So you can put it in a IF statement if you need to stop the program in the middle of a loop.)

But in general I prefer to define a function (prevents strange things happening when the FOR loop gets out of sync with the length of the array):

A word of advice: refrain from evaluating a definition (that is, do NOT put an = immediately following a definition, like you did with m.11). First define                        m.11 :=  ....

Then, on a separate spot, evaluate it with      m.11 =

Yes, it works (most of the time), but occasionally it creates problems.

Success!
Luc

5 REPLIES 5
23-Emerald III
(To:FC_10063827)

A Mathcad program returns ONLY the value of the last statement executed. That's why you see a single element instead of an array.

Here's what you could do (simplified):

(I could have typed just R, instead of return R, but it makes clear what result the program should return. Note that a return statement put anywhere in the program makes the program stop there and return the argument of the return. So you can put it in a IF statement if you need to stop the program in the middle of a loop.)

But in general I prefer to define a function (prevents strange things happening when the FOR loop gets out of sync with the length of the array):

A word of advice: refrain from evaluating a definition (that is, do NOT put an = immediately following a definition, like you did with m.11). First define                        m.11 :=  ....

Then, on a separate spot, evaluate it with      m.11 =

Yes, it works (most of the time), but occasionally it creates problems.

Success!
Luc

3-Visitor
(To:LucMeekes)

Thank you for providing this. Very useful information to know for the future. Managed to make both work for my problem too

13-Aquamarine
(To:FC_10063827)

two other possible ways ...

3-Visitor
(To:JXBWk)

Thank you for this. Capture 1 works best for me as it was very similar to how the actual calculation I have was laid out.

23-Emerald II
(To:FC_10063827)

For those who have similar problems but only have Mathcad Prime Express, here are a few other ways around using programmed for loops.  They work in Mathcad Prime Express 7 and should work in earlier versions.

Stuart

Announcements
Top Tags