# Integral with unreliable result

I want to calculate $\int_R^1 \sqrt{r} |\cos((k+\frac{1}{2})\pi r)|dr$ and I get a result from Mathematica. Then I try to check the result putting the value of $k$ and $R$, (k=1 and R=0.5) in the result and performing a NIntegrate with the same value and the result is different. In the analytical result if you put $k=10$ and $R=0.5$ the result is negative and of course wrong, but if I use Nintegrate the result is posivite. What happened? I am interested in the following function of $R$ and $k$.

$$\text{Assuming}\left[R>0\textrm{&&}k>1\textrm{&&}R<1\textrm{&&}k\in\textrm{Integers},\int_0^R \sqrt{r} \textrm{Abs}\left[\cos\left(k+\frac{1}{2}\right)\pi r\right] dr\right]$$ and Mathematica gives the answer $$\frac{2 S\left(\sqrt{2 k R+R}\right) \sec \left(\pi k R+\frac{\pi R}{2}\right)-2 \sqrt{2 k R+R} \tan \left(\pi k R+\frac{\pi R}{2}\right)}{\pi (2 k+1)^{3/2} \sqrt{\sec ^2\left(\pi k R+\frac{\pi R}{2}\right)}}$$

and if you evaluate this function in $R=0.2$ and $k=1$ the result is $-0.0488018$. This result is different from the definite integral with the same value for the parameters. Thank you in advance again

• Is this a mathematics question or a Mathematica question? If the former, move to another forum. If the latter, please paste actual Mathematica code. – murray Aug 3 '14 at 17:19
• there is a problem with the analytical result given by mathematica. No problem with mathematics. I was editing the formula before the negative vote – Raffaele Carlone Aug 3 '14 at 17:27
• As to why you were asked for the actual code, it is easy to copy and paste the code from Mathematica into the question, and doing so makes it easy for those who would help you to copy from the question and paste into Mathematica. Occasionally, there is the benefit of someone spotting an error in the code. But mainly I would say that including code in questions like these is about being nice to those who would help you. – Michael E2 Aug 3 '14 at 17:59
• I tried but the the code was not readable – Raffaele Carlone Aug 6 '14 at 15:44
• Hi, try copying the cell (Edit > Copy As... > InputText, although just plain copying often works). In the SE edit window, paste it. Select it. Press the code button {} above the edit window. Save edit. Do not try to convert it to TeX. – Michael E2 Aug 6 '14 at 17:55

I get the same result from NIntegrate and Integrate.

Integrate[Sqrt[r] Cos[(k + 1/2) Pi r], {r, R, 1},
Assumptions -> R > 0] /. {k -> 10, R -> 0.5}
NIntegrate[Sqrt[r] Cos[(10 + 1/2) Pi r], {r, 0.5, 1}]


Output:

0.0459518

0.0459518

I assume that |...| means Abs[...].

Define the symbolic integral.

int[r0_?NumericQ, k_?NumericQ] := Integrate[Sqrt[r] Abs[Cos[(k + 1/2) \[Pi] r]], {r, r0, 1}]


Define the numerical integral.

intN[r0_?NumericQ, k_?NumericQ] := NIntegrate[Sqrt[r] Abs[Cos[(k + 1/2) \[Pi] r]], {r, r0, 1}]


Compare these integrals for your chosen parameter values, using 1/2 rather than 0.5 to force symbolic evaluation in int[1/2, 10].

{intN[##], {N[#], #} &@int[##]} &[1/2, 10]

(* {0.269865,
{0.269865,
(1/(441 \[Pi]))(21 + 12 Sqrt[35] + 4 Sqrt[231] + 4 Sqrt[273] +
4 Sqrt[357] + 4 Sqrt[399] - 2 Sqrt[21] FresnelS[Sqrt[21/2]] +
4 Sqrt[21] FresnelS[Sqrt[11]] - 4 Sqrt[21] FresnelS[Sqrt[13]] +
4 Sqrt[21] FresnelS[Sqrt[15]] - 4 Sqrt[21] FresnelS[Sqrt[17]] +
4 Sqrt[21] FresnelS[Sqrt[19]] - 2 Sqrt[21] FresnelS[Sqrt[21]])}} *)


The results are numerically the same, and you've got a symbolic expression to use as well.

• I agree but please read my redit question – Raffaele Carlone Aug 6 '14 at 14:57