0
$\begingroup$

This is probably very simple but I don't see the solution now. Let us say I have a variable e defined as

e=n

ReplaceAll can subsitute n with value 3 using

e/.n->3

The problem is when e is given by an integral of some function f[{n_}, r_] with n as a parameter

e=Integrate[f[{n}, r],{r, rmin, rmax}]

Then the use of

e/.n->3

will take too long if the integral in question is hard to evaluate and so, better would be FIRST to replace n with its numerical value and THEN evaluate the integral.

If e was defined as a function

e[n_]:=Integrate[f[{n}, r],{r, rmin, rmax}]

there would be no problem. But my function f depends on quite many definitions (using "n") I made earlier. It works ok but then in the end of my code there is a complicated f which makes the integration (i.e. evaluation of "e") too long - and I don't want to change all the definitions I made before.

What to do if I want to keep "e" as above, i.e. I don't want to define it as a function?

$\endgroup$
4
  • $\begingroup$ Oh, sure, you're right, I used another symbol, I forgot about that. Thanks, edited. $\endgroup$
    – wondering
    Jan 28, 2015 at 23:51
  • $\begingroup$ If you are not making functions, and n is eventually replaced by a number, why not simply define n beforehand? E.g. n=3;e=n. $\endgroup$ Jan 29, 2015 at 1:28
  • $\begingroup$ @2012rcampion Right, I should have noted that. I cannot use your solution because I will need to evaluate e for several values of n, (e.g. using Table). $\endgroup$
    – wondering
    Jan 29, 2015 at 1:35
  • $\begingroup$ This question may be a duplicate of: (3864) $\endgroup$
    – Mr.Wizard
    Jan 29, 2015 at 2:05

1 Answer 1

5
$\begingroup$

Update

The first method I recommended does not work properly. I fooled myself because I have a $PrePrint definition by default. However following your update I think you do not need ReplaceAll. Instead try Block:

e := Integrate[1/(x^3 + 1), {x, 0, n}];

Block[{n = 2.0}, e]
1.09

Not the use of SetDelayed to keep the definition from evaluating early.

It happens that Table uses a Block-like mechanism(1) therefore you can also use:

Table[e, {n, 1`, 4`, 1`}]
{0.835649, 1.09, 1.15445, 1.17814}

I recommend that you also read:

$\endgroup$
6
  • $\begingroup$ +1. But, two or more levels of Unevaluated usually signal (to me), that Unevaluated is generally the wrong tool for the job (not in this particular case). $\endgroup$ Jan 29, 2015 at 0:25
  • $\begingroup$ Thanks. I corrected the $E$ and added some motivation on why I do not want to define a function. To your answer - for some reason I am not gettin "1.09" as output but rather $\text{Unevaluated}\left[\int_0^{2.} \frac{1}{x^3+1} \, dx\right]$ Why is that? $\endgroup$
    – wondering
    Jan 29, 2015 at 1:13
  • $\begingroup$ @wondering You're right, my result only appears because I am using a $PrePrint definition. I forgot to test this in a default configuration. Sorry. Let me see what I can do. $\endgroup$
    – Mr.Wizard
    Jan 29, 2015 at 1:49
  • $\begingroup$ @wondering Please see the update. $\endgroup$
    – Mr.Wizard
    Jan 29, 2015 at 1:59
  • $\begingroup$ @Mr.Wizard Thanks, this works. Actually this solution (with SetDelayed + Table came to my mind but because I have two parameters m and n from which only n one takes several values, m only one, I would have had anyway to define m beforehand as 2012rcampion suggested (or to make a Table[e,{n,nmin,nmax},{m,m0,m0}], but that is not very good). I think the best solution for this situation is a combination of your two suggestions, with Block and Table, i.e.: Block[{m=1}, Table[e, {n, 1, 5}]] +1 $\endgroup$
    – wondering
    Jan 29, 2015 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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