Why does my loop stop after the first iteration?

Hi everyone! I am trying to generate a sample (either a 0 or 1) with certain probability, and then update a Mean formula and Variance formula, both of which are recursive. I will keep generating samples and keep updating the formulas until this condition is met:
if (n>10) && ((Mn_A(A,n)-2*sqrt(Vn_A(A,n)/n)>.05) || (Mn_A(A,n)+2*sqrt(Vn_A(A,n)/n) < 0.5))
disp(['95% confidence interval achieved for A with N = ' num2str(n) ' and width = ' num2str(WidthA)])
break
end
I am getting an error however. It goes through the loop on the n=1 iteration with no problem, but on n=2, I get this error:
Not enough input arguments.
Error in ee497ca2_1>Mn_A (line 14)
if n == 0
Error in ee497ca2_1>Mn_A (line 19)
r = (Mn_A(n-1) + (x1 - Mn_A(n-1)))/n;
Error in ee497ca2_1 (line 4)
Mn_A(A,n)
I think I am missing something on how recursion works, but I am not sure. Here is the whole code below. Thank you!
samples = 500;
for n = 1:samples
A = randsrc(1,1,[0,1;0.49,0.51]);
Mn_A(A,n)
Vn_A(A,n)
ConfidenceInterval_Min_A = Mn_A(A,n) - 2*sqrt(Vn_A(A,n)/n);
ConfidenceInterval_Max_A = Mn_A(A,n) + 2*sqrt(Vn_A(A,n)/n);
WidthA = ConfidenceInterval_Max_A - ConfidenceInterval_Min_A;
if (n>10) && ((Mn_A(A,n)-2*sqrt(Vn_A(A,n)/n)>.05) || (Mn_A(A,n)+2*sqrt(Vn_A(A,n)/n) < 0.5))
disp(['95% confidence interval achieved for A with N = ' num2str(n) ' and width = ' num2str(WidthA)])
break
end
end
function r = Mn_A(x1,n)
if n == 0
r = 0;
elseif n == 1
r = x1;
else
r = (Mn_A(n-1) + (x1 - Mn_A(n-1)))/n;
end
end
function q = Vn_A(x1,n)
if n == 0
q = 0;
elseif n == 1
q = 0;
else
q = Vn_A(n-1) - Vn_A(n-1)/(n-1) + ((x1-Mn_A(n-1))^2)/n;
end
end

 Respuesta aceptada

Voss
Voss el 26 de Abr. de 2022
Editada: Voss el 26 de Abr. de 2022
Mn_A takes 2 input arguments, x1 and n:
function r = Mn_A(x1,n)
But later in the function Mn_A, you call Mn_A with one input:
r = (Mn_A(n-1) + (x1 - Mn_A(n-1)))/n;
% ^^^ ^^^ one input each time
When Mn_A is called with one input, it has an error on its first line:
function r = Mn_A(x1,n) % if only x1 is given, n is undefined, so
if n == 0 % there's an error trying to refer to n here
(Same for Vn_A.)

4 comentarios

Joseph Weiss
Joseph Weiss el 27 de Abr. de 2022
Oh that makes so much sense! Thank you. When I call the recursive function on each iteration, do the Mn_A and Vn_A values remember the value from the previous iteration? I am not sure if I am doing the recursion properly here.
You're welcome!
To answer your question, when any function is called (recursive or not), the function doesn't "remember" or "know" anything except what is passed to it as input arguments (an exception is if you are using global or persistent variables, which you aren't in this case - and you shouldn't need to).
I would focus on just getting Mn_A to work first, and then use analogous logic in Vn_A. Here's a description of how a recursive function to calculate the mean of a sequence might work, with a simple example:
function r = Mn_A(x1,n) % x1 is a sequence of random samples, n is the length of the sequence
if n == 0 % if there are no samples, the mean is 0
r = 0;
elseif n == 1 % is there is only one sample, it is the mean
r = x1;
else
% if there is more than one sample, determine the mean
% somehow, in terms of the mean of a smaller sequence
% e.g., I know your sequences are 0s and 1s, but suppose
% you had the sequence {20,30,70}. Then:
% (step 1) mean({20,30,70}) is (70 + 2*mean({20,30}))/3, where
% (step 2) mean({20,30}) is (30 + mean({20}) )/2, where
% (step 3) mean({20}) is 20, so
% (back to step 2) mean({20,30}) is (30 + 20 )/2 = 25, so
% (back to step 1) mean({20,30,70}) is (70 + 2*25 )/3 = 40
r = (Mn_A(n-1) + (x1 - Mn_A(n-1)))/n; % <- does not (yet) correspond to the above
end
end
You have to figure out what that line should be:
r = (Mn_A(n-1) + (x1 - Mn_A(n-1)))/n;
in order to express the mean of a sequence x1 in terms of the mean of the smaller sequence formed by removing one element from x1, as in the example I step through above.
I think part of the confusion (including confusion on my part) is that in this case, you are iterating and recursing. On each iteration, you would already have the mean of the previous sequence (i.e., without the most recent sample), so you could use that mean combined with the new sample to get the mean of the new sequence (including the most recent sample). Then a recursive function is not required, since you're doing the same thing that a recursive function would be doing, but doing it by storing and using the means of the smaller sequences that are calculated as you iterate.
I would do this either by iterating (calculating the new mean from the old mean and the new sample) or by recursing (given a sequence, let the recursive function figure out the mean), but I wouldn't combine both approaches - you end up doing the same thing over and over when you do that.
Joseph Weiss
Joseph Weiss el 27 de Abr. de 2022
Okay. I think I was confusing myself as well haha, but it makes sense after you explained it. Thank you again for your help it is much appreciated!
Voss
Voss el 29 de Abr. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 26 de Abr. de 2022

Comentada:

el 29 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by