Histogram with added density function

8 ビュー (過去 30 日間)
Alexander Tollinger
Alexander Tollinger 2020 年 3 月 29 日
編集済み: Ameer Hamza 2020 年 3 月 29 日
Hey guys,
I am new to Matlab and have a simple question about plots and histograms.
I'd need to create four subplots with each containing a histogram. On every histogram the number of datapoints increases. first histo: 5 datapoints, second histo: 50 datapoints, thrid histo: 500 dp , fourth histo: 5000 dp.
  • The datapoints should have an exponential distribution with mean value = 1 (I used exprnd command).
  • Additionally I need to add a cdf density function to each subplot. (distribution parameter can also be 1)
clc;
clear all;
close all;
rng('default');
datapoints5 = exprnd(1 , [5,1]);
datapoints50 = exprnd(1 , [50,1]);
datapoints500 = exprnd(1 , [500,1]);
datapoints5000 = exprnd(1 , [5000,1]);
plot(cdf,histogram(datapoints5000));
histogram(datapoints500);
histogram(datapoints50);
histogram(datapoints5);
Hope somebody can help me figure this out,
Best regards,
Alex

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 29 日
編集済み: Ameer Hamza 2020 年 3 月 29 日
Try this:
clc;
close all;
rng('default');
datapoints{1} = exprnd(1 , [5,1]);
datapoints{2} = exprnd(1 , [50,1]);
datapoints{3} = exprnd(1 , [500,1]);
datapoints{4} = exprnd(1 , [5000,1]);
for i=1:numel(datapoints)
ax = subplot(2,2,i);
histogram(datapoints{i});
dist = fitdist(datapoints{i}, 'Exponential');
x_points = linspace(0, ax.XLim(2), 100);
Pdf = pdf(dist, x_points);
Cdf = cdf(dist, x_points);
yyaxis right
hold on
plot(x_points, Pdf, 'r', 'LineWidth', 2, 'DisplayName', 'PDF');
plot(x_points, Cdf, 'b', 'LineWidth', 2, 'DisplayName', 'CDF');
legend
end
Result:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeExploration and Visualization についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by