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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

How to drop out of loop

ptc-2283641
5-Regular Member

How to drop out of loop

Here is my FORTRAN statements:

      DO 1010 I=1,L

        DO 1010 J=1,NC119

          X119(I,J) = 0.

          X132(J,I) = 0.

          IF(YEN(J) -YV(I)) 1010,1000,1000

 1000       X119(I,J) = 1.0

 1010 CONTINUE

      JAK = 1

      DO 1040 I=1,NC119

        DO 1020 J=JAK,L

          IF(YE(J)) 1020,1020,1030

 1020   CONTINUE

      GO TO 1040

 1030   X132(I,J) = 1.0

        JAK = J+1

 1040 CONTINUE

I include my attempt, results and what I want.

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:ptc-2283641)

Are you looking for this?

LucMeekes_0-1582411861840.png

Success!
Luc

View solution in original post

16 REPLIES 16
LucMeekes
23-Emerald III
(To:ptc-2283641)

Your Prime  and Fortran programs are different.

Assuming the Fortran code functions according to your desired behaviour (whatever that may be), these are the differences I see so far:

JAK <- JAK+1 should be JAK<J+1

You to place this JAK assigment immediately below the X132 assignment, not below/outside of thye entire IF statement.

 

Success!
Luc

ptc-2283641
5-Regular Member
(To:LucMeekes)

X132i,j  1
JAK  j + 1
gives me a 1x12 matrix, not a 2x16.
Ken
LucMeekes
23-Emerald III
(To:ptc-2283641)

The "IF(YE(J))" in your Fortran code looks like an IF statement at first sight, but it isn't because it is NOT followd by "THEN".

What does it do? Especially the three numbers following the IF() are puzzling. Are they line numbers? And how are they processed?

In the first Mathcad program (X119) you translated the IF(argument) with "if argument >=0" in X132 you translated a similar construct IF(argument) with 'if argument <=0". Which one of the two is right, 'less than or equal', or 'larger than or equal' ?

 

It might help a lot if you could explain what functionality you wanted to program. What is the effect that your function should accomplish, in plain text.

 

Success!
Luc

 

ptc-2283641
5-Regular Member
(To:LucMeekes)

That "IF(YE(J))" in the Fortran code IS an IF statement, unless you are using some version I am not aware of. "IF...THEN..." sounds like  BASIC code too me. To me "IF(YE(J)) 1020,1020,1030" in FORTRAN means "IF(YE(J)) ≤ 0 go to statement 1020,or > 0 go to statement 1030". I work in FORTRAN versions like WATFOR or 95, which might be before your time and any newer International Standards.

 

If you were able to examine my MathCad Prime 3.1 attachment, I listed the known variables and attempts for the 2x16 row matrix called X132. I just cannot get a single "1" in the desired elements, i.e. 1,1 and 2,12. The 16x2 column matrix X119 is correct.

ptc-2283641
5-Regular Member
(To:ptc-2283641)

Got it! See attachment.

LucMeekes
23-Emerald III
(To:ptc-2283641)

Are you looking for this?

LucMeekes_0-1582411861840.png

Success!
Luc

ptc-2283641
5-Regular Member
(To:LucMeekes)

Most interesting approach, I was not aware that any version of MathCad (including Prime) had these functions.

Thanks for you assistance. Hope you were able to view my previous attachment.

Ken

ptc-2283641
5-Regular Member
(To:LucMeekes)

LucMeeks,

Here is my final attempt. I have tried reducing statements, but I cannot. Your solution seems to want an exact match(?) between YEN and YV.

Ken

Werner_E
25-Diamond I
(To:ptc-2283641)

Here is my attempt to translate your FORTRAN program to Prime one to one. Its similar to yours and the missing point, as you had found out on your own, was the break command to jump out from the inner loop (which nowadays is considered bad programming style ;-).

Werner_E_0-1582503401855.png

Of course this can be made a bit shorter because in Mathcad, as you found out yourself, its not necessary to set every single element in a matrix to zero. The values are zero by default, no need to initialize.

Not sure if you already realized, that variables used in a Prime program at the left side of an assignment are always local variables only and do not correspond to variables at worksheet level with the same name. So your second program does NOT use the X132 you created in your first program. The only way to change worksheet variables is to assign them the result of a program. So you may notice that the program variables in my program are called X119 and X132 but the worksheet variables have different names (same, but with the number as literal index).

 

Luc's suggestion only works in MC11, not in MC15 and also not in Prime.

Here is a similar approach which works in Prime, too. It uses Y.E for comparison as the original program:

Werner_E_0-1582504923145.png

 

As I could only provide a Prime 5 or Prime 6 worksheet, I don't attach it as your Prime 3.1 would not be able to read it.

 

 

LucMeekes
23-Emerald III
(To:ptc-2283641)

You've multiplied the result of the match() function with ORIGIN, whereas you should only take its first element.

Please try again with the attached file that should open in Prime 3.1

 

Success!

Luc

Werner_E
25-Diamond I
(To:LucMeekes)

This is what we get after recalculating the sheet:

Werner_E_0-1582580595992.png

 

I was surprised to see, that in Prime we can use "max" as function in matrix again. As you know it was allowed in MC11, but it failed in later versions (at least in MC14 and MC15).

Nevertheless I feel that in this case simply assigning the "last" element of the matrix a zero would be more efficient

Werner_E_0-1582580912008.png

 

 

LucMeekes
23-Emerald III
(To:Werner_E)

Provided that ORIGIN=1 !!

I've used max a lot of times as third argument in the matrix() function, see my 'it's a joke' post.

I agree that:

R[ORIGIN+rows(YEN)-1,ORIGIN+rows(Y)-1 <- 0

would be better here, but it's longer...

 

I may have made a mistake in implementing Match() in Mathcad 11.

Can you changing "geq" to "leq"?

 

Luc

 

Werner_E
25-Diamond I
(To:LucMeekes)


Can you changing "geq" to "leq"?


Sure, here you are - looks good

Werner_E_0-1582590865265.png

 

ptc-2283641
5-Regular Member
(To:LucMeekes)

Your file Untitled_20200224_Prime3_1.mcdx does not open.

Changing "geq" to "leq", and using R(subscript rows(YEN),rows(Y)) ← 0 gets me this 2x16 matrix

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 1 2 3 4 5 6 7 8 9 10 11 12 13 14   0

Even leaving off ORIGIN give same results. See my Prime 3.1 attachment where I now initialize the matrix X132 to "0" by placing it in the X132 program to keep from returning two matrices in the X119 program. Changing the "if Yv(j) > 0" statement to "if YEN(i) - Yv(j) > 0", and adding the "also if X132(i,j) > 0" statement to break out of the "DO 1020" inner-loop gives me my matrix requiring a "1" in its aero-panel location, i.e.

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 

 

 

LucMeekes
23-Emerald III
(To:ptc-2283641)

Here's the file, corrected and saved from Prime 3.1, so it should open in Prime 3.1.

I'll explain the program:

The first line creates R as a matrix of 0's, the size of which is determined by the numbers of rows of YEN and Y, using the matrix function, and sets it to all 0's by multiplying it with 0.

The second line starts the for loop, to go through all (at present 2...) elements of vector YEN.

Within that loop the first statement  uses the match function that finds all indices in array Y whose value is less than, or equal to th chosen YEN element's value. Given the values above, match finds the list of indices from ORIGIN to last(Y) when we're working with the first value of YEN. From that array, only the very first (the one with index ORIGIN) is assigned to e. So e holds the index where, for the first time, an element value of Y is below the chosen value of YEN.

In the second row the element of matrix R whose index is determined by r and e is set to 1.

The second time around in this loop, match should give a list of indices starting at the 12th element and on to the last. Again only the first of that list, the index pointing to the 12th element is assigned to e. An again at the chosen position in R a 1 is set.

When all elements of YEN are processed, the matrix R is returned as the function result.

 

Success!
Luc

 

ptc-2283641
5-Regular Member
(To:LucMeekes)

Thanks for your help.

Announcements

Top Tags