Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to take the following integral

NIntegrate[
    ((Sin[Q 1]-Q 1*Cos[Q 1])/(Q 1)^3)^2 
    * S[Q,0.4,2] 
    * Q BesselJ[0,0.5Q]
    , {Q,0,Infinity}]

but it fails with the following error message

First::first: "{} has a length of zero and no first element."

However, if I replaced the 0.5 in the Bessel function with a 1/2

NIntegrate[((Sin[Q 1] - Q 1*Cos[Q 1])/(Q 1)^3)^2 
           * S[Q, 0.4, 2] 
           * Q BesselJ[0, Q/2]
           , {Q, 0, Infinity}]

I get back the result (0.046852).

S[Q,0.4,2] is included below for completeness, but it's mostly just trig functions and polynomials, so I'd be surprised if it was the issue. It certainly doesn't contain a call to first.

Why would the calculation fail for the floating point value but succeed on the rational one? Also, what does any of this have to do with First?

S[Q,0.4,2]==(1+0.095493 (-(0.000127501 (118891. +208412. Q^2-(392972. Q^4-104206. Q^2 (-2+3.93305 Q^2)+4953.78 (24-47.1966 Q^2+15.4689 Q^4)) Cos[1.98319 Q]+2 Q (-117892.-30305.8 Q^2) Sin[1.98319 Q]))/(Q^6 (1+1/Q^60.0000121755 (118891. +208412. Q^2-(392972. Q^4-104206. Q^2 (-2+3.93305 Q^2)+4953.78 (24-47.1966 Q^2+15.4689 Q^4)) Cos[1.98319 Q]+2 Q (-117892.-30305.8 Q^2) Sin[1.98319 Q])))-1/Q^40.00681547 ((-16109.8 Q^2+138.778 (144+31.4644 Q^2)) Cos[1.98319 Q]-4 (-4061.58 Q^2+138.778 (36+7.3277 Q^2)) Cos[2 Q]+8 Q (3217.18 Sin[1.98319 Q]-3259.16 Sin[2 Q]))+(10.0812 (Q^3 Cos[2 Q]-3.6092 (26.0526 +Q^2) Sin[2 Q]))/(678.74 Q+Q^5)))/(1-0.904507 (-(0.000127501 (118891. +208412. Q^2-(392972. Q^4-104206. Q^2 (-2+3.93305 Q^2)+4953.78 (24-47.1966 Q^2+15.4689 Q^4)) Cos[1.98319 Q]+2 Q (-117892.-30305.8 Q^2) Sin[1.98319 Q]))/(Q^6 (1+1/Q^60.0000121755 (118891. +208412. Q^2-(392972. Q^4-104206. Q^2 (-2+3.93305 Q^2)+4953.78 (24-47.1966 Q^2+15.4689 Q^4)) Cos[1.98319 Q]+2 Q (-117892.-30305.8 Q^2) Sin[1.98319 Q])))-1/Q^40.00681547 ((-16109.8 Q^2+138.778 (144+31.4644 Q^2)) Cos[1.98319 Q]-4 (-4061.58 Q^2+138.778 (36+7.3277 Q^2)) Cos[2 Q]+8 Q (3217.18 Sin[1.98319 Q]-3259.16 Sin[2 Q]))+(10.0812 (Q^3 Cos[2 Q]-3.6092 (26.0526 +Q^2) Sin[2 Q]))/(678.74 Q+Q^5)))
share|improve this question
2  
Why are they all Q 1? How did that space get there? –  rm -rf Jun 2 '12 at 3:18
    
They were originally Q R with an implied multiplication, but a substitution of /. R->1 Turned them into Q 1. It's not supposed to be Q1 –  user640078 Jun 3 '12 at 22:32
    
You should update your question with the correct info. –  rm -rf Jun 3 '12 at 22:35
add comment

1 Answer

up vote 4 down vote accepted

Apart from the strange combination of inexact and exact numbers in your definitions, there is an error in the way S is defined. You're using the Equal operator instead of the assignment SetDelayed (:=) (or Set, =).

But after I correct that, the problem seems to be that the numerical integrator has trouble at the preprocessing stage, deciding how to integrate your function in the first place.

To circumvent that, you can help it decide what to do by defining the function S explicitly only for numerical arguments, so that MMA doesn't try to do symbolic calculations on it:

S[Q_?NumericQ] := (1 + 0.095493 ...

where the rest of your definition for S[Q,0.4,2] can be copied. Here, I've removed the ,0.4,2 from the argument list because it's irrelevant for the example, and entered Q_ instead of Q.

The ?NumericQ part makes sure that the function works for arbitrary input Q but only if it's a number.

With that, your integral can proceed:

NIntegrate[((Sin[Q 1] - Q 1*Cos[Q 1])/(Q 1)^3)^2*S[Q]*
  Q BesselJ[0, 0.5 Q], {Q, 0, Infinity}]

It still complains, but those are different issues.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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