Integrating The Normal Probability Density Function

Hello community
I was wondering if the community could help me with integration with Matlab.
I am new to matlab so my apologies for the "basic" question.
I have a set of data from which I have calculated the:-
• Mean = 1853.910 • Standard Deviation = 1.829
I want to integrate the normpdf function in Matlab from x = 1855.739 to x = 1852.081 however I am getting silly results, I should be getting an approx value of 68.26%.
I have searched the Matlab directory and found the normpdf M file and amended it so it now looks like:-
y = exp(-0.5 * ((x - 1853.910)./1.829).^2) ./ (sqrt(2*pi) .* 1.829);
Where I have replaced the variables MU and SIGMA with 1853.910 and 1.829.
I cannot seem to integrate this function from x = 1855.739 to x = 1852.081.
Can anyone help?
Its driving me nuts.
Thank you.

 採用された回答

Star Strider
Star Strider 2017 年 10 月 15 日
編集済み: Star Strider 2017 年 10 月 15 日

2 投票

The normcdf function will do the integration for you:
Mean = 1853.910;
Standard_Deviation = 1.829;
lims = [1855.739 1852.081];
cp = normcdf(lims, Mean, Standard_Deviation);
Prob = cp(1) - cp(2)
Prob =
0.6826895
EDIT
If you want to do the integration yourself, this works:
p = @(x,m,s) exp(-((x-m).^2)/(2*s.^2)) / (s*sqrt(2*pi));
c = integral(@(x) p(x, Mean, Standard_Deviation), 1852.081, 1855.739);
The result is the same as ‘Prob’.

6 件のコメント

John hope
John hope 2017 年 10 月 15 日
Star Strider thank you for your response.
After looking at the normcdf function I have realised that it does not give me what I am looking for.
I actually need to integrate the normpdf function because I created a graph by passing in all of my X values (1851.400, 1851.700, 1852.600, ...) along with my mean (1853.910) and standard deviation (1.829) using the normpdf function. It is this normpdf function I am trying to integrate between.
I took your steps but replaced normcdf with normpdf, I got the following:-
>> Mean = 1853.910;
>> Standard_Deviation = 1.829;
>> lims = [1855.739 1852.081];
>> cp = normpdf(lims, Mean, Standard_Deviation);
>> Prob = cp(1) - cp(2)
Prob =
1.643130076445232e-014
As you can see I get a very wired result.
Can you help?
I have attached my X values along with the corresponding Y values from the normpdf function for reference.
Any ideas?
Thanks
Star Strider
Star Strider 2017 年 10 月 15 日
You’re using normpdf, not normcdf as I used in my code. That makes all the difference!
Change to my code, or use my additional code after the ‘EDIT’ text that uses an anonymous function implementation of normpdf), and you will get the correct result.
John hope
John hope 2017 年 10 月 16 日
Hi Star Strider
I know I am being really stupid so my apologies and thanks for putting up with me.
But in your example you have used the function normcdf :-
cp = normcdf(lims, Mean, Standard_Deviation);
But I want to integrate normpdf.
I am not sure what I am missing.
Would it be possbile to explain the line of code and why you have used this:-
cp = normcdf(lims, Mean, Standard_Deviation);
I have taken your additional code:-
p = @(x,m,s) exp(-((x-m).^2)/(2*s.^2)) / (s*sqrt(2*pi));
and altered it to look like:-
p= @(x)exp(-((x-1853.910).^2)/(2*1.829.^2))/(1.829*sqrt(2*pi))
I then used the following code to integrate it:-
>> (int (p,x,1852.081,1855.739))*100
ans =
68.268949213710298306169235885366
So with your help the problem has been solved so thank you but I would still be eager to know why you have used the normcdf function just to learning purposes.
Thanks again.
Star Strider
Star Strider 2017 年 10 月 16 日
My pleasure.
The normcdf function (the cumulative distribution function) is defined as the integral of the normpdf (the probability density function).
To illustrate
cum_prob = integral(@(x) normpdf(x, Mean, Standard_Deviation), 1852.081, 1855.739)
cp =
0.6826895
Integrating the normpdf function gives the same result as using the normcdf function.
The probability density function is just that — the probability at a specific value of the independent variable. Use the integral of that — the cumulative distribution function — to get the probability that a specific event has occurred between the bounds of the integration. See any decent basic statistics text for a full explanation.
If my Answer helped you solve your problem, please Accept it!
John hope
John hope 2017 年 10 月 16 日
Much appreciated answer accepted.
Thanks again.
Star Strider
Star Strider 2017 年 10 月 16 日
As always, my pleasure.

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2017 年 10 月 15 日

コメント済み:

2017 年 10 月 16 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by