フィルターのクリア

How can I fix my problems in this program using loops?

1 回表示 (過去 30 日間)
Diane
Diane 2013 年 7 月 24 日
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
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
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
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

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by