why is my code is not working?
Show older comments
% Binomial distribution
binom_var1 = binornd(n1, p1, [1, 10]);
binom_var2 = binornd(n2, p2, [1, 10]);
% Geometric distribution
geom_var1 = geornd(p1, [1, 10]);
geom_var2 = geornd(p2, [1, 10]);
% Poisson distribution
poiss_var1 = poissrnd(λ1, [1, 10]);
poiss_var2 = poissrnd(λ2, [1, 10]);
% Binomial PMF and CDF
binom_pmf1 = binopdf(0:n1, n1, p1);
binom_pmf2 = binopdf(0:n2, n2, p2);
binom_cdf1 = binocdf(0:n1, n1, p1);
binom_cdf2 = binocdf(0:n2, n2, p2);
% Geometric PMF and CDF
geom_pmf1 = geopdf(0:max(geom_var1), p1);
geom_pmf2 = geopdf(0:max(geom_var2), p2);
geom_cdf1 = geocdf(0:max(geom_var1), p1);
geom_cdf2 = geocdf(0:max(geom_var2), p2);
% Poisson PMF and CDF
poiss_pmf1 = poisspdf(0:max(poiss_var1), λ1);
poiss_pmf2 = poisspdf(0:max(poiss_var2), λ2);
poiss_cdf1 = poisscdf(0:max(poiss_var1), λ1);
poiss_cdf2 = poisscdf(0:max(poiss_var2), λ2);
% Plot binomial distribution PMF and CDF
figure;
subplot(2,2,1);
stem(0:n1, binom_pmf1);
xlabel('k');
ylabel('P(X=k)');
title(['Binomial Distribution, p=', num2str(p1), ', n=', num2str(n1)]);
subplot(2,2,2);
stem(0:n2, binom_pmf2);
xlabel('k');
ylabel('P(X=k)');
title(['Binomial Distribution, p=', num2str(p2), ', n=', num2str(n2)]);
subplot(2,2,3);
stairs(0:n1, binom_cdf1);
xlabel('k');
ylabel('F(X=k)');
title(['Binomial Distribution, p=', num2str(p1), ', n=', num2str(n1)]);
subplot(2,2,4);
stairs(0:n2, binom_cdf2);
xlabel('k');
ylabel('F(X=k)');
title(['Binomial Distribution, p=', num2str(p2), ', n=', num2str(n2)]);
% Plot geometric distribution PMF and CDF
figure;
subplot(2,1)
Answers (2)
Vilém Frynta
on 2 May 2023
0 votes
You cannot use special symbols for variables. You can only use
- numbers
- letters
- underscore ( _ )
Rename your variable.
1 Comment
Rik
on 2 May 2023
More strictly speaking: function names and variable names must match the regular expression [a-zA-Z][a-zA-Z0-9_]* and be shorter than namelengthmax (which is currently 63 and is not expected to change any time soon).
Walter Roberson
on 2 May 2023
poiss_var1 = poissrnd(λ1, [1, 10]);
The λ is valid in MATLAB under the following circumstances:
- inside a % comment
- inside a %{ %} block comment
- after a ... that marks a line continuation (everything after that on the line is a comment)
- inside a ' ' character vector
- inside a " " string() literal
It is not valid in any portion of a variable name or function name.
If you are trying to indicate that you want lambda to be 1 in the call to poissrnd then use
poiss_var1 = poissrnd(1, [1, 10]);
with no λ character.
Categories
Find more on Poisson Distribution in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!