PDF function does not give same result as normpdf

조회 수: 2 (최근 30일)
George Ansari
George Ansari 2017년 8월 21일
답변: Star Strider 2017년 8월 21일
Hello all, I'm using the following function to create the PD of a RV
function [ X, f ] = Normdist( mu, sigma, min_x, max_x, n )
X = zeros(n,1);
x = min_x;
dx = (max_x - min_x)/n;
for k = 1:n
X(k) = x;
f(k) = 1/sqrt(2*pi*sigma)*exp(-(x-mu)^2/(2*sigma));
x = x+dx;
end
end
I call it as follows:
[LRV, LPDF] = Normdist(0, 2.5, -10, 10, 7);
LRV = [-10; -7,142; -4,285; -1,428; 1,428; 4,285; 7,14]
but when I call:
A = normpdf(linspace(-10,10,7),0,2.5)
I get:
A = [5,353; 0,004; 0,065; 0,159; 0,065; 0,004; 5,353]
what is wrong with the function? George.
  댓글 수: 1
George Ansari
George Ansari 2017년 8월 21일
Sorry I mean LPDF = [5,200; 9,340; 0,006; 0,167; 0,167; 0,006; 9,340]

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 8월 21일
You need to calculate ‘x’ differently (so that it creates the same interval that linspace does), and square ‘sigma’ in the denominator of the exp argument:
X = zeros(n,1);
f = zeros(n,1);
x = min_x;
dx = (max_x - min_x)/(n - 1);
for k = 1:n
X(k) = x;
f(k) = 1/(sqrt(2*pi)*sigma)*exp(-(x-mu)^2/(2*sigma^2));
x = x+dx;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Hypothesis Tests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by