A novice's problem, perhaps: it seems that /. substitution does weird things to the scope.

In[1]:= alpha := -0.6 + 0.05*k /. k -> 1

In[2]:= mu[k_] := Integrate[x^alpha*x^k, {x, 0, 1}]

In[3]:= Table[mu[k], {k, 0, 2}]

Out[3]= {2.5, 0.689655, 0.4}

In[4]:= Table[mu[p], {p, 0, 2}]

Out[4]= {2.22222, 0.689655, 0.408163}

Why does Table iterated over k produce different results from Table iterated over p? And what exactly does Table over kcompute?

| improve this question | | | | |
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Dec 24 '14 at 14:36
  • 2
    $\begingroup$ Do not paste In[], Out[] to your question it it makes it easier to copy your code. $\endgroup$ – user9660 Dec 24 '14 at 14:38

The reason is that in the first table (Table[mu[k], {k, 0, 2}]) the rule substitution (ReplaceAll) does not happen and k in alpha is always take whatever k came from Table.

to check that try:

alpha := -0.6 + 0.05*1

and then

Table[mu[k], {k, 0, 2}]
(*{2.22222, 0.689655, 0.408163}*)

if you want to go for more details, check in your example this:

Table[mu[k], {k, 0, 2}]//Trace

Table[mu[p], {p, 0, 2}]//Trace
| improve this answer | | | | |
  • $\begingroup$ Thank you for your answer. Trace outputs lots of stuff I don't know how to interpret yet, unfortunately. Also, I have noticed that if I replace := with = in the definition of alpha, the problem goes away. Why is that so? $\endgroup$ – Pastafarianist Dec 26 '14 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.