Search code examples
matlabplotmatlab-figurenormal-distribution

How to build overlapping normal distributions in matlab


I built two normal distributions. I tried to combine both into a single image, with the two images overlapping partially and both being the same size and fitting on the same axes. here's what I did:

x = [-2.5:.1:2.5];
norm = normpdf(x,0,1);
y = [-2.5:.1:2.5];
horm = normpdf(y,-1.5,1);
plot(x,norm);
hold on;

then before the next line of code, I manually changed the x axis limits -4 to 4;

plot(y,horm);

now my problem is as follows: for some reason, the distribution "norm" gets plotted fine, but only about 70% of the distribution "norm" gets plotted, by this I mean that the leftmost 30% of the distribution is totally missing. Any ideas why this is happening?

I took a picture of it:

enter image description here


Solution

  • Here's what you looking for:

    x = linspace(-2.5,2.5,50);
    norm = normpdf(x,0,1);
    y = linspace(-4,2.5,50);
    horm = normpdf(y,-1.5,1);
    horm_start = find(horm>norm(1),1);
    horm_end = find(horm>norm(end),1,'last');
    plot(x,norm,y(horm_start:horm_end),horm(horm_start:horm_end));
    

    Which gives:

    two curves