Functions As Arguments

Functions As Arguments - Passing functions containing programming elements to other functions. Order of ops with temperature - Messages

#1 Posted: 11/1/2016 3:58:54 PM
jbutler

jbutler

0 likes in 5 posts.

Group: User

I ran into a couple of issues while running some calculations.

In order to minimize repeated blocks of operations, I found a need to pass a function which contains programming elements to another function or program. It would appear that this is either not possible, or more probably, I am doing something wrong.

I have attached a simplified case that exhibits the issue.

WhatAmIDoingWrong1.sm (9 KiB) downloaded 80 time(s).

Would someone please let me know what I am doing wrong?

THANKS!
-Jeff

P.S. Additionally I found that negative values with temperature units requires parentheses.
#2 Posted: 11/1/2016 5:47:23 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Smath shhould do what you want.
The matter is to dress correctly.
Two typical examples. The Gaussian
spot take ~ 12 sec to show image.
Let it run.

Jean

Utilities Discrete Algo Range.sm (17 KiB) downloaded 68 time(s).
#3 Posted: 11/1/2016 6:37:48 PM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Wrote

I ran into a couple of issues while running some calculations.

In order to minimize repeated blocks of operations, I found a need to pass a function which contains programming elements to another function or program. It would appear that this is either not possible, or more probably, I am doing something wrong.

I have attached a simplified case that exhibits the issue.

WhatAmIDoingWrong1.sm (9 KiB) downloaded 80 time(s).

Would someone please let me know what I am doing wrong?

THANKS!
-Jeff



Hello Jeff,

I think this might be a bug

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#4 Posted: 11/1/2016 8:29:08 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

I ran into a couple of issues while running some calculations.

In order to minimize repeated blocks of operations, I found a need to pass a function which contains programming elements to another function or program. It would appear that this is either not possible, or more probably, I am doing something wrong.

I have attached a simplified case that exhibits the issue.

WhatAmIDoingWrong1.sm (9 KiB) downloaded 80 time(s).

Would someone please let me know what I am doing wrong?

THANKS!
-Jeff

P.S. Additionally I found that negative values with temperature units requires parentheses.



Hey I ran into similar before. For one reason or another the built in if() does not work in programming when cases() from plugins does, see attached.

I feel this is a bug, but I would wait for more knowledgeable members to chime in.

P.S.: cases() is more flexible version of if() anyway.

In regards to negative temperature value you more or less answered it yourself. In first case you applied "C" unit to 196 only, while in the second case you have applied unit "C" to (-196). Smath Studio does not differentiate between "-" as an operator and "-" as a sign, so units are applied to the previous number 196 only, unless you force it otherwise. You can actually clearly see to which expression you are applying units if you keep pressing right arrow before you press ' to apply units.

WhatAmIDoingWrongSOLVED.sm (11 KiB) downloaded 48 time(s).
WhatAmIDoingWrongSOLVED.png
2 users liked this post
Radovan Omorjan 11/2/2016 7:20:00 AM, Davide Carpi 11/2/2016 8:22:00 AM
#5 Posted: 11/2/2016 12:07:48 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Your construction looks in conflict

if.gif
#6 Posted: 11/2/2016 8:00:45 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

I guess the problem is that Function1() throws an error while his arguments are proprocessed to be passed to the procedure (possible fix: skip the exceptions at this level, if it is consistant). I'll move this topic in bug & problems, though.

If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
Radovan Omorjan 11/2/2016 9:22:00 AM
#7 Posted: 11/2/2016 2:36:57 PM
jbutler

jbutler

0 likes in 5 posts.

Group: User

Thank you all for your input.

Alex, thank you, I think the cases() function will allow me to work around the issue (with if-else).

I think most of you understand the the problem I presented, but let me try and clarify.

EXAMPLE THAT WORKS AS EXPECTED
Any function containing one or more "NORMAL" MATH FUNCTIONS, can be passed to another function without error.
good.gif

EXAMPLES THAT PRODUCE UNEXPECTED RESULTS
Any function containing ANY PROGRAMMING CONSTRUCTS, fails to function, or causes an error, when passed to another function.
while.gif

if-else.gif

I'd also just like to add that this one EXCELLENT piece of software! The units handling is a fantastic feature! SO MUCH easier to do engineering calculations than with Excel!

Best regards,
-Jeff

#8 Posted: 11/3/2016 12:13:31 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Jeff,

I'm not sure about what you are trying to do.
(xb,x≤c,x≥d) ... semiconstrained is generally adequate
for plotting. In other instances must complete (x≤a,x≥b).
In other instances must be bolean ...

Test(x,y):=(y+y1(x)≥0)*(y-y1(x)≤0)

Jean

Maths 2D Plot Constrained.sm (9 KiB) downloaded 47 time(s).
Inst_Segment Partial Random.sm (38 KiB) downloaded 41 time(s).
#9 Posted: 11/3/2016 12:49:08 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

... demo the "completely constrained"
combined in bolean.

Jean

Maths 2D Plot Constrained.sm (21 KiB) downloaded 47 time(s).

2D Plot Region.gif
#10 Posted: 11/3/2016 1:06:14 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Note: in the function Pts
you see the 1 in the x,y definitions
and in the while loop, they are not subscript
they are index to explode Random in vector.
#11 Posted: 11/3/2016 1:24:36 PM
jbutler

jbutler

0 likes in 5 posts.

Group: User

Hi Jean,

Here is what I was trying to do. I was evaluating SMath Studio using an example I had implemented in other software (the equation for a Platinum RTD and its inverse using Newton-Raphson). I was only sharing my findings of what I *THOUGHT* should work, but didn't. My previous posts in this thread were simplified *examples*. They were not intended to show *real math*, only to highlight what I thought might be an issue. I have since learned that there are a few ways I can work around the issue. So, at this point, it is no longer a problem for me.

Since the RTD equations differ for t>0°C and t<0°C I started with an if() statement. But I couldn't seem to pass that to my N-R function, as shown here (with the cases() solution).
rtd.gif

I have since found that I could also implement it as shown here.
rtd2.gif

In the process of preparing this example, I found that NR() is evaluating to 0, unless the if() statement is within a line() block. Here are the tooltip captures.
tt1.gif

tt2.gif

Here is the file.
CVD.sm (26 KiB) downloaded 52 time(s).

-Jeff
#12 Posted: 11/3/2016 3:27:53 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Hello Jeff,
New England [Maine], that's not far from Drummondville [QC] !
I visited many many times Maine, Vermont ... and many more.

Few things needs be known before the hard work:
0. Van Dusen Callendar is a dead horse.
1. RTD is a primary standard, governed by BIPM [Bureau International des Poids et Mesures].
2. BIPM gives the total range in two polynomial segments because of the low temperature elbow.
3. I still have them somewhere in Mathcad 11. They are in the public domain otherwise.
4. Being of polynomial format, therefore analytical, easy to solve for Ω from °C lab bath.

The other part of the story is much simpler.

Some years ago, I have published in my Mathcad web site a "very good" RTD formula.
What was not published is the technique behind the "finding". Was done Mathcad 11.

I will be back soon, my Smath RTD needs a bit of better presenation.

Cheers, Jean


#13 Posted: 11/4/2016 10:05:18 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Jeff,

Please, visit Pt100 in "Samples" [posted last minute].
Observe 'argument(w,t)' passed in the solver:
Sol(w,t):=solve(argument(w,t)≡0,t,-260,970)
Your request: "Function(s) as Argument"

Jean
#14 Posted: 11/22/2016 5:45:21 PM
Arie

Arie

93 likes in 285 posts.

Group: User

I was also having some strange issues with my programs that passed functions. Specifically really strange behavior with using ranges in the el() function. However, submatrix() worked just fine. I also had some issues with vectorization. If i tried performing a function within the program, I would receive an error. However, if the program returned the input to the function and I used the same call outside of the program, it worked fine.

I removed any if() statements used in my programs and replaced with cases(). The issues seemed to resolve themselves.

Edit: I'm using 0.98.6128.10018
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
#15 Posted: 11/23/2016 12:27:54 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Hello, Jeff

I don't understand what you are trying to do vs PT100 formulas.
Either formula I gave or from BIPM. AFAIU, you are trying to
inverse [solve] a discontinuous function. That is not native
in Smath older versions for which solvers don't take "if".

I'm not arguing that "cases" might do.
This is as far as I can help from what I understand.

Jean

Inst_Pt100 [VDC].sm (16 KiB) downloaded 41 time(s).
#16 Posted: 11/23/2016 1:06:12 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

This document includes solving both VDC segments.
Strictly speaking " Functions as arguments" ...
in "2D Bar Stem More.sm", observe in the first
collapsed area ... observe the last argument
Bar:=Stem(p), calling two first functions as
arguments [Smath is delighted doing !].

Jean

Inst_Pt100 [VDC].sm (22 KiB) downloaded 42 time(s).

2D BarStem MORE.sm (90 KiB) downloaded 40 time(s).
#17 Posted: 11/23/2016 1:17:55 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

... in VDC: move the positive segment a bit down to clear the 1rst dichotomy.
Observe the dramatic error of VDC at NEGPt100(BIPM=-259.3467)=-7.86 !!!

Jean
  • New Posts New Posts
  • No New Posts No New Posts