PDF function does not give same result as normpdf

2 vues (au cours des 30 derniers jours)
George Ansari
George Ansari le 21 Août 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 commentaire
George Ansari
George Ansari le 21 Août 2017
Sorry I mean LPDF = [5,200; 9,340; 0,006; 0,167; 0,167; 0,006; 9,340]

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 21 Août 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

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by