Plotting over a histogram

조회 수: 175 (최근 30일)
Tayyab Khalil
Tayyab Khalil 2021년 4월 23일
댓글: Tayyab Khalil 2021년 4월 24일
Hi all,
So I am trying to plot a normal curve over a histogram but the curve doesn't appear. Here is the code:
clear all, close all, clc
%% Defining the simulation parameters
N = 10000;
M = 30;
alpha = 0.7;
%Finding the uniformly distributed random numbers
phi = unifrnd(-pi, pi,1,N);
%Defining the function for random variable x
x = cos(phi);
%Initializing and Finding the output sequence Y through the difference equation
y = zeros(1, N);
y(1) = x(1);
for i = 2:N
y(i) = y(i-1)*alpha + x(i);
end
figure(1)
edges = [-3:0.2:3];
p2 = histogram(y, edges);
hold on
norm = normpdf(edges, 0,1);
plot(edges, norm)
hold off
Any help would be appreciated, thank you.

채택된 답변

Adam Danz
Adam Danz 2021년 4월 23일
편집: Adam Danz 2021년 4월 23일
You're plotting a normal probability density function which are probabilities between 0 and 1 that sum to 1 (depending on sampling).
Check out your norm data. Note the y value at the peak of the curve.
% from your code
figure
edges = [-3:0.2:3];
norm = normpdf(edges, 0,1);
plot(edges,norm)
Look at the numerical integration which should sum (close to) 1.
trapz(edges,norm)
ans = 0.9972
Now let's put this into your data and increase the curve's line width do you can see it. You'll see the curve squished down at the bottom because it only reaches a height of ~0.4 while your histogram's height is ~750.
% from your code
figure
N = 10000;
M = 30;
alpha = 0.7;
phi = unifrnd(-pi, pi,1,N);
x = cos(phi);
y = zeros(1, N);
y(1) = x(1);
for i = 2:N
y(i) = y(i-1)*alpha + x(i);
end
figure
edges = [-3:0.2:3];
p2 = histogram(y, edges);
hold on
norm = normpdf(edges, 0,1);
plot(edges, norm, 'LineWidth', 4)
hold off
Solutions
histfit
It's unclear what the goal is but if you want to fit the distribution to a normal curve use histfit. Note that histfit only allows you to enter the number of bins rather than the bin edges so the histogram may differ from the one you generated if the bins are not the same.
figure()
hf = histfit(y, 30); % for 30 bins
fitdist
Alternatively you could fit the distribution with your specified bin edges and plot the curve yourself using fitdist. In this example, the amplitude of the normal curve is set to the max bin count.
hcounts = histcounts(y,edges); % get bin counts
pd = fitdist(y(:), 'normal'); % normal distribution params (mean, sigma)
xd = linspace(min(edges),max(edges),100);
yd = max(hcounts)*exp(-(((xd-pd.mean).^2)/(2*pd.sigma.^2))); % new y-values (use any x vals)
figure()
p2 = histogram(y, edges);
hold on
plot(xd, yd, 'r-','LineWidth',3)
  댓글 수: 2
Steven Lord
Steven Lord 2021년 4월 23일
Another option is to display the PDF of the histogram of your data as well as the curve obtained from evaluating the theoretical PDF.
%% Defining the simulation parameters
N = 10000;
M = 30;
alpha = 0.7;
%Finding the uniformly distributed random numbers
phi = unifrnd(-pi, pi,1,N);
%Defining the function for random variable x
x = cos(phi);
%Initializing and Finding the output sequence Y through the difference equation
y = zeros(1, N);
y(1) = x(1);
for i = 2:N
y(i) = y(i-1)*alpha + x(i);
end
figure(1)
edges = [-3:0.2:3];
p2 = histogram(y, edges, 'Normalization', 'pdf');
hold on
norm = normpdf(edges, 0,1);
plot(edges, norm)
hold off
Looks reasonable to me.
Tayyab Khalil
Tayyab Khalil 2021년 4월 24일
Thank You for your contributions. Yes that's exactly what I wanted. Have a good day!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by