MATLAB: Help with Homework please Loops are hard

loopssummation

Use a while-end loop in a script file to calculate the sum of the first n terms of the series:
Summation sign on the top n bottom k=1 : ((-1)^k * k^2 +5k)/3^k Show the script file and the two results of n = 10 and n = 20.
WHAT IS THIS I DONT UNDERSTAND LOOPS HELP PLEASE IM BEGGING!!!. x.x

Best Answer

So, in the first case, you have to add 10 numbers together. In the second case, you have to add 20 numbers together. I'm sure you know how to add numbers together, so don't be intimidated by the loop.
In my opinion, the natural way to do this sum is with a "for" loop:
total = 0;
for k=1:n
total = total + ((-1)^k * k^2 +5*k)/3^k;
end
However, you have been told to do this with a "while" loop. Therefore, it is equivalent to write:
total = 0;
k=1;
while k <= n
total = total + ((-1)^k * k^2 +5*k)/3^k;
k = k+1;
end
All you have to do is define "n" as required.
Related Question