Please write a script for summation of 1/k
4 views (last 30 days)
Show older comments
2 Comments
Accepted Answer
DGM
on 24 Jan 2022
I don't see why the code you wrote wouldn't work.
N = 100;
s = 0; % using sum as a name shadows the function sum()
for k = 1:N
s = s + 1/k;
end
s
That works, but you can always do it without the loop.
s = sum(1./(1:N))
3 Comments
More Answers (1)
Image Analyst
on 25 Jan 2022
Is summation.m a function or a script? If it's what you posted already, it's a script and should not give any error about inputs since there are no inputs. And it should work, once you rename sum to x, as DGM showed in his answer, which ran it in MATLAB online.
On the other hand, if it's a function the first line will be
function summation(N)
and maybe you're supposed to call it passing in N and maybe all you did was click the green run triangle without assigning N. Again, attach your m-file. Use the paperclip icon.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!