While loops

While loops - How to recover multiple parameter values from within a while loop - Messages

#1 Posted: 7/21/2014 8:46:50 AM
cartographer

cartographer

0 likes in 4 posts.

Group: User

Hi ! I am in need of some help please from an Smath Studio expert.

I am fairly new to Smath and I am trying to port some maths (in fact two closely related programs that are largely spherical trig) to see if they will work in Smath. I have previously coded the software in Liberty Basic Pro, Python 3.4.1 and Mathcad (15 and Prime 3) and all give consistent, correct, results.

The code requires use of "while" loops within which to converge iterative elements of the math in each case. The problem is that it is necessary to get at the final values of several parameters that are used within the loops (6 in one case and 10 in the other) once convergence is complete. These values are then used for further, post convergence, calculations.

This is no problem in Liberty Basic and Python. However, it does not seem very easy (to me at least !)to produce "report quality" output via these options. Mathcad provides quality output, but the solution I found does not seem to work with Smath.

I have been exploring Smath for about a year and it now seems to have reached the stage (currently I am using RC 0.97.5301) where it can give good results. However, I cannot find any way of getting it to do what I need in respect of the recovery of multiple values from the while loops. I have been playing around with the use of column vectors to act as "containers" for returning the requisite values, but without success as yet. Am I on the right track ?

Can anybody help me please ?

#2 Posted: 7/21/2014 9:47:04 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello cartographer,

If you don't need to store intermediate results, you can use a standard while loop like the attached.
trace() function (wiki:trace) in this case helps a lot to debug.

In the other hand, if you need the collection of the values at any loop, you can use vectors/matrices to store the results.


Best regards,

Davide


P.S. Use numeric optimization to speed up calculations
2014-07-21 15_56_27-SMath Studio Desktop - [while.sm_].png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#3 Posted: 7/21/2014 10:27:52 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Wrote


The code requires use of "while" loops within which to converge iterative elements of the math in each case. The problem is that it is necessary to get at the final values of several parameters that are used within the loops (6 in one case and 10 in the other) once convergence is complete. These values are then used for further, post convergence, calculations.


If you just need a final set of values (and not the iteration history of the values) this is trivial for canvas-level loops, as the variables just have their last values and are visible outside the loop.

If the loop is part of a function body (right hand side of a function definition) then there are two ways to convey the result to the outer namespace:
- put the values in a vector or list and make that list the last instruction in the function body (return value).
- use assignments to function parameters

loop.sm (7 KiB) downloaded 71 time(s).
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#4 Posted: 7/21/2014 11:19:02 AM
cartographer

cartographer

0 likes in 4 posts.

Group: User

w3b5urf3r, mkraska,

Many thanks to you both for your quick replies.

With regards to the info from mkraska, I can confirm that just the final values are sufficient. However, when I tried a simple while loop, any attempt to reads the final values resulted in empty placeholders with red boxes and "undefined" messages. I think I must be close and I am doing something silly. I will try again and see what I can do.

With regards to Davide's info, I see strong similarities with the advice from Martin. I will have a go at adapting his first sample of code and see what transpires.

Regards,

Michael
#5 Posted: 7/21/2014 1:21:20 PM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

You might consider showing us your .sm file with some indication of what you tried and what you actually expect.
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#6 Posted: 7/23/2014 5:53:32 AM
cartographer

cartographer

0 likes in 4 posts.

Group: User

Martin,

Re your suggestion to send my problem code, I originally resisted this as I did not want to trouble folk unduly. However, I have now extracted the problem while loop from what is a growing worksheet, and this is attached. I hope it is understandable !

Having looked the the earlier replies I received from you I cannot see why this does not work.

Thanks again for the help.


Michael
testcase.sm (13 KiB) downloaded 75 time(s).
#7 Posted: 7/23/2014 6:19:27 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello Michael,

It seems here is a "standard" problem we bumped into (again).

Just right click on the while loop and choose Optimization->Numeric. Then you will get the results. This is advisable when you have only numerical calculations.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#8 Posted: 7/23/2014 8:55:17 AM
cartographer

cartographer

0 likes in 4 posts.

Group: User


Davide, Martin and Radovan - all is now solved and numbers emerge efficiently from the while loop !!

I have checked that numeric optimisation is on.
I have now added a vector within the loop in which to capture all of the required parameters.
Progress on my project will hopefully now be in a forwards direction once again !


Many thanks for all of your help.

Michael
  • New Posts New Posts
  • No New Posts No New Posts