使用 Mathematica 查找 2 个 PDF 函数的第一个交集

发布于 2024-12-21 07:26:15 字数 1331 浏览 2 评论 0原文

在 Mathematica 8.0.1.0 中,我使用 FindRoot[] 来识别两个 2 pdf 函数的交集。

但是,如果我的 pdf 函数在多个点处相交,并且 x 轴范围的上限超出了第二个交点,则 FindRoot[] 仅返回第二个交点。

pdf1 = 1/x 0.5795367855565214` (E^(
  11.170058830053032` (-1.525439351903338` - Log[x]))
   Erfc[1.6962452696714152` (-0.5548887795964352` - Log[x])] + 
 E^(1.2932713057519` (2.60836043407439` + Log[x]))
   Erfc[1.6962452696714152` (2.720730943938539` + Log[x])]);

pdf2 = 1/x 0.4648445097126269` (E^(
  5.17560914275408` (-2.5500941338198615` - Log[x]))
   Erfc[1.7747318880142482` (-2.139288893723375` - Log[x])] + 
 E^(1.1332542415053757` (3.050849516581922` + Log[x]))
   Erfc[1.7747318880142482` (3.1407996592474956` + Log[x])]);

Plot[{pdf1, pdf2}, {x, 0, 0.5}, PlotRange -> All]   (* Shows 1st intersection *)
Plot[{pdf1, pdf2}, {x, 0.4, 0.5}, PlotRange -> All] (* Shows 2nd intersection *)

{x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.5}],
x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.4}]}

在此处输入图像描述 在此处输入图像描述

上图显示了该问题。绘制时,它们在两点相交:

{0.464719, 0.0452777}

分别。

因为我事先无法知道是否会有第二个交叉点,并且我不知道如果这样做的话它可能会落在 x 轴上的哪个位置,所以任何人都可以建议一种方法来实现 FindRoot[] 只返回第一个交集而不返回第二个?

如果没有,有人可以建议另一种方法吗?

With Mathematica 8.0.1.0, I have used FindRoot[] to identify the intersection of two 2 pdf functions.

But if I have pdf functions that intersect at more than one point, and I have the upper limit of the x axis range beyond the second intersection, FindRoot[] only returns the second intersection.

pdf1 = 1/x 0.5795367855565214` (E^(
  11.170058830053032` (-1.525439351903338` - Log[x]))
   Erfc[1.6962452696714152` (-0.5548887795964352` - Log[x])] + 
 E^(1.2932713057519` (2.60836043407439` + Log[x]))
   Erfc[1.6962452696714152` (2.720730943938539` + Log[x])]);

pdf2 = 1/x 0.4648445097126269` (E^(
  5.17560914275408` (-2.5500941338198615` - Log[x]))
   Erfc[1.7747318880142482` (-2.139288893723375` - Log[x])] + 
 E^(1.1332542415053757` (3.050849516581922` + Log[x]))
   Erfc[1.7747318880142482` (3.1407996592474956` + Log[x])]);

Plot[{pdf1, pdf2}, {x, 0, 0.5}, PlotRange -> All]   (* Shows 1st intersection *)
Plot[{pdf1, pdf2}, {x, 0.4, 0.5}, PlotRange -> All] (* Shows 2nd intersection *)

{x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.5}],
x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.4}]}

enter image description here
enter image description here

The above plots show the issue. When plotted they intersect at two points:

{0.464719, 0.0452777}

respectively.

As I can't know before hand if I'll have a second intersection and I don't know where it might fall on the x axis if I did, can anyone suggest a way to have FindRoot[] only return the first intersection rather than the second?

If not, can anyone suggest another way to go about it?

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

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

发布评论

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

评论(2

跨年 2024-12-28 07:26:15

使用FindRoot[],您只能获取给定起点的单个根。迭代不同的选项非常麻烦,并且对于某些边缘情况,您甚至可能无法获得所需的结果,除非您找到了正确的起点选择。

在这种情况下,NSolveReduce 之类的东西可能是更好的选择。如果您知道您的表达式会衰减,则使用 x 的可能值的合理上限,您可以使用以下方法,这非常快,并且会为您提供所有根。

NSolve[{pdf1 == pdf2, 0 < x < 1}, x] // Timing
Out[1]= {0.073495, {{x -> 0.0452777}, {x -> 0.464719}}}

With FindRoot[], you can only get a single root for a given starting point. Iterating through different options is cumbersome and you might not even get the desired result for certain edge cases unless you hit upon the right choice of starting point.

In this case, something like NSolve or Reduce might be a better option. If you know that your expressions decay, using a reasonable upper bound for possible values of x, you can use the following, which is pretty quick and will give you all roots.

NSolve[{pdf1 == pdf2, 0 < x < 1}, x] // Timing
Out[1]= {0.073495, {{x -> 0.0452777}, {x -> 0.464719}}}
腻橙味 2024-12-28 07:26:15

怎么样:

首先你必须一步找到所有的根。我这样做

roots=Reduce[pdf1==pdf2&&0.000001<x<0.5,x]

然后你可以取最小值(在你的特殊情况下x轴上的第一个根)。

rootMin=Min[N[x/.{ToRules[roots]}]]

How about the following:

First you have to find all roots in one step. I do this with

roots=Reduce[pdf1==pdf2&&0.000001<x<0.5,x]

And then you could take the minimum (first root on the x axis in your special case).

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