Search code examples
matlabnormal-distribution

Find intersection of two normal distributions in Matlab


I just started working in Matlab and am having some trouble in understanding how it all works.

I am trying to return the intersection of two Gaussian distributions with mean 0 and 1 and variance 0.5. I use the following code to generate the two distributions:

mu1 = 0;
mu2 = 1;

sigma1 = sqrt(0.5);
sigma2 = sqrt(0.5);

dist1 = makedist('normal',mu1,sigma1);
dist2 = makedist('normal',mu2,sigma2);

I found that in the documentation that you can generate a probability density functionwith the following code:

pdf(dist1,[range])

The problem is that I don't really have a range, I just want to find the intersection of these two distributions. What would be the best approach for this in Matlab?


Solution

  • I understood that you wanted the intersection point, rather than the area under both of the curves. If that is accurate, it's easier to use anonymous functions,

    mu1 = 0;
    mu2 = 1;
    sigma1 = sqrt(0.5);
    sigma2 = sqrt(0.5);
    dist1 =@(x) exp(-(x-mu1).^2 / (2*sigma1^2)) / sqrt(2*sigma1^2*pi);
    dist2 =@(x) exp(-(x-mu2).^2 / (2*sigma2^2)) / sqrt(2*sigma2^2*pi);
    fzero(@(x) dist1(x) - dist2(x), rand * (mu1 - mu2) + (mu1 + mu2))
    
        0.500