문제

Here's my problem:

Compute the value of π using the following series:

((π^2)-8)/16=[sum from 1 to pos. infinity] 1/(((2n−1)^2)*((2n+1)^2))

• Find the smallest number of terms required to obtain an absolute value of the error on π smaller than 10e−8.

Here's my code:

x=0;
for i=1:1000

    x=x+(1/((((2*i)-1)^2)*(((2*i)+1)^2)));
    z=sqrt((x*16)+8);
    error=abs(z-pi);
    if (error < 10e-8)
        i
        break
    end
end

The answer that I get is 81 when the loop breaks, but it is not the right answer. I have been trying to figure out what is wrong with my code that it doesn't do what I need.

I've been staring at the code for quite a while and cant see where I made a mistake.

도움이 되었습니까?

해결책

I found the problem. The error is supposed to be less than 10^-8 not 10e-8. Somehow the numbers got changed over when copying.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top