Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Diane
Diane am 24 Jul. 2013
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
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
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Sanchez
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

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!

Translated by