Need help initializing variable x and plotting probability density function
Show older comments
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!
1 Comment
Theory, plotting, random number generation and much more:
Answers (1)
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)
1 Comment
Stirling Ellis
on 27 Apr 2022
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!