Passing function variable inside Sum[... NIntegrate[...

I need help overcoming a peculiarity in Mathematica (7 and 9) in which a function containing a numerical integral inside a sum does not access a function variable. If I define

sumnint[T_] := 
 Sum[cn[n] NIntegrate[ 
     Exp[- an[n] x T] /. {an[1] -> 1.0`, an[2] -> 2.0`}, {x, 
      0, \[Infinity]}], {n, 1, 2}] /. {cn[1] -> 1.0`, cn[2] -> 2.0`}

then

sumnint[T] /. T β†’ 1

yields an unevaluated result

NIntegrate[
  Exp[-an[n] x] /. {an[1] -> 1., an[2] -> 2.}, {x, 0, \[Infinity]}]

although

sumnint[1]

properly yields

(Analytic integration gives cn[1]/an[1] + cn[2]/an[2], I.S.Gradshteyn and I.M.Ryzhik,Table of Integrals,Series,and Products
5ed (Academic,New York,1994),p.732,No.3.351.3.)

But if I reverse the order of summation and integration I get the evaluated answer. For

nintsum[T_] := 
NIntegrate[ 
Sum[cn[n] Exp[- an[n] x T] /. {an[1] -> 1.0`, an[2] -> 2.0`}, {n, 1,
  2}] /. {cn[1] -> 1.0`, cn[2] -> 2.0`}, {x, 0, \[Infinity]}]

nintsum[T] /. T -> 1

gives

This is a simple example of a much more complicated integral I am doing in which I will want to substitute a whole series of values of variables like T for which the /. evaluation form is much easier for my purposes than copying and pasting values into the function variable list. Can you see a workaround?

It would be easier to diagnose if you had put your code into a code sample window. In the text form, it looks like the symbol for infinity is wrong. It should be [Infinity].

Sorry for confusing you. I see no option for inserting a window of any sort. The infinity symbol when copied as input gets pasted as " [Infinity]". What follows is just the input (in plane text) and output (in quotes):

sumnint[T_] :=
Sum[cn[n] NIntegrate[
Exp[- an[n] x T] /. {an1 β†’ 1.0, an[2] -> 2.0}, {x,
0, [Infinity]}], {n, 1, 2}] /. {cn1 β†’ 1.0, cn[2] -> 2.0}

sumnint[T] /. T β†’ 1

β€œ3. NIntegrate[
Exp[-an[n] x] /. {an1 β†’ 1., an[2] β†’ 2.}, {x, 0, [Infinity]}]”

sumnint1

β€œ2.”

Sum[cn[n] NIntegrate[
Exp[- an[n] x T] /. {an1 β†’ 1.0, an[2] -> 2.0, T β†’ 1}, {x,
0, [Infinity]}], {n, 1, 2}] /. {cn1 β†’ 1.0, cn[2] -> 2.0}

β€œ2.”

nintsum[T_] :=
NIntegrate[
Sum[cn[n] Exp[- an[n] x T] /. {an1 β†’ 1.0, an[2] -> 2.0}, {n, 1,
2}] /. {cn1 β†’ 1.0, cn[2] -> 2.0}, {x, 0, [Infinity]}]

nintsum[T] /. T β†’ 1

β€œ2.”

β€œBelow is an image of the worksheet:”

The first icon, the one that looks like a rectangle with <> inside it, is for code samples.

(Restated using code lines.) If one defines a function containing a numerical integral inside a sum,

sumnint[T_] := 
 Sum[cn[n] NIntegrate[ 
     Exp[- an[n] x T] /. {an[1] -> 1.0`, an[2] -> 2.0`}, {x, 
      0, \[Infinity]}], {n, 1, 2}] /. {cn[1] -> 1.0`, cn[2] -> 2.0`}

then the following attempt to send the function the value for T of 1

sumnint[T] /. T -> 1

yields an unevaluated integral as output,

3. NIntegrate[
  Exp[-an[n] x] /. {an[1] -> 1., an[2] -> 2.}, {x, 0, \[Infinity]}]

though the T in the integral is replaced by 1.
On the other hand, simply copying the 1 into the argument

sumnint[1]

gives a fully evaluated result, 2. (Analytic integration gives cn[1]/an[1] + cn[2]/an[2], I.S.Gradshteyn and I.M.Ryzhik,Table of Integrals,Series,and Products \ 5ed (Academic,New York,1994),p.732,No.3.351.3.)
However, if the sum is inside the numerical integral, this problem does not appear:

 nintsum[T_] := 
 NIntegrate[ 
  Sum[cn[n] Exp[- an[n] x T] /. {an[1] -> 1.0`, an[2] -> 2.0`}, {n, 1,
      2}] /. {cn[1] -> 1.0`, cn[2] -> 2.0`}, {x, 0, \[Infinity]}]

followed by

nintsum[T] /. T -> 1

gives a gives a fully evaluated result, 2.

This is a simple example of a much more complicated, deeply nested integral I am doing in which I will want to substitute a whole series of values of variables like T for which the /. evaluation form is much easier for my purposes than copying and pasting values into a function variable list. Can you see a way to get the numerical integral to evaluate even when calling it using

sumnint[T] /. T -> 1

?

As you define it

In[1]:= sumnint[T_] := 
 Sum[cn[n] NIntegrate[
     Exp[-an[n] x T] /. {an[1] -> 1, an[2] -> 2}, {x, 
      0, \[Infinity]}], {n, 1, 2}] /. {cn[1] -> 1, cn[2] -> 2}


In[6]:= sumnint[1]
Out[6]= 2.

In[10]:= sumnint[q] /. q -> 1
During evaluation of In[10]:= NIntegrate::inumr: The integrand E^(-q x) has evaluated to non-numerical values for all sampling points in the region with boundaries {{\[Infinity],0.}}. >>
During evaluation of In[10]:= General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>
Out[10]= 3 NIntegrate[Exp[-an[n] x] /. {an[1] -> 1, an[2] -> 2}, {x, 0, \[Infinity]}]

Mathematica stops working after the NIntegrate::inumr message. That’s all. One way to circumvent that is to define

  sumnint[T_?NumericQ] := <snip>

which makes good sense if one is going to use a numeric function inside sumnint.

The other way is to keep the function undefined too, with other words

In[16]:= sumnint[T_] := (Sum[
     cn[n] X[Exp[-an[n] x T] /. {an[1] -> 1, an[2] -> 2}, {x, 
        0, \[Infinity]}], {n, 1, 2}] /. {cn[1] -> 1, cn[2] -> 2}) /; UnsameQ[T, x]

In[17]:= sumnint[1] /. X -> NIntegrate
Out[17]= 2.

In[18]:= sumnint[q] /. {X -> NIntegrate, q -> 1}
Out[18]= 2.

In[19]:= sumnint[q] /. {q -> 1, X -> NIntegrate}
Out[19]= 2.

The second workaround worked nicely even for my complicated version. Many thanks, Jack