Message Boards Message Boards

0
|
7414 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

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

Posted 9 years ago

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

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

although

sumnint[1]

properly yields

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.)*

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

2.

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?

POSTED BY: Jack Straton
6 Replies
Posted 9 years ago

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

POSTED BY: Jack Straton

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.
POSTED BY: Udo Krause

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

POSTED BY: Frank Kampas
Posted 9 years ago

(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

?

POSTED BY: Jack Straton
Posted 9 years ago

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] /. {an[1] -> 1.0, an[2] -> 2.0}, {x, 0, [Infinity]}], {n, 1, 2}] /. {cn[1] -> 1.0, cn[2] -> 2.0}

sumnint[T] /. T -> 1

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

sumnint[1]

"2."

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

"2."

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

"2."

"Below is an image of the worksheet:" image of worhskeet

POSTED BY: Jack Straton

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].

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract