【问题标题】:Can this integral be done numerically in either Matlab or Mathematica?这个积分可以在 Matlab 或 Mathematica 中以数值方式完成吗?
【发布时间】:2023-04-05 08:06:02
【问题描述】:

我希望能够完全用数字进行积分。

其中、 和、 和 是常量,为简单起见,都可以设置为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

数值方法一:

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。我理解为什么——第一种方法对分母造成的无穷大有问题,而第二种方法有效地删除了导致问题的积分部分。但我希望能够整合另一个积分(比xyz 更难的积分),它无法通过分析简化。上面的积分为我提供了一种测试任何新方法的方法,因为我知道答案应该是什么。

【问题讨论】:

  • 与问题没有直接关系,但如果您学会了在mathematica中定义函数,它会很有用(即,它会为您节省一些精力)
  • 在您的第一种方法中,您似乎已将解析积分的条件结果剪切并粘贴到您的数字结果中。坏主意,因为条件相当明确地排除了在 -pi 到 pi 的范围内对 y 进行积分。
  • @Verbeia 不,这是另一个积分(不会消失)
  • @Daniel 如果我使用 PrincipalValue -> True,条件是 $y$ 介于 $-\pi$ 和 $\pi$ 之间,是否适合使用?

标签: matlab wolfram-mathematica numerical-integration


【解决方案1】:

除非我写下积分错误,否则应该可以:

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

【讨论】:

  • 这是一个绝妙的主意@acl ...如果我做积分,我会得到 0.0130098,而不是 0.0170457,尽管困倦也可能是我这边的一个因素。
  • @Calvin 不,我可能以某种微不足道的方式搞砸了。我明天再看看
  • 我找到了 - 我使用了自己的代码,所以我错过了你的 eps 和分母与我的相差 2 倍。
  • @Calvin 哎呀对不起,你是对的!让我解决这个问题。好吧,他们现在同意了。所以,题外话了,但是,这大概是涉及二维正方形晶格、玻色子和一阶微扰理论的东西?
  • +1 用于整合酷感。不要破坏一个愉快的聚会,但我们不是有点偏离 Mathematica 作为一种编程语言吗?这一切似乎都朝着数学或物理 SE 的方向发展。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 2015-11-13
  • 1970-01-01
  • 2014-03-26
  • 2023-03-21
相关资源
最近更新 更多