1

I want to carry out a numeric integration using Mathematica. The integrand is

I=Exp[-z^2]F[z]^2

where this F[z] is defined as Integrate[1/(q+I) Exp[-c(q+z)^2],{q,-Infinity,0}]. Since Mathematica does not know how to carry out the Integration, it has to be carried out numerically. I have to change the integration into 'NIntegrate'. It seems to me that Mathematica refuses to do numerical integration over another numerical integration. The reason I am not using NIntegrate over multi-variables is that the actual integrand is long and complicated which involve F[z].

I also wonder if we could define our own special function like the build-in function in Mathematica. Since error function is an integration and Mathematica does not complain when Numerical integrate over error function. Can I transform the F[z] here like a built-in function?

Many Thanks

1 Answers1

3
In[1]:= c = 2; (* make sure all variables have constant values assigned *)
F[z_?NumericQ] := NIntegrate[1/(q + I) Exp[-c (q + z)^2], {q, -Infinity, 0}];
NIntegrate[Exp[-z^2] F[z]^2, {z, 1, 3}]

Out[3]= 0.00387755 + 0.0794878 I
Bill
  • 3,664
  • 1
  • 12
  • 9
  • Thanks Bill, your proposal works. The only concern is the efficiency. If I define error function as erf[z_?NumericQ]:= Sqrt[Pi]/2 NIntegrate[Exp[-t^2],{t,0,z}], and NIntegrate this function from -infinity to infinity, the time this takes is 4.5s. But If I use the built-in Erf, the whole integration takes 0.056s. I need to minimize the time as much as possible. I have also tried defining F-function as F[z_?NumericQ]:=F[z]=... which avoid duplicate calculation, but still the actual calculation takes too long. – Huijie Guan Sep 20 '13 at 00:30
  • Functions compiled into the Mathematica kernel will often be 10-100 times faster than user defined functions. If you really need speed then you might try optimizing or Simplify your function. There is a Compile function in Mathematica, but that is not something a novice is going to easily figure out how to use. Or you might write your own optimized integration of F in C code outside of Mathematica, compile that with your C compiler and make a minimum number of calls to that. None of those are good. Perhaps starting over with a new question on speed, showing your function, would be best. – Bill Sep 20 '13 at 03:55