필터 지우기
필터 지우기

why is my code is not working?

조회 수: 4 (최근 30일)
Albandari Al-Manaa
Albandari Al-Manaa 2023년 5월 2일
댓글: Rik 2023년 5월 2일
% 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]);
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
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)

답변 (2개)

Vilém Frynta
Vilém Frynta 2023년 5월 2일
You cannot use special symbols for variables. You can only use
  • numbers
  • letters
  • underscore ( _ )
Rename your variable.
  댓글 수: 1
Rik
Rik 2023년 5월 2일
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
Walter Roberson 2023년 5월 2일
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.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by