SMath Studio Forum
»
SMath Studio
»
Questions
»
Function call does not evaluate numerically inside a for() loop
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
Good day SMath community, I was battling this issue in my worksheet for a better part of the weekend. I wrote a formula (biaxial bending in a column) to calculate unknown input loads based on known response. A key to practical use of the formula is to solve for unknown response based on known input loads (backwards of what formula does). I attempted to use FindRoot() with no success. I decided to write a simple "solver" using a for() loop, which i have done many other times before. To my surprise the custom formula does not evaluate correctly inside the for() loop, while evaulating just fine outside it. This is rather baffling. Please take a look and help - i honestly dont know what to do here - this should be a simple task which proved to be very stubborn. Many thanks! formula.sm (89kb) downloaded 23 time(s).
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Hello Alex, Originally Posted by: Alex M. I decided to write a simple "solver" using a for() loop, which i have done many other times before. To my surprise the custom formula does not evaluate correctly inside the for() loop, while evaulating just fine outside it. The loop fails on i=2, where the neutral axis has a value that doesn't cross the section; looking at biaxial(2) this case is not handled (local variable #chunk not defined -> error on stress block properties calculation -> error in the loop) Originally Posted by: Alex M. definition of xii "erased" This is by design since SMath Studio 0.99.6622 Originally Posted by: Andrey Ivashov Calculation error happened during definition of the variable/function clears previous value of this variable/function if it is available; Edited by user 15 April 2018 12:08:42(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
1 user thanked Davide Carpi for this useful post.
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Alex M. This is rather baffling. Please take a look and help - i honestly dont know what to do here - this should be a simple task which proved to be very stubborn. Alex, everything is OK and NOT in few points. I worked a bit on your previous version. Now the last error "_ not defined" The incompatibility is too deep to be isolated... % appears as a factor is no problem for FindRoot solver. rotation° is the problem in the solver. The function is OK in the program data*rot(,), but the solver does not solve for (° because the demand is for solving the argument of a function of a function.. This in Mathcad is possible via an iterative Given/Find, not directly possible in Smath. What you can do and in fact is what you need, create a matrix of solutions from ... biaxial(°,%) ... works fine in the attached 6179 Cheers Alex ... Jean formula[1].sm (109kb) downloaded 28 time(s).
|
1 user thanked Jean Giraud for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
Oh thank you very much Davide and Jean, now i have something to work with :-) Jean, i entertained initially the idea of creating a matrix of solutions, but what proved to be a challenge is to interpolate the desired one from said matrix (which is a 3D surface) Davide, so is "variable undefined" a sign that function evaluated to an error? I managed to get my solution using a for() loop by removing the error you mentioned, but once i encapsulate said for loop in a function call the error is back formulaR2.sm (100kb) downloaded 24 time(s).Edited by user 15 April 2018 19:43:47(UTC)
| Reason: Not specified
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Alex M. Oh thank you very much Davide and Jean, noiw i have something to work with :-)
Jean, i entertained initially the idea of creating a matrix of solutions, but what proved to be a challenge is to interpolate the desire one from said matrix (which is a 3D surface) Alex, Your "self entertaining" was correct, but unfortunately up to this point, you have a system, not a surface. Mathcad would plot nothing either. Most probably it would generate "a critical error occured". The last olive can be programmed for convenience. I love projects that understand nothing but work ! I'm not a bridge maker, just a Process Control old man. Cheers Alex ... Jean formula[2].sm (129kb) downloaded 23 time(s).
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
... Alex, From this completed demo, you can: interpolate [cinterp from sanity plots] I tried some surface interpolation, too small data set [4 x 3]. Jean formula[3].sm (124kb) downloaded 27 time(s).
|
1 user thanked Jean Giraud for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
Originally Posted by: Jean Giraud ... Alex, From this completed demo, you can: interpolate [cinterp from sanity plots] I tried some surface interpolation, too small data set [4 x 3]. Jean formula[3].sm (124kb) downloaded 27 time(s). Hi Jean, greatly appreciate your help. In the example you developed, can you interpolate unknown values of "°" & "%" based on given values of P.z and ratio Mx/My? or find all possible values Mx and My for a known value of Pz This is what i am after. Surface defined by dots in coordinate system (PzMxMy) is an interaction diagram of column failure Edited by user 16 April 2018 00:41:07(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
I have attempted to approach the task from a different angle - parametrically define Pz(a#,na#) Mx(a#,na#) My(a#,na#) and solve for a# and na# by evaluating Pz, Mx, My using function at(). Function at() does not work with my definition. Davide, why am i hitting a brick wall here? Thanks BiaxialColumnCheck.sm (557kb) downloaded 30 time(s).
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Hi Jean, 1. In the example you developed, can you interpolate unknown values of "°" & "%" based on given values of Pz and ratio Mx/My? 2. or find all possible values Mx and My for a known value of Pz. This is what I am after. 3. Surface defined by dots in coordinate system (PzMxMy) is an interaction diagram of column failure ================================================
1. done, perfect 2. Don't understand completely 3. up until now, does not work well [NFG !]
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Jean Giraud 1. done, perfect 2. Don't understand completely 3. up until now, does not work well [NFG !]
================================1 & 3 done Can you re-explain your # 2 Alex3D.sm (37kb) downloaded 29 time(s). formula[3] Copy.sm (155kb) downloaded 25 time(s).The 3D crashes the main sheet
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
2. or find all possible values Mx and My for a known value of Pz. This is what I am after. =====================================
I think it's possible
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
Originally Posted by: Jean Giraud Originally Posted by: Jean Giraud 1. done, perfect 2. Don't understand completely 3. up until now, does not work well [NFG !]
================================1 & 3 done Can you re-explain your # 2 Alex3D.sm (37kb) downloaded 29 time(s). formula[3] Copy.sm (155kb) downloaded 25 time(s).The 3D crashes the main sheet Hi Jean, huge effort here from you - BIG thanks. Will take some time for me to digest. To make it a little easier for me can you point out where i can "interpolate unknown values of "°" & "%" based on given values of Pz and ratio Mx/My?". I know Pz, ratio of Mx/Mz and need to find "°" & "%" that correspond to it (all data is spit out by biaxial() formula). Many Thanks!
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Alex M. Hi Jean, huge effort here from you - BIG thanks. Will take some time for me to digest. To make it a little easier for me can you point out where i can "interpolate unknown values of "°" ====== will be back on that tomorrow ======
Here is the lookup wrt PzMxMy, alternately search from either col. formula[3] Copy.sm (169kb) downloaded 20 time(s).
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Alex M. To make it a little easier for me can you point out where I can "interpolate unknown values of "°" Maybe, I didn't completely understood ? Here is the refreshed version. formula[3] Copy.sm (163kb) downloaded 21 time(s).
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Originally Posted by: Alex M. I managed to get my solution using a for() loop by removing the error you mentioned, but once i encapsulate said for loop in a function call the error is back The problem here is that when you pass to a function an argument that requires numerical evalution and the evaluation is not possible because undefined variables, the preprocessing fails (at smath level) the workaround is to create a function that internally defines the leftside as the function that cannot be passed in argument and call your solver internally notice eval@ inside the solver, this makes possible to use solveNR both directly (with at()) both within the wrapper function solvePcr (not sure now why at() doesn't work in both the cases, have to investigate when it will be possible) formulaR2_rev2.sm (106kb) downloaded 35 time(s).Edited by user 16 April 2018 11:07:57(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
1 user thanked Davide Carpi for this useful post.
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Davide, Alex ... thanks for the distiller-solution. Two points about your Newton-Raphson example: 1. Will it work for this application in traditional code ? 2. Two [2] pitfalls: 2\a. about your cubic, it is ill-posed. In maths, it is paramount polynomials are expressed in Lagrange canonical format. For quick getting the polyroots and set the cubic solver. In fact, cubic may have complex roots [your example is cubic complex]. Complex solutions are essentials in modeling/solving Control Systems. 2\b. The Smath native "roots" solves directly your example. Rev(2) most appreciated for eventual hard solving future. Solve roots Newton-Raphson.sm (52kb) downloaded 25 time(s).
|
1 user thanked Jean Giraud for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 03/03/2014(UTC) Posts: 418 Was thanked: 125 time(s) in 96 post(s)
|
investigated a few options: 1. point interpolation on a surface is tough to implement (for me) 2. Solving for na# works well (Davide's last post) 3. Solving for rotation does not work well (relationship is far from linear, solver chokes up too often - see example formulaR4.sm (221kb) downloaded 15 time(s). ) My current solution is solve for na# (Davide's last post) by bracketing the rotation till desired Mx/My is achieved (not shown here)
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Alex M. investigated a few options:
1. point interpolation on a surface is tough to implement (for me) 2. Solving for na# works well (Davide's last post) 3. Solving for rotation does not work well (relationship is far from linear, solver chokes up too often - see example <a class="attachedImageLink {html:false,image:false,video:false}" href="/forum/resource.ashx?a=22839 From your suggested NR solver, here is attached. It solves 'na' [height%] wrt target Pz, rot° From what I understand, you would like solving mutually [rot°, height%]. That, you have it from the 'test function' ... you can collect multiple zigzag test ? Between close enough two points test, safe to linterp(,,,)For sparse > two test, just cinterp(,,,) Surface from another post is strictly linear. The Spline2D is equivalent to quadratic interpolation, but meshed fine enough is immaterial. Next => visit your last attachment. Project_0 Biaxial SolveRotdeg.sm (106kb) downloaded 27 time(s).
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
... suite,
by solving, simultaneously [rot°, _%], if biaxial would be an analytical f(x,y) you can solve x<=y or vice versa y<=x. Up until now, found only one that Smath/Maple does not solve. On the other hand, biaxial(rot°, _%) generates 3 sets of results. The CreateMesh [not the Smath CreateMesh] can alternately scan/evaluate and tabulate results for further use ... I'm working on that, in the mean time, if you have a good range of each [rot°, _%] that would be nice, though easy to modify.
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
|
|
|
|
SMath Studio Forum
»
SMath Studio
»
Questions
»
Function call does not evaluate numerically inside a for() loop
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.