This homework question is poorly written:
- It seems like the intent of the n is to use it as the upper limit of the for-loop, but the directions then state to hard-code this upper limit at 20, making n useless.
- It uses the variable name "sum" which causes the code to shadow the MATLAB sum( ) function. Best practice is to use a different variable name that doesn't shadow a MATLAB function.
Because of these apparently contradictory instructions, I can see how a beginner would get confused. I would advise you to ignore the direction in the first part 3 and use n as the upper limit of the for-loop. So the body of your function code would look like this:
sum = 0;
for i=1:n
sum = sum + _______;
end
Again, "sum" is a poor choice for a variable name, but I am using it because the directions explicitly told you to do so.
The fill-in-the-blank part is not going to be sum_of_cubes(n) as you have currently written it. See if you can figure out what it should be instead. Look at the sum that is written out in the instructions and ask yourself what you should be adding into sum at each iteration. The instructions state "... keep adding the cube of every number ...", so that is your clue as to what should be here.
Best Answer