# Need help initializing variable x and plotting probability density function

1 view (last 30 days)
Stirling Ellis on 27 Apr 2022
Commented: Stirling Ellis on 27 Apr 2022
Hi, I am currently trying to plot the probability density function (PDF) below.
%Equation and initialized variables
mean = 1930000;
standardDeviation= 64000;
fx = (1/(standardDeviation * sqrt(2 * pi))) * exp(-((x - mean)^2 / (2 * standardDeviation ^ 2)));
In this code, you will notice x is a variable, and is unassigned prior to the code, which obviously presents an error. X is supposed to be a random variable, and the plot of the above function should yield a normally distributed bell curve. I was hoping someone knew how to call x as a randomly assigned variable, as well as plot the PDF to appear as a normal distribution. Thank you in advance!
Torsten on 27 Apr 2022
Edited: Torsten on 27 Apr 2022
Theory, plotting, random number generation and much more:

Paul on 27 Apr 2022
Hi Stirling,
To plot the curve
mean = 1930000;
standardDeviation= 64000;
% note theat ^2 is changed to .^2 for elementwise operation
fx = @(x) (1/(standardDeviation * sqrt(2 * pi))) * exp(-((x - mean).^2 / (2 * standardDeviation ^ 2)));
xmin = mean - 3*standardDeviation;
xmax = mean + 3*standardDeviation;
plot(xmin:xmax,fx(xmin:xmax)) Samples of x can be obtained as
xval = normrnd(mean,standardDeviation,1,5)
xval = 1×5
1.0e+06 * 2.0381 2.0147 1.9156 1.8856 1.9168
Stirling Ellis on 27 Apr 2022
Thank you so much! This was so helpful!