Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot area under two intersecting normal curves

Subject: Plot area under two intersecting normal curves

From: Sarah-Jane

Date: 25 Apr, 2013 21:36:07

Message: 1 of 3

Hi,
I am VERY out of practice with MATLAB! I am hoping what I am trying to do is not that complicated - any help would be greatly appreciated!
I have scoured documentation, forums etc and though I am sure the answer is out there I am not understanding it.

I have plotted two Gaussian curves on the same figure. These are both the result of a normpdf() on the same range but different mean and std

I get two lovely curves intersecting slightly.
Now I "just" need to calculate the area of intersection.....and this is what is going very wrong!

The code up to the point of plotting the curves is:
mx256m = [-17747.998047,-17859.041016,-17474.904297,-16872.554688,-18829.910156,-19107.115234,-17471.216797,-19600.185547,-18658.857422,-17313.257812];
imp = [-43974.179688,-28197.365234,-48259.617188,-35682.351562,-38330.378906,-29280.316406,-23040.587891,-22871.777344];

figure('Color',[1 1 1]);
x = -50000:0.1: -14000;

mu256=mean(mx256m);
sigma256=std(mx256m);
Y256pdf=normpdf(x,mu256,sigma256);
plot(x,Y256pdf,'r','LineWidth',2);
hold;


muimp=mean(imp);
sigmaimp=std(imp);
Yimppdf=normpdf(x,muimp,sigmaimp);
plot(x,Yimppdf,'-g','LineWidth',2);


I have looked at trapz() and quad() . I have tried trapz(Yimppdf) etc but the figures I am getting are nonesense.
I am very confused by the onlie docs for these functions. Can anyone tell me how I use them in for the curves I have plotted above and more specifically to get teh intersecting area???

Thank you for your time and ideas!

Subject: Plot area under two intersecting normal curves

From: Teja Muppirala

Date: 26 Apr, 2013 08:22:09

Message: 2 of 3

TRAPZ works by summing things assuming unit step size in the x-direction. So you need to scale the answer by "dx" to get the right answer, or you can call TRAPZ(x,y) and it will do the appropriate scaling for you.

The area of the intersection is basically the area under the minimum of the two curves.
So you could define a function that returns the minimum of the two curves, then evaluate that and call TRAPZ, or you could use INTEGRAL (for newer versions) or QUADGK (for older versions).

With the code below, I get an answer of 0.0593 either way, and that seems reasonable.

mx256m = [-17747.998047,-17859.041016,-17474.904297,-16872.554688,-18829.910156,-19107.115234,-17471.216797,-19600.185547,-18658.857422,-17313.257812];
imp = [-43974.179688,-28197.365234,-48259.617188,-35682.351562,-38330.378906,-29280.316406,-23040.587891,-22871.777344];

figure('Color',[1 1 1]);
x = -50000:0.1: -14000;

mu256=mean(mx256m);
sigma256=std(mx256m);
Y256pdf=normpdf(x,mu256,sigma256);
plot(x,Y256pdf,'r','LineWidth',2);
hold;

muimp=mean(imp);
sigmaimp=std(imp);
Yimppdf=normpdf(x,muimp,sigmaimp);
plot(x,Yimppdf,'-g','LineWidth',2);

% Define a function to be the minimum of the two curves
F = @(x) min( normpdf(x,mu256,sigma256) ,normpdf(x,muimp,sigmaimp) );
area(x,F(x),'linewidth',2); %Draw the shaded area
x_int= -50000:0.1:0; % Just in case, take a wider region that you need
trapz(x_int,F(x_int)) % Call TRAPZ with 2 inputs, so the scaling is applied correctly
integral(F,-50000,0) %<-- Also works

Subject: Plot area under two intersecting normal curves

From: Sarah-Jane

Date: 26 Apr, 2013 10:26:10

Message: 3 of 3

"Teja Muppirala" wrote in message <klddfh$at0$1@newscl01ah.mathworks.com>...
> TRAPZ works by summing things assuming unit step size in the x-direction. So you need to scale the answer by "dx" to get the right answer, or you can call TRAPZ(x,y) and it will do the appropriate scaling for you.
>
> The area of the intersection is basically the area under the minimum of the two curves.
> So you could define a function that returns the minimum of the two curves, then evaluate that and call TRAPZ, or you could use INTEGRAL (for newer versions) or QUADGK (for older versions).
>
> With the code below, I get an answer of 0.0593 either way, and that seems reasonable.
>
> mx256m = [-17747.998047,-17859.041016,-17474.904297,-16872.554688,-18829.910156,-19107.115234,-17471.216797,-19600.185547,-18658.857422,-17313.257812];
> imp = [-43974.179688,-28197.365234,-48259.617188,-35682.351562,-38330.378906,-29280.316406,-23040.587891,-22871.777344];
>
> figure('Color',[1 1 1]);
> x = -50000:0.1: -14000;
>
> mu256=mean(mx256m);
> sigma256=std(mx256m);
> Y256pdf=normpdf(x,mu256,sigma256);
> plot(x,Y256pdf,'r','LineWidth',2);
> hold;
>
> muimp=mean(imp);
> sigmaimp=std(imp);
> Yimppdf=normpdf(x,muimp,sigmaimp);
> plot(x,Yimppdf,'-g','LineWidth',2);
>
> % Define a function to be the minimum of the two curves
> F = @(x) min( normpdf(x,mu256,sigma256) ,normpdf(x,muimp,sigmaimp) );
> area(x,F(x),'linewidth',2); %Draw the shaded area
> x_int= -50000:0.1:0; % Just in case, take a wider region that you need
> trapz(x_int,F(x_int)) % Call TRAPZ with 2 inputs, so the scaling is applied correctly
> integral(F,-50000,0) %<-- Also works


Firstly Teja, thank you so much for your response.
Both trapz and integral are, as you said, returning the same figure.
Can you tell me, if I want to split the area under the curve and calculate the area would the code below make sense?
What I think I am doing is calculating the area to the left of the x-coordinate value -18000 and then the area to the right of that value?
...or am I just dillusional?

x_int= -50000:0.1:-18000;
trapz(x_int,F(x_int))

x_int= -18000:0.1:0;
trapz(x_int,F(x_int))

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us