How can I fix my problems in this program using loops?
조회 수: 2 (최근 30일)
이전 댓글 표시
The actual question asks to write a program (Using a loop) that determines the expression
m
2|-| ((2n)^2)/((2n)^2)-1) = 2(4/3*16/15*36/35....)
n=1
(above is an infinite product series symbol)
Run the program with m = 100, m = 100,000, and m = 1,0000,000. Compare the result with π. (Use format long.)
So far this is what I have come up with in MATLAB
clc
clear all
format long
n=1;
S=0;
m=input('Enter m')
for n=1:m
S(1:m)=2*(((2*n)^2))/(((2*n)^2)-1)
end
m
pi
댓글 수: 2
dpb
2013년 7월 24일
Where's the summation????
You'll need to accumulate into S each term inside the loop on n to do the sum via looping...the LHS should be a single term to add each pass not a vector.
Raghavendra
2013년 7월 24일
Yes that's right..
create a dummy variable then keep accumulating the results into it, finally do the summation. s= []; %Initialize the dummy variable for n=1:m S = 2*(((2*n)^2))/(((2*n)^2)-1); % do the calculation s= [s;S];% store the result end Sum_S = sum(s); %get the sum
답변 (1개)
David Sanchez
2013년 7월 24일
I think your equation has a mistake, since for n=1 it yields 0, and subsequently, the result is 0 for being a multiplication. The idea behind the series recursion is the following. Adapt it to your equation and make sure you write it rightly.
S = 1; % I start in one to yield non-zero result.
m = input('Enter m: ');
for n = 1:m
S = S*( (2*n)^2 /((2*n)^2 -1 ) );
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!