How can I fix my problems in this program using loops?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
dpb
am 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
am 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
Antworten (1)
David Sanchez
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!