having trouble with for loops

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 13 de Feb. de 2014

0 votos

out = 1;
for ii = 2:1000
out = out + ii^2;
end

3 comentarios

Joseph
Joseph el 13 de Feb. de 2014
SumN=0; % initializes N^2
for N=1:1000
SumN = SumN + N^2;
end
Still not giving me correct answer...Don't know what I'm doing wrong
Thomas
Thomas el 13 de Feb. de 2014
what answer do u expect?
Joseph
Joseph el 13 de Feb. de 2014
i got it thanks

Iniciar sesión para comentar.

Más respuestas (2)

Roger Stafford
Roger Stafford el 13 de Feb. de 2014

0 votos

Here's a way to check your answer:
n = 1000;
s = n*(n+1)*(2*n+1)/6;
This gives you the same value as
s = sum((1:n).^2);
Alonso Mukendi Kalonji
Alonso Mukendi Kalonji el 29 de Oct. de 2022

0 votos

a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)

Categorías

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

Etiquetas

Preguntada:

el 13 de Feb. de 2014

Respondida:

el 29 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by