The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

I need help with some octave code

b0bd0db0bd0d Registered User regular
edited September 2009 in Help / Advice Forum
I'm in a class that deals with using Octave to do some math stuff. The trouble I'm having is related to computing pi using a infinite sum of series to within a tolerance of 1e-5.
pi = 4arctan(1)
and
pi = 4(arctan(1/2) + arctan(1/3))
with arctan being
((-1)^n)*(x^(2*n+1)))/(2*n+1)

so here's my code. The first part works fine. The second part causes all types of problems and gets stuck in a loop.
x=1;
tol = 1e-5;
i=0;		#n
a=0;		#answer
b=0;		#next answer. Need to check for tol

more off;			#display output so you know something's happening
format long;
printf("These are both calculated extremely inefficiently, so please wait a few seconds for results...\n");

	a = (a+(((-1)^i)*(x^(2*i+1)))/(2*i+1));
	b = (a+(((-1)^(i+1))*(x^(2*i+2)))/(2*i+2));
	i++;
	
while abs((4*b)-(4*a)) > tol
	a = (a+(((-1)^i)*(x^(2*i+1)))/(2*i+1));
	b = (a+(((-1)^(i+1))*(x^(2*i+2)))/(2*i+2));
	i++;
endwhile
printf("After %g steps to within %g, 4*tan^-1(1) = %f\n",i,tol,(a*4));


#here we go again...

x1=(1/2);
x2=(1/3);

i=0;

a=0;	#tan^-1(1/2)
b=0;	#tan^-1(1/3)
aplusb = 0;	#a+b. need for tol check with cplusd

#next sums
c=0;	#tan^-1(1/2)
d=0;	#tan^-1(1/3)
cplusd=0;	#c+d


	a = (a+(((-1)^i)*(x1^(2*i+1)))/(2*i+1));
	b = (b+(((-1)^i)*(x2^(2*i+1)))/(2*i+1));
	aplusb = a+b;	
	
	c = (c+(((-1)^(i+1))*(x1^(2*i+2)))/(2*i+2));
	d = (d+(((-1)^(i+1))*(x2^(2*i+2)))/(2*i+2));
	cplusd = c+d;
	
	i++;
	
	
	
#there's a bug here and I don't know why....
while abs((cplusd) - (aplusb)) > tol
	a = (a+(((-1)^i)*(x1^(2*i+1)))/(2*i+1));
	b = (b+(((-1)^i)*(x2^(2*i+1)))/(2*i+1));
	aplusb = a+b;	
	
	c = (c+(((-1)^(i+1))*(x1^(2*i+2)))/(2*i+2));
	d = (d+(((-1)^(i+1))*(x2^(2*i+2)))/(2*i+2));
	cplusd = c+d;
	
	i++;
endwhile

printf("After %g steps to within %g, 4*(tan^-1(1/2) + tan^-1(1/3)) = %f\n",i,tol,(4*aplusb));

b0bd0d on

Posts

  • DaenrisDaenris Registered User regular
    edited September 2009
    Well, I think I found the problems. Your incremental answers aren't being calculated from the correct previous answer.

    In the first set you have it as

    a = a+something
    b = a+something

    Which correctly calculates the b as the next value beyond a. However, in the second set, you have

    a = a+something
    b = b+something
    c = c+something
    d = d+something

    So, your c and d aren't being calculated correctly as the next answer.
    In the second set, to match the formula of your first set, it should be like

    a = a+something
    b = b+something
    c = a+something
    d = b+something

    Additionally, I think your formula for the next value is off.
    Your arctan formula is:
    ((-1)^n)*(x^(2*n+1)))/(2*n+1)
    So your next n formula should be:
    ((-1)^(n+1))*(x^(2*(n+1)+1)))/(2*(n+1)+1)

    You can't simplify 2*(n+1)+1 to 2*n+2, because the 2*(n+1) would be calculated first, so it would be 2*n+3.

    edit: With minor syntax edits, I ran it with my changes through Matlab and it no longer hangs on the second set, and at least provides a sensible result estimating pi.

    Daenris on
Sign In or Register to comment.