Group Abstract Group Abstract

Message Boards Message Boards

0
|
9K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

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

Posted 10 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 10 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
Posted 10 years ago
POSTED BY: Jack Straton

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

POSTED BY: Frank Kampas
Posted 10 years ago
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