just started matlab just wondering how to set up a 'for' loop
Show older comments

6 Comments
the cyclist
on 22 Nov 2021
It is a bit difficult to help you here, without just writing out the full code.
I would highly recommend watching the MATLAB Onramp tutorial, and also reading the documentation of the for statement, which has a few examples. Then, you could try to write the code yourself, and come back with specific questions.
Bethany Young
on 22 Nov 2021
James Tursa
on 22 Nov 2021
Edited: James Tursa
on 22 Nov 2021
That's a good start. Three suggestions:
- Change the top value inf to some finite number
- wrap the -1 exponent in parentheses, e.g. (-1)^(n+1)
- To display the answer you can use disp(S) or maybe look at fprintf( )
James Tursa
on 22 Nov 2021
Are you supposed to sum these terms until the terms get small enough that they don't affect the sum? Or ...?
Bethany Young
on 22 Nov 2021
Edited: Bethany Young
on 22 Nov 2021
James Tursa
on 22 Nov 2021
Then I think with the -1 exponent correction you are done.
Answers (1)
James Tursa
on 22 Nov 2021
Here is an outline to get you started:
x = _____;
nmax = _____;
result = 0;
for n=1:nmax
result = result + _____;
end
You need to fill in the blanks. The x value and the maximum n value to use. For the blank inside the for-loop, you need to write an expression involving x and n that matches the terms in your sum formula.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!