1
$\begingroup$

I would like to evaluate an integral I am taking in cylindrical coordinates, in which the integrand contains a previously defined function. I need to pass the r and phi integrand values to this function so that it can be evaluated. However I get the

"integrand has evaluated to non-numerical values for all sampling point errors"

when I do this. If I simply pass numbers to my function call in the integrand, it works fine. Here is the code, any help much appreciated!

Defined function:

SinsoidFluc[height_, factor_, scale_, x_, y_] := 
 Which[x > 2*Pi, SinsoidFluc[height, factor, scale, x - 2*Pi, y],
  x < -2*Pi, SinsoidFluc[height, factor, scale, x + 2*Pi, y],
  y > 2*Pi, SinsoidFluc[height, factor, scale, x, y - 2*Pi],
  y < -2*Pi, SinsoidFluc[height, factor, scale, x, y + 2*Pi], 
  x >= -2*Pi && x <= 2*Pi && y >= -2*Pi && y <= 2*Pi, 
  height - factor*Sin[x*scale]*Sin[y*scale]]

Would like to evaluate

PotentialCalc[height_, factor_, scale_, distance_] := 
 1/(4*Pi)*NIntegrate[
   SinsoidFluc[height, factor, scale, r*Cos[phi], r*Sin[phi]]*
    distance*r/(r^2 + distance^2)^1.5, {r, 0, Infinity}, {phi, 0, 
    2*Pi}]

If I change the integrand to another defined functions, I still get the error.

    Hole[x_, y_, radius_, holefraction_] := 
 Which[x^2 + y^2 <= radius, holefraction, x^2 + y^2 > radius, 1]
HolePotential[x_?NumberQ, y_?NumberQ, radius_?NumberQ, 

  holefraction_?NumberQ, pitch_?NumberQ] := 
 Hole[x - Round[x, pitch], y - Round[y, pitch], radius, holefraction]

PotentialCalc[potential_, factor_, radius_, pitch_, distance_] := 
 1/(4*Pi)*NIntegrate[
   HolePotential[r*Cos[phi], r*Sin[phi], potential, radius, factor, 
     pitch]*distance*r/(r^2 + distance^2)^1.5, {r, 0, Infinity}, {phi,
     0, 2*Pi}]
$\endgroup$
  • $\begingroup$ I cannot reproduce the error. PotentialCalc[2, 1, 1, 1] returns 1. without error. BTW, do you know about Mod[x, 2*Pi]? In particular Sign[x] Mod[Abs[x], 2 Pi] might replace the recursion in SinsoidFluc and speed up the computation. $\endgroup$ – Michael E2 Apr 9 '15 at 18:38
  • $\begingroup$ Thanks Michael, I agree that passing hard values for x,y works. What I want to do is to integrate over a spatially varying potential, I get the error when I replace 1,1 with rCos[phi],rSin[phi]. Thanks for the recursion tip, I saw that on this forum to begin with, ill replace it as well. $\endgroup$ – daFireman Apr 9 '15 at 19:20
  • $\begingroup$ Defining as SinsoidFluc[height_?NumberQ,...] will keep it effectively a black box function. So the minimization code will not attempt any symbolic processing that gives rise to such messages. $\endgroup$ – Daniel Lichtblau Apr 9 '15 at 19:41
  • $\begingroup$ Sorry, what does that mean exactly? If I want to pass the integrand parameters to the SinsoidFluc function, I would need to redefine the input parameters for that function as ?NumberQ ? $\endgroup$ – daFireman Apr 9 '15 at 19:50
  • $\begingroup$ Solution! Changing the poor recursion to Michael's Sign/Mod product suggestion seems to have solved the issue. Not sure why, but definitely a more elegant solution, thanks! $\endgroup$ – daFireman Apr 9 '15 at 20:22

Your Answer

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

Browse other questions tagged or ask your own question.