1
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ If you don't specify a value for λ, NIntegrate[] won't work. $\endgroup$ Commented Nov 4, 2017 at 19:44
  • $\begingroup$ 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}] $\endgroup$
    – Will.Mo
    Commented Nov 4, 2017 at 20:32
  • $\begingroup$ How could ıI do this? $\endgroup$
    – umos
    Commented Nov 4, 2017 at 20:55
  • $\begingroup$ @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. $\endgroup$
    – Will.Mo
    Commented Nov 5, 2017 at 7:02
  • $\begingroup$ 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}}. $\endgroup$
    – user53311
    Commented Nov 5, 2017 at 11:45

1 Answer 1

1
$\begingroup$

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

Your code:

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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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