# Mathematica: Evaluating integral numerically

1. Jul 17, 2012

### Niles

Hi

I am trying to evaluate an integral in Mathematica numerically as I have done many times before, but it simply wont work. To make things easy on everybody, I have attached a notebook with the integral.

I simply can't see what is wrong here. I'd be happy to recieve some help.

Best,
Niles.

#### Attached Files:

• ###### pf.nb
File size:
5.7 KB
Views:
68
2. Jul 17, 2012

### Bill Simpson

NIntegrate requires that every variable involved have been assigned a numeric value. In your notebook f is not assigned any value. That is why NIntegrate is objecting.

Now if I assign f=1 or f=10^3 or ... and then try the notebook I get a different warning. It claims that it is stopping because of loss of precision. This usually means that during all the calculation enough uncertainty has accumulated that there will be zero or only a few bits of precision left.

If I try evaluating lorentzian[f, Sqrt[x^2+ y^2+ 4*z^2]]*gaussian[x, y, z] for x,y,z in the range of -100...100 I find lots of Underflow[] results. That usually means the result is so small that there isn't a negative exponent big enough to describe it.

Does any of this help? Can you, for example write down what result would be when x,y,z are all -100 or 100 and f is whatever it is supposed to be?

Hint: How small is gaussian[100,100,100]==Exp[-3*(10^2)^2*(10^3)^2]?

Last edited: Jul 17, 2012
3. Jul 17, 2012

### Niles

Hi

Thanks for replying so quickly. It does help -- I think I normalized by Gaussian wrongly. I have corrected it now, and even multiplied by a constant (just to see if it works.. I can always divide it out later).

However NIntegrate still fails. I have uploaded my new attempt. What can I do now?

Best,
Niles.

EDIT: Actually the upper/lower limits don't really matter. I just randomly chose 100 because I thought the function was practically zero there. 10 might even have done it.

#### Attached Files:

• ###### pf.nb
File size:
7.5 KB
Views:
61
4. Jul 17, 2012

### Bill Simpson

If you integrate a gaussian from -Infinity to Infinity you get exactly 1, assuming you don't scale this by 10^6 which I do not understand why you are doing. Your first integral is almost exactly that. Integrating over a smaller domain, where you know what the answer should be, is always a good idea so you can check your calculations for correctness.

I'm not certain what your lorentzian integral should give you, but it seems very close to 1.

Your third integral takes forever and gives warnings about convergence, which was what I saw. Integrating between -j and j for j up to 3 rapidly gives a result with no warning. j beyond 3 (far less than your 100) results in warnings that it just isn't finding an answer.

Can you perhaps reformulate your formula to converge much more rapidly to an accurate answer? See if you can find a copy of "Real Computing Made Real" in a library somewhere and read that. That is an excellent book trying to teach people how to pose problems so they get accurate correct answers.

5. Jul 17, 2012

### Niles

The reason for multiplying by 10^6 was because I thought Mathematica was having a difficult time with all the small values.. but I see it doesn't make a difference.

Honestly I'm not sure what is wrong here. Basically we're just adding numbers here, I don't see why NIntegrate finds that so difficult. I'm not sure how to reformulate the ingral.. I mean, it is what it is, but I see why you suggest it.

I'll check out the book, but I'm not optimistic.

Thanks so far.

Best,
Niles.