Why does my graph get sent to the end of the html when I publish it?
6 次查看(过去 30 天)
显示 更早的评论
I'm using MATLAB R2021b and am trying to format my homework to output the graphs within the document, but when I try to plot something with a function inside it it throws all my graphs at the end of the publication rather than in line where I set it.
Here is my code
%% Module 1 MATLAB Exercise
%% Part 1
% Compute and plot the discrete distribution for a Binomially % distributed
% random variable n where the number of trials N=10 and p=0.7. The MATLAB
% function "nchoosek.m” is helpful.*
N = 10; %Number of Trials
p = 0.7; %Probability
q = 1-p;
k = (0:10); %Random variable from 0 to 10
bar(k,arrayfun(@(x) nchoosek(N, x)*p^x*q^(N-x), k));
snapnow;
%%
% The bar graph is centered around 7 successes and falls off on either
% side and though it is extremely small, there still is a very small
% possibility that there will be 0 successes.
%% Part 2
% In a for loop run M times, perform 10 Bernoulli trials sum to get
% the number of successes for each set of 10 trials. When done, use the
% MATLAB "hist.m" function to plot the tabulated sums with 11 bins
% specified to "hist.m” for binning the the numbers 0 to 10. You should
% normalize the histogram so that it sums to one, i.e., divide by M. Do
% this for M=10, 100, and 1000000, plot and compare.*
%%
% Histogram of 10 Bernoulli Trials
histogram(TenBernoulliTrials(10),11,'BinEdges',0:10,'Normalization','probability');
snapnow;
%%
% At just 10 trials, the total varies greatly, sometimes matching the shape
% of the graph generated in Part 1 pretty well, sometimes looking very
% different. This is because 10 is barely enough trials to look like the
% first graph. There are not enough data points for there to be anything
% that goes beyond barely resembling the graph from part 1. This is because
% there are not enough trials at this point for there to be a clear
% distinction between the different results.
%%
% Histogram of 100 Bernoulli Trials
histogram(TenBernoulliTrials(100),11,'BinEdges',0:10,'Normalization','probability');
snapnow;
%%
% At 100 trials, the shape starts to fit the graph from part 1 much better,
% though there are some minute differences that can be seen in certain
% sections of the histogram.The distribution can clearly be seen, but the
% actual numbers do not have the resolution of the bar graph from part 1.
% This is because there are enough trials at this point for there to be a
% clear distinction between the different results, but not enough to
% accurately define the probability.
%%
% Histogram of 1000000 Bernoulli Trials
histogram(TenBernoulliTrials(1000000),11,'BinEdges',0:10,'Normalization','probability');
snapnow;
%%
% At 1000000 trials, the shape fits nearly exactly with the bar graph
% generated from Part 1. The shape of the graph matches nearly perfectly
% and the magnitudes of the normalized bar graph match the probability
% distribution from Part 1. This is because there are enough trials at this
% point to have a small enough resolution to generate values that are
% comparable to the first part.
%% Bernoulli Trial Function
function SUM = TenBernoulliTrials(M)
SUM = zeros(M,1);
for x = 1:M
for N = 1:10
if rand < 0.7
SUM(x) = SUM(x) + 1;
end
end
end
end
%%
% This is the function used to generate the histographs. It uses a random
% number generator to simulate the 0.7 probability for a success and sums
% the number of successes M number of times which is the output of the
% function.
And here's the ending of what I get when I publish it
I've attached the pdf of the full document
0 个评论
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!