How to use FOR loops in Matlab ?

2 views (last 30 days)
  3 Comments
Chad Greene
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.
Yash Shil Balgobin
Yash Shil Balgobin on 29 Mar 2021
Hi Chad. I really appreciate you taking time to reply to my question. I am trying to do this question using only FOR loops. However, tutorials and learning videos about FOR loops are somewhat limited. I looked up for a few examples and tutorials on FOR loops but the examples were kind of advanced.

Sign in to comment.

Accepted Answer

David Hill
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
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
Yash Shil Balgobin
Yash Shil Balgobin on 2 Apr 2021
Thank you so much David. This actually helped a lot. Thank you very much.

Sign in to comment.

More Answers (0)

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!