# Math Help - Matlab while statement problem

1. ## Matlab while statement problem

I'm trying to write a simple program that computes the sum of the series 1^2 + 2^2 + 3^2... such that the sum is as large as possible without exceeding 1000. I need to use a while loop in this program. Here's my code so far:

clear;
number=0;
sum_vector=[];
End_flag=0

while End_flag==0;
number=number+1
square=number.^2
sum_vector(number)=square
sum=sum(sum_vector)
if sum>=1000
End_flag=1;
end
end

So my basic idea here was to place each number squared into an array, then sum the array and check to see if it makes the sum condition (<= 1000) and if not go up and plug the next number into the array etc. But this doesn't work and I'm not sure why. Any pointers?

Thanks,
Kim

2. Originally Posted by Kim Nu
I'm trying to write a simple program that computes the sum of the series 1^2 + 2^2 + 3^2... such that the sum is as large as possible without exceeding 1000. I need to use a while loop in this program. Here's my code so far:

clear;
number=0;
sum_vector=[];
End_flag=0

while End_flag==0;
number=number+1
square=number.^2
sum_vector(number)=square
sum=sum(sum_vector)
if sum>=1000
End_flag=1;
end
end

So my basic idea here was to place each number squared into an array, then sum the array and check to see if it makes the sum condition (<= 1000) and if not go up and plug the next number into the array etc. But this doesn't work and I'm not sure why. Any pointers?

Thanks,
Kim
Well for a start do not use the name of a function for a variable also, it may be legal but it is bad practice.

Also you do not need an array with the squares, you just need a running total of the sum of squares, try something like:

Code:

SumOfSquares=0
Num=0;

while Num<1000

work=SumOfSquares+(Num+1)*(Num+1);
if work>1000
break
end
SumOfSquares=work;
Num=Num+1;

end
CB

3. Hey Captain Black,

Thanks alot for the help. I've just been introduced to while statements in Matlab and the only instruction I've received so far has been to start and stop the while statements using flags. But your solution looks alot better. I'm also not familiar with the "break" statement you put in your if statement. Is this condition what stopped the while statement?

Kim

4. Originally Posted by Kim Nu
Hey Captain Black,

Thanks alot for the help. I've just been introduced to while statements in Matlab and the only instruction I've received so far has been to start and stop the while statements using flags. But your solution looks alot better. I'm also not familiar with the "break" statement you put in your if statement. Is this condition what stopped the while statement?

Kim
Yes, a break statement imediatly terminates a while or for loop.

type "help break" at the command prompt or look break up in the online documentation.

CB

5. Or using flags:

Code:

SumOfSquares=0;
Num=0;
flg=0;

while flg==0

Num=Num+1;
SumOfSquares=SumOfSquares+Num*Num;
if SumOfSquares>1000
flg=1
end

end

SumOfSquares=SumOfSquares-Num*Num;
Num-Num-1;
Not as neat since the real test is now done at the top of the loop abd so we have to remove the last term from the answer after we exit

CB

6. CaptainBlack,

You're the best. Just out curiosity, could the program be written with my original idea of summing an array and then proceeding or stopping the while loop?

Kim

7. Originally Posted by Kim Nu
CaptainBlack,

You're the best. Just out curiosity, could the program be written with my original idea of summing an array and then proceeding or stopping the while loop?

Kim
Yes, but there is no point all you ever look at is the sum so far. Also continually extending the matrix is a very slow process, it would be better if you cound pre declare it (1000 elements will allways be adequate).

CB