Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
dpb
dpb on 24 Jul 2013
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 on 24 Jul 2013
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

Sign in to comment.

Answers (1)

David Sanchez
David Sanchez on 24 Jul 2013
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

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!