Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- Re: How to drop out of loop

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

02-22-2020
12:44 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
12:44 PM

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.

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

02-22-2020
05:51 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

16 REPLIES 16

02-22-2020
01:19 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
01:19 PM

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

02-22-2020
02:04 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
02:04 PM

X132i,j ← 1

JAK ← j + 1

gives me a 1x12 matrix, not a 2x16.

Ken

- Tags:
- programming

02-22-2020
04:49 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
04:49 PM

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

02-22-2020
05:09 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
05:09 PM

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.

02-22-2020
06:00 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
05:51 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
06:05 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-22-2020
06:05 PM

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

02-23-2020
07:41 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-23-2020
07:41 AM

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

02-23-2020
07:25 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-23-2020
07:25 PM

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 ;-).

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:

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.

02-24-2020
03:47 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-24-2020
03:47 PM

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

02-24-2020
04:43 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-24-2020
04:43 PM

This is what we get after recalculating the sheet:

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

02-24-2020
05:00 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-24-2020
05:00 PM

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

02-24-2020
07:34 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-24-2020
07:34 PM

Can you changing "geq" to "leq"?

Sure, here you are - looks good

02-24-2020
07:43 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-24-2020
07:43 PM

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

02-25-2020
02:13 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-25-2020
02:13 AM

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

02-25-2020
10:59 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

02-25-2020
10:59 AM

Thanks for your help.