Inner loop not working

13 visualizaciones (últimos 30 días)
Tasneem Raihan
Tasneem Raihan el 28 de Feb. de 2017
Comentada: Jan el 28 de Feb. de 2017
Hi, I am trying to run the following code with a nested loop. It seems that for each value of i the inner loop works only once, that is only for k=0 and does not run for the rest of the values of k. Any advice will be highly appreciated. If you need any further information, please let me know.
N.B. I deliberately avoided writing out the full question that I am trying to solve since it's a homework question and I don't want specific answers to be publicly available.
function summa=my_func(n)
t=linspace(0,4*pi, 1001);
len=length(t);
summa=zeros(1,len);
b=0;
for i=1:1001
for k=0:n
numerat=((-1)^k)*sin((2*k+1)*t(i));
denomat= ((2*k)+1)^2;
b=b+ (numerat/denomat);
end
summa(i)=summa(i)+b;
end
  5 comentarios
Walter Roberson
Walter Roberson el 28 de Feb. de 2017
Are you sure that you want b to accumulate over all i values? Perhaps b should initialize inside the i loop?
Tasneem Raihan
Tasneem Raihan el 28 de Feb. de 2017
@Walter thanks for your response. I want b to accumulate for each i. Actually, I need to sum up all the different ratios of numerat and denomat that are produced for different values of k. I hope I am not confusing you.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 28 de Feb. de 2017
Your code does run. You can use the debugger to check this: Set a breakpoint inside the loop (the red dots on the left), then Matlab stops there at the execution. See Matlab: Debug. Now step through the code line by line and examine the local variables in the WorkSpace-browswer or in the command window.
Because your assumption "only for k=0 and does not run for the rest" is not correct, but you do not explain, why you assume this, it is difficult to suggest an improvement. Without knowing, what the code should calculate, I cannot guess if it contains a bug.
Perhaps "b=0;" should appear inside the loop. "summa(i) = b" should be sufficient, because summa is predefined with zeros.
  2 comentarios
Tasneem Raihan
Tasneem Raihan el 28 de Feb. de 2017
Hi Jan,
Thank you so much for checking my code. I apologize for making the goof here in the first place. The inner loop was indeed working. I was misled to believe that it wasn't while running the debugger for n=0. I feel embarrassed now. But your last piece of advice is what I actually needed...it worked like a magic. Can't thank you enough for pointing out my mistake. You rock!
Jan
Jan el 28 de Feb. de 2017
@Tasneem: You are welcome and your questions are welcome here also. There is not need to apologize, because this forum is thought for asking questions. And it is typical for questions, that they look trivial, when they are solved. :-)

Iniciar sesión para comentar.

Más respuestas (1)

Chad Greene
Chad Greene el 28 de Feb. de 2017
Editada: Chad Greene el 28 de Feb. de 2017
Perhaps you need to move the b=0 after i=1:1001:
for i=1:1001
b=0;
for k=0:n
  1 comentario
Tasneem Raihan
Tasneem Raihan el 28 de Feb. de 2017
Thanks a lot for your suggestion Chad. It has solved my problem. You are a star :)

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by