Plotting over a histogram
163 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tayyab Khalil
am 23 Apr. 2021
Kommentiert: Tayyab Khalil
am 24 Apr. 2021
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.
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 23 Apr. 2021
Bearbeitet: Adam Danz
am 23 Apr. 2021
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)
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 Kommentare
Steven Lord
am 23 Apr. 2021
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.
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!