Info

This question is closed. Reopen it to edit or answer.

Error: Subscript indices must either be real positive integers or logicals

2 views (last 30 days)
The following code produces the error in the title:
syms x;
% Alkuarvoja
n = 100;
P = 0.02;
lambda = n*P; % Poissonin parametri
% Tiheysfunktiot
b(x) = nchoosek(n,x) * P^x * (1-P)^(n-x); % Binomi
p(x) = (lambda^(x))/(exp(-lambda) * factorial(x)); % Poisson
% Todennäköisyydet
Pb = 0; % Binomi
Pp = 0; % Poisson
% Binomilaskenta
for i = 0:1:2
Pb = Pb + b(i);
end
% Poissonin approksimaatio
for i = 0:1:2
Pp = Pp + P(i);
end
Pp = 1 - Pp;
Pb = vpa(Pb);
Pp = vpa(Pp);
disp(Pb)
disp(Pp)
Any idea what might be causing it?

Answers (1)

Walter Roberson
Walter Roberson on 24 Sep 2016
You have
for i = 0:1:2
Pp = Pp + P(i);
end
MATLAB is case sensitive, so the P there refers to
P = 0.02;
not to
p(x) = (lambda^(x))/(exp(-lambda) * factorial(x)); % Poisson
We recommend avoiding using different variables that differ only in uppercase / lowercase: you can certainly do that and sometimes there are good reasons to do it, but unless there are good reasons the practice is more likely to lead to exactly this kind of confusion.

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!