Need help initializing variable x and plotting probability density function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!