Message Boards Message Boards

Calculate a numerical integration with NIntegrate?

GROUPS:

Dear all, I'm struggling calculating a numerical integral. Here you can find attached (and also written in the post) two notebooks: R1 (where I used Integrate) and R2 (where I used Nintegrate). I think that R1 does not end the notebook evaluation because the integral is probably not solvable analytically, while R2 gives me a warning about the convergence and a wrong final result.

What I'm after

  • The result for the integrals (variable R) should be 0.5
  • When an integral is not analytically solvable (and then I suppose that I have to use Nintegrate), is it still possible to make the integral symbol appear in the code editor? In such a way to have more readable equations (especially when they are really long).

I'm sorry for bothering you with this stupid beginner question, but I tried to read the documentation but I didn't manage to solve my issue. Thanks a lot in advance for your help.

Here is R_1:

ClearAll["Global`*"];
n2 = 1;
n1 = 1.38;

Rs[\[Theta]_] = (
  n1*Cos[\[Theta]] - n2*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2))/(
  n1*Cos[\[Theta]] + n2*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2));

Rp[\[Theta]_] = (-n2*Cos[\[Theta]] + 
   n1*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2))/(
  n2*Cos[\[Theta]] + n1*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2));

R[\[Theta]_] = (Rs[\[Theta]] + Rp[\[Theta]])/2;

c1 = \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(\[Pi]/
     2\)]\(R[\[Theta]]*\ Sin[\[Theta]]*
    Cos[\[Theta]] \[DifferentialD]\[Theta]\)\) ;

c2 = \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(\(-\[Pi]\)/
     2\), \(0\)]\(R[\[Theta]]*\ Sin[\[Theta]]*
\*SuperscriptBox[\((Cos[\[Theta]])\), \(2\)] \[DifferentialD]\[Theta]\
\)\) ;

R = (3 c2 + 2 c1)/(3 c2 - 2 c1 + 2)

Here is R_2:

enter code In[47]:= ClearAll["Global`*"];
n1 = 1.38;
n2 = 1;
Rs[\[Theta]_] = 
  Abs[((n1*Cos[\[Theta]] - n2*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2))/(
    n1*Cos[\[Theta]] + n2*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2)))]^2;

In[51]:= Rp[\[Theta]_] = 
  Abs[((n1*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2) - n2*Cos[\[Theta]])/(
    n1*\[Sqrt](1 - (n1/n2*Sin[\[Theta]])^2) + n2*Cos[\[Theta]]))]^2;

In[52]:= R[\[Theta]_] = (Rs[\[Theta]] + Rp[\[Theta]])/2;

In[53]:= c1 = 
 NIntegrate[
  R[\[Theta]]* Sin[\[Theta]]*Cos[\[Theta]], {\[Theta], 0, \[Pi]/2}, 
  MaxRecursion -> 100]

During evaluation of In[53]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

Out[53]= 0.256811

In[54]:= c2 = 
 NIntegrate[
  R[\[Theta]]* Sin[\[Theta]]*(Cos[\[Theta]])^2, {\[Theta], -(\[Pi]/2),
    0}, MaxRecursion -> 100]

During evaluation of In[54]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

Out[54]= -0.12405

In[55]:= R = (3*c2 + 2*c1)/(3*c2 - 2*c1 + 2)

Out[55]= 0.12697
Attachments:
POSTED BY: Giovanni Iacucci
Answer
16 days ago

The functions Rs and Rp become complex for \[Theta]0 = ArcSin[50/69]. You can insert that value into the integration interval as {\[Theta], 0, \[Theta]0, \[Pi]/2}, which silences the messages. You are using the symbol R with two different meanings, which may not be a good idea. The. I am afraid NIntegrate does not have a special notation.

POSTED BY: Gianluca Gorni
Answer
16 days ago

Dear Gianluca, thanks a lot for your reply. If a priori you do not know if the integral is solvable analytically do you suggest in any case to use directly Nintegrate or to try before with Integrate? Are the warning messages that we silenced somehow "dangerous" for the result of the calculated integral?

POSTED BY: Giovanni Iacucci
Answer
15 days ago

I am not an expert in integration. In your case for the integrals without Abs I could find an analytical primitive, but it was not easy to use, because it has a discontinuity in the imaginary part. Still, I think you can find an exact result in that case, with some patience. If you are content with a floating-point result, NIntegrate can be more than enough. I think that your warnings come because the algorithm somehow senses the singular point, but you have to study every case on its own.

POSTED BY: Gianluca Gorni
Answer
15 days ago

Group Abstract Group Abstract