About the graphing process - another question about the "Problem in Mathematica 8 with function declaration"

Related Problem in Mathematica 8 with Function Declaration

Clear["Global`*"] 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}; functionB1[x_] = model /. fit; functionB2[x_] := model /. fit; 

The difference in ratings between functions B1 and functionB2 can be detected by the Trace command in mma, as shown below:

 functionB1[Sqrt[0.2]] // Trace functionB2[Sqrt[0.2]] // Trace 

I have no questions about function B1. , which puzzles me , because functionB2[Sqrt[0.2]] does not even give a numerical result, but gives the function x 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/( 4.29 + x) , and then how is its plot Plot[functionB2[Sqrt[x]], {x, 0, 1}] possible?

I mean, when you run Plot[functionB2[Sqrt[x]], {x, 0, 1}] , what happens inside mma is:

x takes a number, say 0.2, then 0.2 finally passes to B2, but B2 gives a function, not a number. Then how is the next digit created?

enter image description here

And its trace result ( Plot[functionB2[Sqrt[x]], {x, 0, 1}] // Trace ) seems very unreadable. Interestingly, the explicit process of constructing the function B2. Can anyone show this?

thanks ~ :)

+5
source share
3 answers

SetDelayed acts as a scope scheme. Arguments are localized if necessary. Any variables that explicitly match arguments are bound within this scope, others are not.

 In[78]:= a[x_] := x^2 + b b = x^4; (* the first x^2 is explicitly present and bound to the argument. The x in x^4 present via b is not bound *) In[80]:= a[x] Out[80]= x^2 + x^4 (* this is what you would expect *) In[81]:= a[y] Out[81]= x^4 + y^2 (* surprise *) In[82]:= a[1] Out[82]= 1 + x^4 (* surprise *) 

So what you can do is one of two things:

  • Use Evaluate : functionB2[x_] := Evaluate[model /. fit]; functionB2[x_] := Evaluate[model /. fit];
  • Make model dependency on x explicit:

    In [68]: = model2 [x_] = 4 / Sqrt [3] - a1 / (x + b1) - a2 / (x + b2) ^ 2 - a3 / (x + b3) ^ 4;

    In [69]: = functionB3 [x_]: = model2 [x] /. fit;

    In [85]: = functionB3 [Sqrt [0,2]]

    Out [85] = 2.01415

Edit due to issue update
Due to your definition of function B2, any value of the argument gives the same result as described above:

 In[93]:= functionB2[1] Out[93]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(4.29 + x) In[94]:= functionB2["Even a string yields the same ouput"] Out[94]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(4.29 + x) 

However, this expression contains x and therefore it can get a numerical value if we provide a numerical value for x:

 In[95]:= functionB2["Even a string yields the same ouput"] /. x -> 1 Out[95]= 2.13607 

Well, that’s basically what Plot does. That is why you are still getting the plot.

+5
source

Definition:

 functionB2[x_] := model /. fit 

is a Mathematica instruction to replace all future occurrences of an expression that looks like functionB2[x_] with the result of substituting the argument value for each occurrence of x in the model /. fit expression model /. fit model /. fit . But in model /. fit model /. fit no occurrences of x : the only characters in this expression are model and fit (and, technically, ReplaceAll ). Therefore, the definition returns a fixed result of model /. fit model /. fit , regardless of the argument. Indeed, the definition may simply be:

 functionB2a[] := model /. fit 

If you build functionB2a[] , you will get the same result as you built functionB2[anything] . What for? Because Plot will evaluate this expression when changing the symbol x over the range of the graph. It so happened that model /. fit model /. fit evaluates the expression that includes this character, so you get an exposed graph.

Now consider functionB1 :

 functionB1[x_] = model /. fit 

He also says to replace all occurrences of x on the right side, but this time the right side is evaluated before defining the definition. The result of evaluating model /. fit model /. fit is an expression that contains the character x , so now the definition is sensitive to the argument passed. The end result is as if the function was defined in this way:

 functionB1a[x_] := 4/Sqrt[3]-0.335/(0.435+x)^2+0.347/(0.712+x)^4-0.27/(4.29+x) 

So, if you build functionB1[Sqrt[x]] , the Plot command will see the expression:

 4/Sqrt[3]-0.335/(0.435 +Sqrt[x])^2+0.347/(0.712 +Sqrt[x])^4-0.27/(4.29 +Sqrt[x]) 

Official symbols

When defining definitions using SetDelayed name of the formal argument ( x in this case) is independent of any occurrences of the same character outside the definition. Such definitions can use any other character and still generate the same result. On the other hand, the definitions set using Set (for example, functionB1 ) are based on the result of evaluating the right-hand side containing the same character as the formal argument. This can be a source of subtle errors, since care must be taken not to use characters that accidentally have preexisting lowering values. Using formal characters (described in Letters and letter-like forms ) for argument names can help deal with this problem.

+5
source

You can understand what is happening by trying:

 Table[functionB2[Sqrt[y]],{y,0.5,.5,.5}] Table[functionB2[Sqrt[x]],{x,0.5,.5,.5}] (* {4/Sqrt[3] - 0.335/(0.435+ x)^2 + 0.347/(0.712+ x)^4 - 0.27/(4.29+ x)} {2.03065} *) 

What is being replaced is x inside the definition of function B2, not a formal argument.

Edit

The plot you get is not what you want. Sqrt[x] not taken into account in functionB2[...] , and the implicit x is replaced, as you can see here:

enter image description here

+2
source

Source: https://habr.com/ru/post/888435/


All Articles