1
$\begingroup$

Integrate is adding a tiny imaginary error to an easy result. Why? And how can I stop it?

gauMix[x_, means_, vars_] := 
  Total[(E^-(((x - means)^2)/(2*vars)))/Sqrt[2*Pi*vars]]/Length[means];
means = {-7, 7};
vars = {6, 6.5};
f[x_] := gauMix[x, means, vars];
fxx = Integrate[f[x]*(x^2), {x, -Infinity, Infinity}]


(* 55.25 - 3.16199*10^-14 I  *)

If I change vars to {6,6} everything is fine. And NIntegrate hasn't made this error yet, that I've seen. But I want to use Integrate instead of NIntegrate so I can get an exact, analytical answer.

$\endgroup$
  • $\begingroup$ How tiny? Try Chop. $\endgroup$ – Kuba Jan 28 '15 at 23:41
  • $\begingroup$ @Kuba I could try that, but I worry about WHY it's adding the error. Is it a symptom of some other error? $\endgroup$ – Jerry Guern Jan 28 '15 at 23:48
  • 3
    $\begingroup$ Not addressing why you see this particular behavior, but if you want an exact analytic answer, you need exact inputs. 6.5 is not an exact number. $\endgroup$ – chuy Jan 29 '15 at 0:39
  • 2
    $\begingroup$ @JerryGuern He means this. Google for "mathematica inexact number". 65/10 would be considered exact. Integrate doesn't play well with inexact numbers. $\endgroup$ – Szabolcs Jan 29 '15 at 4:38
  • 2
    $\begingroup$ Perhaps because these are not full answers? See also, meta.mathematica.stackexchange.com/questions/1457/… $\endgroup$ – Sjoerd C. de Vries Jan 29 '15 at 16:00
8
$\begingroup$
gauMix[x_, means_, vars_] := Total[(E^-(((x - means)^2)/(2*vars)))/Sqrt[2*Pi*vars]]/ Length[means];
means = {-7, 7};
vars = {6, 65/10};
f[x_] := gauMix[x, means, vars];
fxx = Integrate[f[x]*(x^2), {x, -Infinity, Infinity}]

(* 221/4 *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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