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

6 Comments

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.
the question had asked for x to be 0.8, this is what i down so far however i dont think i have done it right it followed what what written on onramp
x = 0.8;
S = 0;
for n = 1:inf
S = S +((-1)^n+1)./(2*n-1)*x^(2*n-1);
end
S;
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( )
Are you supposed to sum these terms until the terms get small enough that they don't affect the sum? Or ...?
ive been asked to do the first 10 terms so ive just changed inf to 10
Then I think with the -1 exponent correction you are done.

Sign in to comment.

Answers (1)

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

Products

Release

R2020a

Asked:

on 22 Nov 2021

Commented:

on 22 Nov 2021

Community Treasure Hunt

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

Start Hunting!