# How to include conditional statements in NIntegrate?

I have a function g which is the result of another integrated function f. This function (f) has some singularities for some input values. While integrating function f I want to tell NIntegrate to take some value say p at singularities.

f[x1_, x2_, t_, ra_] = Sin[x1]*ra +
ra*Cos[x2]*ArcTan[x1*ra + t*x2,
ra*t + x1*x2];


This function has singularities for ArcTan[0,0].

g[x1_, x2_, t_] :=
With[{ff = f[x1, x2, t, ra]},
NIntegrate[ff, {ra, 0, 10^(-3)},
Exclusions -> ArcTan[__, __] == 0]];


My actual code is about 300 lines long. The above problem might help me to solve my actual problem in Mathematica. Kindly anyone help me.

EDITED

I have function "f" inside this function I have many h1[ArcTan[exp1, exp2]] , h2[ArcTan[exp3, exp4]],....functions (may be more than 50 of ArcTan's are there). For some input values exp1, exp2, exp3, exp4, exp... goes to 0. I applied

f /. ArcTan[0, 0] -> p


at the end of the function. I was thinking When I when I substitute the input values inside f1 it will have ArcTan[0,0] and that gets replaced with "p".But it is not doing that. Is there a way to Replace ArcTan[0,0] with "p" .Then this f1 function is called for another function g where it is integrated.

• I have updated the answer taking into account your "EDITED" section. – Alexey Popkov Oct 14 at 7:13

Please read carefully the Documentation pages for Exclusions and NIntegrate: your understanding of this option is wrong. It doesn't allow to specify a specific value at exclusions as you apparently think. NIntegrate is supposed to automatically handle singularities and all what you can do is to help it a bit by specifying them via this option. The correct usage in your case would be

Exclusions -> {x1*ra + t*x2 == 0 && ra*t + x1*x2 == 0}


If you really do not see a better way than providing a specific value for you function at singularity points (what is a bad idea in general) you could try to turn your objective function into a Piecewise or a black-box function with the specific value p for those points:

NIntegrate[Piecewise[{{p, x1*ra + t*x2 == 0 && ra*t + x1*x2 == 0}},
f[x1, x2, t, ra]], {ra, 0, 10^(-3)}]


or (if the previous doesn't work)

fBlackBox[x1_?NumericQ, x2_?NumericQ, t_?NumericQ, ra_?NumericQ] :=
Piecewise[{{p, x1*ra + t*x2 == 0 && ra*t + x1*x2 == 0}}, f[x1, x2, t, ra]];
NIntegrate[fBlackBox[x1, x2, t, ra], {ra, 0, 10^(-3)}]


You can also replace AcrTan with a Piecewise function having value p at the singular point:

arcTan[x_, y_] := Piecewise[{{p, x == 0 && y == 0}}, ArcTan[x, y]]
f[x1_, x2_, t_, ra_] := Sin[x1]*ra + ra*Cos[x2]*arcTan[x1*ra + t*x2, ra*t + x1*x2];


Of course, you can also define it via Condition but I'm not sure whether NIntegrate will handle such a definition correctly, so you probably should define f as a black-box function in this case as I do below:

arcTan[x_, y_] /; x == 0 && y == 0 := p
arcTan[x_, y_] := ArcTan[x, y]
fBlackBox[x1_?NumericQ, x2_?NumericQ, t_?NumericQ, ra_?NumericQ] :=
Sin[x1]*ra + ra*Cos[x2]*arcTan[x1*ra + t*x2, ra*t + x1*x2];

• hi, thanks for the reply. I tried this way it did not work though. f[x1_, x2_, t_, ra_] = Sin[x1]*ra + raCos[x2]*ArcTan[x1*ra + tx2, ra*t + x1*x2] /; ArcTan[,] == ArcTan[0,0] -> p; Is there a way to write it using Condition function – Gummala Navneeth Oct 13 at 16:47
• @GummalaNavneeth I'm sorry, but your code ArcTan[, ] == ArcTan[0, 0] just demonstrates that you don't understand even the basics of the Wolfram Language. You should learn the basics of the Wolfram Language. I recommend the book by Leonid Shifrin as a quick start. It won't take much of your time. Please learn it before asking new questions. – Alexey Popkov Oct 13 at 18:28
• @GummalaNavneeth And Yes, it is possible to write it via Condition, although the ways I showed in the answer should be better. But in any case, without understanding of the basics you won't be able to go further with this language. – Alexey Popkov Oct 13 at 18:42
• @Alexwy Popkov Sorry for the trouble caused. I will go through that book. Thanks – Gummala Navneeth Oct 13 at 20:46
• @Alexwy Popkov Thanks a lot – Gummala Navneeth Oct 14 at 9:51