Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The definition:</p> <pre><code>functionB2[x_] := model /. fit </code></pre> <p>is an instruction to Mathematica to replace all future occurrences of an expression that looks like <code>functionB2[x_]</code> with the result of substituting the value of the argument for every occurrence of <code>x</code> in the expression <code>model /. fit</code>. But there <em>are</em> no occurrences of <code>x</code> in <code>model /. fit</code>: the only symbols in that expression are <code>model</code> and <code>fit</code> (and, technically, <code>ReplaceAll</code>). Therefore, the definition returns a fixed result, <code>model /. fit</code>, irrespective of the argument. Indeed, the definition could just simply be:</p> <pre><code>functionB2a[] := model /. fit </code></pre> <p>If you plot <code>functionB2a[]</code>, you will get the same result as if you plotted <code>functionB2[anything]</code>. Why? Because <code>Plot</code> will evaluate that expression while varying the symbol <code>x</code> over the plot range. It so happens that <code>model /. fit</code> evaluates to an expression involving that symbol, so you get the exhibited plot.</p> <p>Now consider <code>functionB1</code>:</p> <pre><code>functionB1[x_] = model /. fit </code></pre> <p>It too says to replace all occurrences of <code>x</code> on the right-hand side -- but this time the right-hand side is evaluated <em>before</em> the definition is established. The result of evaluating <code>model /. fit</code> is an expression that <em>does</em> contain the symbol <code>x</code>, so now the definition is sensitive to the passed argument value. The net result is as if the function were defined thus:</p> <pre><code>functionB1a[x_] := 4/Sqrt[3]-0.335/(0.435+x)^2+0.347/(0.712+x)^4-0.27/(4.29+x) </code></pre> <p>So, if you plot <code>functionB1[Sqrt[x]]</code>, the <code>Plot</code> command will see the expression:</p> <pre><code>4/Sqrt[3]-0.335/(0.435 +Sqrt[x])^2+0.347/(0.712 +Sqrt[x])^4-0.27/(4.29 +Sqrt[x]) </code></pre> <p><strong>Formal Symbols</strong></p> <p>When establishing definitions using <code>SetDelayed</code>, the name of the formal argument (<code>x</code> in this case) is independent of any occurrences of the same symbol outside of the definition. Such definitions can use any other symbol, and still generate the same result. On the other hand, definitions established using <code>Set</code> (such as <code>functionB1</code>) <em>rely</em> on the result of evaluating the right-hand side containing the same symbol as the formal argument. This can be a source of subtle bugs as one must take care not use symbols that accidentally have pre-existing down-values. The use of formal symbols (described in <a href="http://reference.wolfram.com/mathematica/tutorial/LettersAndLetterLikeForms.html" rel="nofollow">Letters and Letter-like Forms</a>) for argument names can help manage this problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThe situation is actually somewhat more complex since both `Set` and `SetDelayed` are scoping constructs and sometimes rename their pattern variables, while of course they will not be renamed for the r.h.s. of `Set`. This may not be apparent for the top-level definitions (where such renamings are not so aggressive), but this causes subtle problems for example inside `Module`, as discussed e.g. in this thread: http://forums.wolfram.com/mathgroup/archive/2010/May/msg00102.html. My own conclusion is that such implicit constructs as `f[x_]=a/.b` are not reliable and should not be used.
      singulars
    2. CO@Leonid Agreed. There are situations when it is useful to establish a generated definition, but it would be safer to set up such definitions using other strategies, e.g. ones that involve `With`, localized symbols and/or formal symbols. As you say in the cited post, the `Set` strategy can be very useful in an interactive context, but it is another one of those things (like evaluation leakage) that is hard to get correct when writing library code.
      singulars
    3. COYes, that's exactly what I meant. I do not mind doing the "scope surgery" and manipulating the variable bindings in scoping constructs, but my feeling is that such manipulations generally fall under 2 distinct categories - well-defined (those where you have the full control over the result), and not well-defined (those where you only have partial control). And I try to avoid the latter, while the example under discussion clearly belongs to it. Also, in my experience, it was always possible to achieve the goal with methods available within the first category.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload