这个积分可以在 Matlab 或 Mathematica 中进行数值计算吗?

发布于 2024-11-30 07:00:42 字数 3444 浏览 3 评论 0原文

我希望能够完全以数值方式进行下面的积分。

equation

其中 nepsilonabbeta 是常量,为简单起见,可以全部设置为 1

对 x 的积分可以通过手动或使用 Mathematica 进行分析,然后对 y 的积分可以使用 NIntegrate 以数值方式完成,但这两种方法给出不同的答案。

分析:

In[160]:= ex := 2 (1 - Cos[x])

In[149]:= ey := 2 (1 - Cos[y])

In[161]:= kx := 1/(1 + Exp[ex])

In[151]:= ky := 1/(1 + Exp[ey])

In[162]:= Fn1 := 1/(2 \[Pi]) ((Cos[(x + y)/2])^2)/(ex - ey)

In[163]:= Integrate[Fn1, {x, -Pi, Pi}]

Out[163]= -(1/(4 \[Pi]))
 If[Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
   y \[NotElement] Reals, \[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
   Log[Cos[y/2]] Sin[y], 
  Integrate[Cos[(x + y)/2]^2/(Cos[x] - Cos[y]), {x, -\[Pi], \[Pi]}, 
   Assumptions -> ! (Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
       y \[NotElement] Reals)]]

In[164]:= Fn2 := -1/(
  4 \[Pi]) ((\[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
     Log[Cos[y/2]] Sin[y]) (1 - ky) ky )/(2 \[Pi])

In[165]:= NIntegrate[Fn2, {y, -Pi, Pi}]

Out[165]= -0.0160323 - 2.23302*10^-15 I

数值方法 1:

In[107]:= Fn4 := 
 1/(4 \[Pi]^2) ((Cos[(x + y)/2])^2) (1 - ky) ky/(ex - ey)

In[109]:= NIntegrate[Fn4, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[109]:= 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. >>

During evaluation of In[109]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 18 recursive bisections in x near {x,y} = {0.0000202323,2.16219}. NIntegrate obtained 132827.66472461013` and 19442.543606302774` for the integral and error estimates. >>

Out[109]= 132828.

数值 2:

In[113]:= delta = .001;
pw[x_, y_] := Piecewise[{{1, Abs[Abs[x] - Abs[y]] > delta}}, 0]

In[116]:= Fn5 := (Fn4)*pw[Cos[x], Cos[y]]

In[131]:= NIntegrate[Fn5, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[131]:= 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. >>

During evaluation of In[131]:= NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained 0.013006903336304906` and 0.0006852739534086272` for the integral and error estimates. >>

Out[131]= 0.0130069

因此,两种数值方法都没有给出 -0.0160323。我明白为什么 - 第一种方法无法处理由分母引起的无穷大,而第二种方法有效地删除了导致问题的积分部分。但我希望能够积分另一个无法简化的积分(比 x、y 和 z 更难的积分)分析地。上面的积分给了我一种测试任何新方法的方法,因为我知道答案应该是什么。

I want to be able to do the integral below completely numerically.

equation

where n, epsilon and a, b and beta are constants which for simplicity, can all be set to 1.

The integral over x can be done analytically by hand or using Mathematica, and then the integral over y can be done numerically using NIntegrate, but these two methods give different answers.

Analytically:

In[160]:= ex := 2 (1 - Cos[x])

In[149]:= ey := 2 (1 - Cos[y])

In[161]:= kx := 1/(1 + Exp[ex])

In[151]:= ky := 1/(1 + Exp[ey])

In[162]:= Fn1 := 1/(2 \[Pi]) ((Cos[(x + y)/2])^2)/(ex - ey)

In[163]:= Integrate[Fn1, {x, -Pi, Pi}]

Out[163]= -(1/(4 \[Pi]))
 If[Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
   y \[NotElement] Reals, \[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
   Log[Cos[y/2]] Sin[y], 
  Integrate[Cos[(x + y)/2]^2/(Cos[x] - Cos[y]), {x, -\[Pi], \[Pi]}, 
   Assumptions -> ! (Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
       y \[NotElement] Reals)]]

In[164]:= Fn2 := -1/(
  4 \[Pi]) ((\[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
     Log[Cos[y/2]] Sin[y]) (1 - ky) ky )/(2 \[Pi])

In[165]:= NIntegrate[Fn2, {y, -Pi, Pi}]

Out[165]= -0.0160323 - 2.23302*10^-15 I

Numerical method 1:

In[107]:= Fn4 := 
 1/(4 \[Pi]^2) ((Cos[(x + y)/2])^2) (1 - ky) ky/(ex - ey)

In[109]:= NIntegrate[Fn4, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[109]:= 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. >>

During evaluation of In[109]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 18 recursive bisections in x near {x,y} = {0.0000202323,2.16219}. NIntegrate obtained 132827.66472461013` and 19442.543606302774` for the integral and error estimates. >>

Out[109]= 132828.

Numerical 2:

In[113]:= delta = .001;
pw[x_, y_] := Piecewise[{{1, Abs[Abs[x] - Abs[y]] > delta}}, 0]

In[116]:= Fn5 := (Fn4)*pw[Cos[x], Cos[y]]

In[131]:= NIntegrate[Fn5, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[131]:= 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. >>

During evaluation of In[131]:= NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained 0.013006903336304906` and 0.0006852739534086272` for the integral and error estimates. >>

Out[131]= 0.0130069

So neither of the numerical methods give -0.0160323. I understand why - the first method has trouble with the infinities caused by the denominator, and the second method effectively deletes the part of the integral that is causing problems. But I'd like to be able to integrate another integral (a harder one over x, y and z) which can't be simplified analytically. The above integral gives me a way to test any new method since I know what the answer should be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北凤男飞 2024-12-07 07:00:42

除非我写错了积分,否则这应该可以完成工作:

n[x_] := 1/(1 + Exp[eps[x]])
eps[x_] := 2(1 - Cos[x])
.25/(2*Pi)^2*NIntegrate[
    Cos[(x + y)/
     2]^2 ((1 - n[y]) n[y] - (1 - n[x]) n[x])/(Cos[y] - Cos[x]),
    {x, -Pi, Pi},
    {y, -Pi, Pi},
    Exclusions -> {Cos[x] == Cos[y]}
  ]

给出0.0130098

好的,我想最快的解释方法是这样的:

在此处输入图像描述

从第一行到第二行我刚刚将第一个 eq 重命名为 x (积分区域是对称的,所以没问题)。然后我将 I 的两个(等价)表达式相加,得到 2I,我进行数值积分就是这样。关键是分子和分母在同一点消失,因此实际上不需要 Exclusions 选项。请注意,在上述方法的草图中,为了简洁(或者由于懒惰,取决于观点),我删除了 1/(4*Pi^2) 。

Unless I wrote down the integral wrong, this should do the job:

n[x_] := 1/(1 + Exp[eps[x]])
eps[x_] := 2(1 - Cos[x])
.25/(2*Pi)^2*NIntegrate[
    Cos[(x + y)/
     2]^2 ((1 - n[y]) n[y] - (1 - n[x]) n[x])/(Cos[y] - Cos[x]),
    {x, -Pi, Pi},
    {y, -Pi, Pi},
    Exclusions -> {Cos[x] == Cos[y]}
  ]

giving 0.0130098.

OK, I guess the quickest way to explain is like this:

enter image description here

going from the first to the second line of the first eq I've just renamed y to x (the region of integration is symmetric so it's OK). Then I have added the two (equivalent) expressions for I to obtain 2I, and what I numerically integrate is that. The point is that the numerator and denominator vanish at the same points, so in fact the Exclusions option isn't needed. Note that in my sketch of the method above I dropped the 1/(4*Pi^2) for brevity (or due to laziness, depending on the viewpoint).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文