Determine Expression

Write a program (using a loop) the dertimes the expression: sqrt 12 Sigma (m on top) (n=0 on bottom) ((-1/3)^n) / (2*n+1) Run the program with m=5 m=10 and m=20. Compare the results with pi (Use Long format).
this is what I have done
format long
S=0 m=input('Input a value for m=')
for n=0:m S(1:m)=sqrt(12)*(S+((-1/3)^n)/(2*n+1))
end
not sure if this is right
run program for m=5 m=10 m= 20
my results m =
5s =
Columns 1 through 4
3.464101615137754 3.464101615137754 3.464101615137754 3.464101615137754Column 5
3.464101615137754s =
Columns 1 through 4
11.615099820540248 11.615099820540248 11.615099820540248 11.615099820540248
Column 5
11.615099820540248
s =
Columns 1 through 4
40.312866084211663 40.312866084211663 40.312866084211663 40.312866084211663
Column 5
40.312866084211663
s =
1.0e+002 *
Columns 1 through 4
1.396295359331753 1.396295359331753 1.396295359331753 1.396295359331753Column 5
1.396295359331753s =
1.0e+002 *
Columns 1 through 4
4.836956528011152 4.836956528011152 4.836956528011152 4.836956528011152Column 5
4.836956528011152s =
1.0e+003 *
Columns 1 through 4
1.675569596143254 1.675569596143254 1.675569596143254 1.675569596143254Column 5
1.675569596143254

 채택된 답변

bym
bym 2011년 11월 21일

0 개 추천

the multiplication of sqrt(12) should occur outside the loop. Also do not include S in the summation:
for n = 0:m
s(n+1) = (-1/3)^n/(2*n+1);
end
now you have a vector of values you can sum & multiply by sqrt(12)

추가 답변 (2개)

Jan
Jan 2011년 11월 21일

2 개 추천

Could we have an idea?
Do you have a computer? Is Matlab installed? Did you read the getting started chapters already? Do you know, what a loop is, especially a FOR loop? Have you aksed Google for some details already?
You've posted the homework question and we cannot post the running code. So what kind of help do you expect?

댓글 수: 6

Sven
Sven 2011년 11월 21일
We already have the question. We *don't* have any idea what you've tried so far. You say "I have no idea where to start". Please start by answering the following:
1. Do you have MATLAB installed?
2. Have you read the "Getting Started" chapter? (it's a *great* place to start)
3. Do you know how to write a "for loop"?
4. Please show us your "for loop" for this question and describe what you want it to do.
Bobby
Bobby 2011년 11월 21일
this is what I have done
format long
S=0
m=input('Input a value for m=')
for n=0:m
S(1:m)=sqrt(12)*(S+((-1/3)^n)/(2*n+1))
end
not sure if this is right
Sven
Sven 2011년 11월 21일
Bobby, please stop deleting your comments.
Bobby
Bobby 2011년 11월 21일
is my for loop correct? What about my results?
Sean de Wolski
Sean de Wolski 2011년 11월 21일
Are your results correct? Do they meet the constraints of the problem? If they do, I would hazard a guess they're correct!
Walter Roberson
Walter Roberson 2011년 11월 21일
Your loop is wrong, and your results are wrong.
Re-read the question. How many elements are output from a summation ? How many output elements do you have?

댓글을 달려면 로그인하십시오.

Wayne King
Wayne King 2011년 11월 21일

0 개 추천

Have you read the MATLAB Getting Started Guide? There are plenty of examples on using loops.

댓글 수: 1

Bobby
Bobby 2011년 11월 21일
this is what I have done
format long
S=0
m=input('Input a value for m=')
for n=0:m
S(1:m)=sqrt(12)*(S+((-1/3)^n)/(2*n+1))
end
not sure if this is right

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2011년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by