A problem in Mathematica 8 with function declaration
Asked Answered
P

2

6

This is a strange result with a function defined as "functionB" in this example. Can someone explain this? I want to plot functionB[x] and functionB[Sqrt[x]], they must be different, but this code shows that functionB[x] = functionB[Sqrt[x]], which is impossible.

model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435,
    b3 -> 0.712};
functionB[x_] := model /. fit

Show[
 ParametricPlot[{x, functionB[x]}, {x, 0, 1}],
 ParametricPlot[{x, functionB[Sqrt[x]]}, {x, 0, 1}]
 ]

functionB[x] must different from functionB[Sqrt[x]], but in this case, the 2 lines are the same (which is incorrect).

Pruinose answered 18/5, 2011 at 14:45 Comment(8)
It's definitely not a bug... See my answer below (and doubtless numerous others that will appear soon)Nobile
I will try to remember this example to confuse/challenge my students the next time I teach with MMA. :) Some suggestions: I'd highly recommend you get a copy of 'Mathematica Navigator' (3rd). It's available for cheap and contains lots of examples to clear up your programming thought process. Next, get Sal's 'Mathematica Cookbook'--you should be able to find an eBook deal online for $9.99. Once comfortable with those, download Leonid Shifrin's 'Mathematica Programming - An Advanced Introduction' (it's free!) WARNING: do not dive into Leonid's book until you're ready! He's a smart dude.Tolle
ParametricPlot is a bit overkill here given that the first coordinate is a plain x. Plot[functionB[x], {x, 0, 1}] suffices.Grethel
@telefunken Please abstain to comment like that about Leonid's book. He is lurking around here and your effusiveness may cause him to have second thoughts about giving away his book! :DJevons
@belisarius, @Tolle The good thing about Creative Commons License is that it is irrelevant whether or not I change my mind after releasing under it, at least for whatever version was released under it - that particular version will always be free :). But the issue in question is beyond its scope anyway (pun intended). Apart from the excellent answer of @acl, this thread is possibly relevant (due to its format, you'll need to track replies to see them all - unlike google groups where google does this for you) forums.wolfram.com/mathgroup/archive/2010/May/msg00102.htmlHandclasp
@user759342, do either of the answers answer your question? If so, then you should accept that answer by using the checkmark. This has three effects, both you and the poster get a reputation boost, and it allows others know that that answer seems to be the correct one.Lubricous
@Leonid Shifrin: Thank you for your solution.Pruinose
DaoTRINH, who is the cutie in your profile picture?Porty
N
10

If you try ?functionB, you'll see that it is stored as functionB[x_]:=model/.fit. Thus, whenever you now have functionB[y], for any y, Mathematica evaluates model/.fit, obtaining 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(4.29 + x).

This has to do with using SetDelayed (i.e., :=). The rhs of functionB[x_]:=model/.fit is evaluated anew each time Mathematica sees the pattern f[_]. That you have named the pattern x is irrelevant.

What you want could be achieved by e.g. functionC[x_] = model /. fit. That is, by using Set (=) rather than SetDelayed (:=), so as to evaluate the rhs.

Hope this is clear enough (it probably isn't)...

Nobile answered 18/5, 2011 at 14:56 Comment(6)
Thank you, I understand. Vote for you !Pruinose
if it answers your question, you can "Accept" it as the answer. or you can wait to see if other, better answers turn upNobile
@acl, at one point, I don't think that would have worked, and you'd have to Evaluate the RHS first and use SetDelayed. But, according to the docs under Scope, it works correctly (in v.7, also). I wonder when the change was. So, you get a +1 for teaching me something new about an old function.Lubricous
@Lubricous I know for certain that Mma versions 4 through 8 all support using Set in this way. I suspect this behaviour goes all the way back to v1, but perhaps an old(er) timer can set me straight.Protolithic
@WReach, I seem to remember it not working for me, but it has been a long time since I tried it. (Although, if something is buggy, and I've accidentally done this, it is the first thing I change.) I could be remembering behavior from v.3, as that when I first actively used mma.Lubricous
@Lubricous I suspect that you are referring to the pitfalls described by @Leonid in one of his comments to a follow-up question -- namely that symbols in the context of the RHS of Set do not play well with nested scoping constructs.Protolithic
N
3

You might want to try defining the model inside functionB so x in both places are related:

fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435, b3 -> 0.712};
functionB[x_] := Module[
  {model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4},
  model /. fit
]
Nightlong answered 18/5, 2011 at 16:5 Comment(1)
dev, prior to re-reading the docs, I'd agree that this would work where acl's solution involving Set would not. But, apparently you can use a pattern rule with Set, also, and get the answer you expect.Lubricous

© 2022 - 2024 — McMap. All rights reserved.