PDF function does not give same result as normpdf

2 Ansichten (letzte 30 Tage)
George Ansari
George Ansari am 21 Aug. 2017
Beantwortet: Star Strider am 21 Aug. 2017
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 Kommentar
George Ansari
George Ansari am 21 Aug. 2017
Sorry I mean LPDF = [5,200; 9,340; 0,006; 0,167; 0,167; 0,006; 9,340]

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 21 Aug. 2017
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

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by