sum of cubes question

Please let me know how to fix my code. I am unable to make mat lab run.
Here is my code so far:
I created a file and saved it as sum_of_cubes.m
for n=1:20
[sum]=sum_of_cubes(n)
end
I created a new live script, then ran it (or attempted to).
sum=0;
[sum]=sum_of_cubes(20)
Please see the homework prompt below.

 Accepted Answer

James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
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; % <-- 2nd part 3 direction
for i=1:n % <-- using n as the upper limit of the for-loop, ignoring 1st part 3 direction
sum = sum + _______; % <-- you fill this in, 2nd part 3 direction
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.

22 Comments

N/A
N/A on 24 Nov 2020
Thank you for starting me on this, yet I struggle with this part: sum = sum + _______ .I don't know what to do. If I understood you correctly, said to do the following:
input this information into the sum_of_cubes.m file
for i=1:n
sum = sum + ????;
end
move to the new live script and input this information
[sum]=sum_of_cubes(20)
Where would this information go?
[sum]=sum_of_cubes(n)
James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
The ???? part above needs to be "... keep adding the cube of every number ...".
What is the cube of the i'th number? If I gave you a variable named i, how would you calculate the cube of it?
This line is how you call your function (e.g., from the command line):
[sum] = sum_of_cubes(20)
This line doesn't go anywhere in your function. Remove it:
[sum]=sum_of_cubes(n)
Or
% Test Script:
n = 5;
theSum = sum_of_cubes(n);
fprintf('For n = %d, sum_of_cubes(%d) = %d.\n', n, n, theSum);
% Function definition:
function sum_of_cubes(n)
theSum = 0; % <-- 2nd part 3 direction
for i = 1 : n % <-- using n as the upper limit of the for-loop, ignoring 1st part 3 direction
theSum = theSum + n _______; % <-- you fill this in, 2nd part 3 direction
end
N/A
N/A on 24 Nov 2020
Ok, I am reading through your answers now and trying them. Will share results shortly.
N/A
N/A on 24 Nov 2020
James I tried what you recommended (or what I think you recommended) and this did not work. See screen shot.
The code you posted above is supposed to be inside a function with this signature:
function sum = sum_of_cubes(n)
Then n will be defined by passing in an argument when you call it.
N/A
N/A on 24 Nov 2020
This is what I have and it is not working.
James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
Why did you get rid of the sum=0 line? That needs to be there. Also, why did you suddenly switch variable name from n to number? Just use n.
N/A
N/A on 24 Nov 2020
I changed those things to make it work better. Here is me trying it again:
James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
The sum=0 line needs to go inside your function. Make it the first line after the function line. You initialize this variable to 0 as your first step in the function. Then you add to it with the for-loop code.
N/A
N/A on 24 Nov 2020
function [sum=0]=sum_of_cubes(n) ?
N/A
N/A on 24 Nov 2020
This still does not work.
N/A
N/A on 24 Nov 2020
Is this what you meant?
James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
You are very close, but you are still struggling. The code for the function is the following, which is what you already have but in the proper order:
% This code goes in a file called sum_of_cubes.m
function sum = sum_of_cubes(n)
sum = 0; % <-- 2nd part 3 direction
for i = 1 : n % <-- using n as the upper limit of the for-loop, ignoring 1st part 3 direction
sum = sum + i^3; % <-- you fill this in, 2nd part 3 direction
end
end
Then you call it e.g. from the command line:
>> sum_of_cubes(20)
N/A
N/A on 24 Nov 2020
When I do that it does not work.
N/A
N/A on 24 Nov 2020
Please post all of your current code so we can look to see what is going wrong.
N/A
N/A on 24 Nov 2020
That is all of it. Here is the typed version if you must have it:
function sum_of_cubes(n)
sum = 0;
for i = 1 : n
sum = sum + i^3;
end
>> sum_of_cubes(20)
N/A
N/A on 24 Nov 2020
Edited: N/A on 24 Nov 2020
The other stuff you see is from my last code for this problem that worked. My last code was this:
function[sum]=sum_of_cubes(n)
sum=0
For i=1:n
sum=sum+i^3
end
end
>> [sum]=sum_of_cubes(20)
sum =
44100
James Tursa
James Tursa on 24 Nov 2020
Edited: James Tursa on 24 Nov 2020
This function signature of your earlier post:
function sum_of_cubes(n)
doesn't return anything to the caller. That is, it does some calculations to generate the sum variable, but then it throws it away and doesn't return it. You have to have syntax that shows you want to return the sum variable:
function sum = sum_of_cubes(n)
That being said, it looks like you already have code that does this and works? I am not sure what problems you are currently having.
N/A
N/A on 24 Nov 2020
Yes, I have a code that works. For some reason, your code does not work when I copy and paste it. Not sure why.
N/A
N/A on 24 Nov 2020
Thank you for your help! :)

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Asked:

N/A
on 24 Nov 2020

Commented:

N/A
on 24 Nov 2020

Community Treasure Hunt

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

Start Hunting!