2
$\begingroup$

I have some functions, that I cannot break down to a reasonable MWE, so I'm going to try for an abstract question without providing the code. If need be, I'll include the extra code.

I have some functions that do some stuff. They seem to work when executed directly. When I put them in a Plot it's really slow, because function evaluations can be 1 second each. So I'm making a Table of values for a ListLinePlot, which is quicker (I've tried PerformanceGoal and Mesh and they were all slow). My problem is that I get errors that I cannot reproduce directly/manually. One example is if I run the following code,

maxThresh[findQ2[6]]

I get,

0.172831

But if I run,

Table[maxThresh[findQ2[q1]], {q1, {6}}]

I get,

During evaluation of In[702]:= NMinimize::nrnum: The function value -0.00996218+0.113392 I is not a real number at {x} = {0.516997}. >>

and a whole bunch more of similar errors. So the direct evaluation worked and gave me the right numerical answer, and the Table evaluation gives me an error. I was hoping to gain an understanding as to how these two things are at all different. Does Table not simply loop over the values and substitute in for q1? There is a subtlety here, because running that function with the value 6 works (btw, it works for most values, but 20% fail with errors like these, that I cannot reproduce if I just execute the function directly).

Still haven't got my head around Mathematica substitutions...

$\endgroup$
14
  • 3
    $\begingroup$ Possible source of the problem: "Table effectively uses Block to localize values or variables."[documentation] $\endgroup$
    – Karsten7
    Commented Sep 15, 2016 at 18:03
  • $\begingroup$ Does Table[maxThresh[findQ2[§q1]], {§q1, {6}}] work? $\endgroup$
    – Karsten7
    Commented Sep 15, 2016 at 18:05
  • $\begingroup$ @Karsten7. yes that works! What is this black magic you have introduced me to? $\endgroup$
    – bjorne
    Commented Sep 15, 2016 at 18:06
  • 3
    $\begingroup$ You probably have a global q1 somewhere in your code, that is not localized to a function. By renaming you avoid that unwanted change of that global q1. $\endgroup$
    – Karsten7
    Commented Sep 15, 2016 at 18:31
  • 1
    $\begingroup$ The discussion in (94061) might be useful. $\endgroup$
    – WReach
    Commented Sep 19, 2016 at 13:55

0

Browse other questions tagged or ask your own question.