cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead 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

Nested loops example - MSc Subsea Engineering Question

MikeArmstrong
5-Regular Member

Nested loops example - MSc Subsea Engineering Question

I have attached a worksheet for informational purposes. Recently a few people have been indicating that they are wanting to learn programming, so I thought it might be prudent to upload a worksheet that I have been working through.

It is a question from an MSc in Subsea Engineering which I am currently studying towards. It calculate the probability of a maximum wave occurring in a given time frame.

It clearly shows the use of nested loops which might help Mathcad 'newbies'.

Any feedback or questions welcome.

Mike

22 REPLIES 22

Shortened version in one program. Not as clear and unacceptable if produced for a calculation as a non-Mathcad user could not trace the program.

Mike

Might be clearer to separate the calculation of probabilities from the observed times - see attached. Note that in your worksheets you define Hs but don't subsequently use it!

Alan

Message was edited by: AlanStevens I realise that my worksheet doesn't meet your aim of showing newbies about programming nested loops, but the nice thing about Mathcad is that you can often go a long way without needing any exolicit programming structures.

MikeArmstrong
5-Regular Member
(To:AlanStevens)

Might be clearer to separate the calculation of probabilities from the observed times - see attached. Note that in your worksheets you define Hs but don't subsequently use it!

Sorry about that, Hs is used later in further calculations which where not included.

Message was edited by: AlanStevens I realise that my worksheet doesn't meet your aim of showing newbies about programming nested loops, but the nice thing about Mathcad is that you can often go a long way without needing any exolicit programming structures.

Its good that you have provided an alternative gives newbies a chance to compare.

Cheers

Mike

One comment. Unnecessary operations often use up so little extra time they are not worth worrying about (especially if that makes the worksheet clearer). But inside a loop, or a nested loop, unnecessary operations get calculated multiple times! In your case the loops are not that large, so this really does not matter that much, but sometimes it can make a huge difference to execution time. Moving only one operation outside the loop knocked 10-15% off the execution time of your program.

The moral of the story is that it's a good idea to get into the habit of looking for unnecessary operatons inside loops, and moving them outside the loops.

wayne
3-Visitor
(To:RichardJ)

Nice,

additionally, why multiply the boolean tests by 1,surprisingly to me, it makes a difference.

RichardJ
19-Tanzanite
(To:wayne)

Yes, you are right. It also surprises me how much time that costs.

wayne
3-Visitor
(To:RichardJ)

There's another one in the nested loop, bumbs the difference up to 7.5%.

RichardJ
19-Tanzanite
(To:wayne)

I missed that one

RichardJ
19-Tanzanite
(To:RichardJ)

Taking this to it's limit, there's a couple of other things we can do too.

There is some more stuff we can move outside the loop. I'm surprised this doesn't make a bigger difference.

We can get rid of the final vectorized multiplication, which is itself a nested loop (just an implicit one rather than an explicit one), by moving the multiplication inside the existing loop. This doesn't make much difference, but is slightly faster.

wayne
3-Visitor
(To:RichardJ)

Richard,
followed up a little., noting that neither N2z or Hm even need to be in the program, let along in the first loop.

However, I get different times, if I keep pressing f9 on each. sometimes better, sometimes worse. The trend shows improvement as expected, but less than thought at first.

Kept playing by disabling, etc. the best I can do is a 19% improvement by taking your last P5 function and taking N2z and Hm out of the program. But it appears that just running the sheet one time does not always give you accurate information. even misleading about the amount of improvement, or sometimes, I think, I had previous P's better than the last one.

Any way, just adding the point that the computer execute time is dependent on what else the computer is doing and what the resources are at any particular instant. So without other extreme methods to make a test fair, I sort of get a feeling by recalculating a bunch of times and may changing order or disabling some of the other compairsons as well.

(do I have this sort of right?)

RichardJ
19-Tanzanite
(To:wayne)

(do I have this sort of right?)

That about sums it up

Or you could increase the number of trials to about 100,000,000 and just average those variations out over an entire days run time

MikeArmstrong
5-Regular Member
(To:wayne)

Any way, just adding the point that the computer execute time is dependent on what else the computer is doing and what the resources are at any particular instant. So without other extreme methods to make a test fair, I sort of get a feeling by recalculating a bunch of times and may changing order or disabling some of the other compairsons as well.

I get the same feeling. The results seem inconsistent to say the least.

Mike

I should add that the best approach to get consistent results seems to be to get the section on screen where you can see the timings, and then hit Ctrl F9 to recalculate everything. There is still quite a bit of variation, but less than hitting F9 on each program.

Mike Armstrong wrote:

Any way, just adding the point that the computer execute time is dependent on what else the computer is doing and what the resources are at any particular instant. So without other extreme methods to make a test fair, I sort of get a feeling by recalculating a bunch of times and may changing order or disabling some of the other compairsons as well.

I get the same feeling. The results seem inconsistent to say the least.

Mike

The results do depend upon a number of background factors as well as the worksheet itself - I've got a vague recollection of seeing more consistency and improved execution speed when i turned off the virus checker and SETI@Home (or BOINC now).

However, you can also add a degree of consistency by wrapping up the function list inside a program - see attached, which makes use of my Timer1 and Counter1 functions.

I like this game 🙂

Stuart

Stuart,
That's neat.

(But I think the SWAG method is looking better and better.

MikeArmstrong
5-Regular Member
(To:StuartBruff)

Superb work Stuart,

Just like to thank all who have contributed to this thread - much appreciated.

Mike

RichardJ
19-Tanzanite
(To:StuartBruff)

However, you can also add a degree of consistency by wrapping up the function list inside a program - see attached, which makes use of my Timer1 and Counter1 functions.

I had forgotten about those functions. Looks like I can't keep track of everything ever posted either

This seems to have gone from a general comment about avoiding unnecessary operation in loops to a full blown optimization exercise!

MikeArmstrong
5-Regular Member
(To:RichardJ)

This seems to have gone from a general comment about avoiding unnecessary operation in loops to a full blown optimization exercise!

That tends to happen more regularly these days

Mike

RichardJ
19-Tanzanite
(To:StuartBruff)

Looking at the extra versions you added, I guess that a valid comment would be "the best thing you can do with nested loops is get rid of them entirely"

MikeArmstrong
5-Regular Member
(To:RichardJ)

Looking at the extra versions you added, I guess that a valid comment would be "the best thing you can do with nested loops is get rid of them entirely"

Good point indeed, but they are needed on those rare occasions.

Mike

MikeArmstrong
5-Regular Member
(To:wayne)

additionally, why multiply the boolean tests by 1,surprisingly to me, it makes a difference.

Nice spot Wayne, I was meant to remove this before posting.

Mike

MikeArmstrong
5-Regular Member
(To:RichardJ)

Good point Richard, cheers for the contribution.

Mike

Top Tags