How to use FOR loops in Matlab ?
2 views (last 30 days)
Show older comments
Yash Shil Balgobin
on 28 Mar 2021
Commented: Yash Shil Balgobin
on 2 Apr 2021
3 Comments
Chad Greene
on 28 Mar 2021
Hi Yash,
Welcome to the forum. It's totally acceptable to ask for help with homework here, but I think you'll have better luck if you can describe what you've tried so far. If you don't know where to begin, at least write in your own words what you're trying to accomplish.
Accepted Answer
David Hill
on 28 Mar 2021
Look at the sum() function and element-wise operations. No loop is needed. I provided the numerator as an example.
sum((-1).^(1:k).*(1:k).^2)
3 Comments
David Hill
on 30 Mar 2021
Instead of doing the calculation all at once, just sum it as you go with a for-loop
n=20;
y=0;
for k=1:n
y=y+(-1)^k*k^2/%you finish
end
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!