使用 Mathematica 查找 2 个 PDF 函数的第一个交集
在 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}]}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
FindRoot[]
,您只能获取给定起点的单个根。迭代不同的选项非常麻烦,并且对于某些边缘情况,您甚至可能无法获得所需的结果,除非您找到了正确的起点选择。在这种情况下,
NSolve
或Reduce
之类的东西可能是更好的选择。如果您知道您的表达式会衰减,则使用x
的可能值的合理上限,您可以使用以下方法,这非常快,并且会为您提供所有根。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
orReduce
might be a better option. If you know that your expressions decay, using a reasonable upper bound for possible values ofx
, you can use the following, which is pretty quick and will give you all roots.怎么样:
首先你必须一步找到所有的根。我这样做
然后你可以取最小值(在你的特殊情况下x轴上的第一个根)。
How about the following:
First you have to find all roots in one step. I do this with
And then you could take the minimum (first root on the x axis in your special case).