Need help initializing variable x and plotting probability density function

조회 수: 1 (최근 30일)
Stirling Ellis
Stirling Ellis 2022년 4월 27일
댓글: Stirling Ellis 2022년 4월 27일
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개)

Paul
Paul 2022년 4월 27일
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

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by