找到两条高斯曲线的交点

找到两条高斯曲线的交点

本文介绍了Python:找到两条高斯曲线的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个高斯图:

x = np.linspace(-5,9,10000)
plot1=plt.plot(x,mlab.normpdf(x,2.5,1))
plot2=plt.plot(x,mlab.normpdf(x,5,1))

,我想找到两条曲线相交的点.有没有办法做到这一点?特别是我想找到它们相交处的x坐标的值.

and I want to find the point at where the two curves intersect. Is there a way of doing this? In particular I want to find the value of the x-coordinate where they meet.

推荐答案

您要查找x,以使两个高斯函数具有相同的高度.(即相交)

You want to find the x's such that both gaussian functions have the same height.(i.e intersect)

您可以通过将两个高斯函数相等并求解x来实现.最后,您将得到一个二次方程,其系数与高斯均值和方差有关.这是最终结果:

You can do so by equating two gaussian functions and solve for x. In the end you will get a quadratic equation with coefficients relating to the gaussian means and variances. Here is the final result:

import numpy as np

def solve(m1,m2,std1,std2):
  a = 1/(2*std1**2) - 1/(2*std2**2)
  b = m2/(std2**2) - m1/(std1**2)
  c = m1**2 /(2*std1**2) - m2**2 / (2*std2**2) - np.log(std2/std1)
  return np.roots([a,b,c])

m1 = 2.5
std1 = 1.0
m2 = 5.0
std2 = 1.0

result = solve(m1,m2,std1,std2)

输出为:

array([ 3.75])

您可以绘制找到的交叉点:

You can plot the found intersections:

x = np.linspace(-5,9,10000)
plot1=plt.plot(x,mlab.normpdf(x,m1,std1))
plot2=plt.plot(x,mlab.normpdf(x,m2,std2))
plot3=plt.plot(result,mlab.normpdf(result,m1,std1),'o')

情节将是:

如果您的高斯人有多个交集,那么代码也会找到所有的交集(例如m1 = 2.5,std1 = 3.0,m2 = 5.0,std2 = 1.0):

If your gaussians have multiple intersections, the code will also find all of them(say m1=2.5, std1=3.0, m2=5.0, std2=1.0):

这篇关于Python:找到两条高斯曲线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:31