# Using NIntegrate with constants

Is there any way to use the NIntegrate function with constants?

f = Exp[-Sqrt[x^2 + y^2 + z^2]/λ]*Cos[π*x/a]*Cos[π*y/b]*Cos[π*z/c]
d = Laplacian[f, {x, y, z}, "Cartesian"] // Simplify
ren = -2/Sqrt[x^2 + y^2 + z^2]
s = d + ren


The above ' s ' is the function I would like to integrate.

• If you don't specify a value for λ, NIntegrate[] won't work. Commented Nov 4, 2017 at 19:44
• You also need bounds for the integral. Maybe you want a function of the constants that does the integral? Then try something like sFcn[x_, y_, z_, a_, b_, c_, λ_] = s; and J[a_, b_, c_, λ_] := NIntegrate[ sFcn[x, y, z, a, b, c, λ], {x, 0, 1}, {y, 0, 1}, {z, 0, 1}] Commented Nov 4, 2017 at 20:32
• How could ıI do this?
– umos
Commented Nov 4, 2017 at 20:55
• @umos Use the 'at' symbol to identify who you are speaking to. Try executing the code I posted above, and see if it does what you want. If you have further questions, try to be more specific. Commented Nov 5, 2017 at 7:02
• The codes you have given gives me the error as I try to evaluate function J NIntegrate::inumr: The integrand -(2/Sqrt[x^2+y^2+z^2])+(E^(-(Sqrt[x^2+y^2+z^2]/[Lambda]_)) (2 [Pi] x Cos[([Pi] y)/Pattern[<<2>>]] Cos[([Pi] z)/Pattern[<<2>>]] a_ b_^2 c_^2 [Lambda]_ Sin[([Pi] x)/Pattern[<<2>>]]+Cos[([Pi] x)/Pattern[<<2>>]] (<<1>>)))/(Sqrt[x^2+y^2+z^2] a_^2 b_^2 c_^2 [Lambda]_^2) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1},{0,1},{0,1}}. Commented Nov 5, 2017 at 11:45

As @J.M. said, NIntegrate doesn't work when the integrand is non-numerical. You also must specify numerical bounds. You can build a function that returns a result for arbitrarily specified numerical constants as follows

f = Exp[-Sqrt[x^2 + y^2 + z^2]/λ]*Cos[π*x/a]*Cos[π*y/b]*Cos[π*z/c];
d = Laplacian[f, {x, y, z}, "Cartesian"] // Simplify;
ren = -2/Sqrt[x^2 + y^2 + z^2];
s = d + ren;


My code:

sFcn[x_, y_, z_, a_, b_, c_, λ_] = s;
J[a_, b_, c_, λ_] :=
NIntegrate[
sFcn[x, y, z, a, b, c, λ], {x, 0, 1}, {y, 0, 1}, {z, 0,
1}];


Example execution:

J[1, 1, 1, 1]


-2.36029

The reason it works is that the constants are all numerical when NIntegrate is finally called. If you insist on non-numerical input, e.g. J[a, 1, 1, 1], you will get the error again (NIntegrate::inumr).