Homework Help: Calculate 'pi' in Fortran

1. Dec 20, 2012

mrous27

1. The problem statement, all variables and given/known data
I have a project about pi. We are wanted to calculate pi in Fortran 90/95,without use intrinsic functions. For example, we must think like a computer.

2. Relevant equations
We must use formula of sin(x) and cos(x) for find 'pi'. When Sin(x)=cos(x) (and error is 10**-4)
4*(result) gives us 'pi.' Do you have any suggestion?

3. The attempt at a solution

2. Dec 20, 2012

niklaus

Quick and dirty (but inefficient):

Do a loop that increases x by a small amount every time, calculate sin(x) and cos(x), compare them, if they fulfill the condition, congratulations you found pi

more sophisticated: do large increments while sin(x) < cos(x), as soon as cos(x)>sin(x) decrease with smaller increments etc. until you get close enough to sin(x)=cos(x)

3. Dec 20, 2012

Staff: Mentor

Welcome to the PF.

4. Dec 23, 2012

mrous27

My attempt ;
PROGRAM ASD
IMPLICIT NONE
REAL::SINX,x,cosx,z
INTEGER::I,N=9000
DO i = 1,n

x=0.5
n=1
sinx=0.
sinx=sinx+z
n=n+1
z=z+(-1)**n*x**(2*n+1)/((2*n-2)*(2*n-1))
print*, sinx

END DO

DO i = 1,n
x=0.5
n=1
cosx=0.
cosx=cosx+z
n=n+1
z=z+(-1)**n*x**(2*n)/((2*n-1)*(2*n))
print*,cosx

END DO
IF ((abs((sinx+z)-(cosx+z))) < (1.E-4)) THEN

print*, (abs(sinx-cosx))
end if
end program asd

5. Dec 23, 2012

rcgldr

Your program calculates one value for sinx, another for cosx, does a single compare and then just quits. You will need an outer loop that tries different values of x, while the two inner loops calculate values for sinx and cosx.

Your loops to calculate sinx and cosx need to be fixed. You might want to use the actual sin(x) and cos(x) functions from Fortran and compare them to the values you get from your loops.

Last edited: Dec 23, 2012
6. Dec 23, 2012

mrous27

Thakns,It is forbidden to use sin(x) and cos(x). We have to use these infinite series to describe sin(x) and cos(x) to Fortran.

7. Dec 23, 2012

rcgldr

I only meant to use sin(x) and cos(x) to check your infinite series loops. Those loops need to be fixed.

8. Dec 23, 2012

mrous27

Thanks for this also :) I will try to fix them.

9. Dec 23, 2012

niklaus

Your loops don't calculate anything like sin(x) or cos(x) at the moment. I am not sure if you are trying to implement the taylor expansions of sine and cosine (a good idea in my opinion), but if you are, 9000 terms would be total overkill. 10 should be plenty for your purposes.

Hint about loops: You probably should make use of the variable i somehow...

Suggestion about the structure of the program: If you know how, maybe it would be helpful to put the calculations of sin and cos into separate functions, so you can call them like the built in ones, for example call them mysin(x) and mycos(x).

10. Dec 26, 2012

CWatters

There are several ways without using trig functions..

http://mathworld.wolfram.com/PiFormulas.html

not all appear converge quickly. You would need to check how many terms are required to get to the required accuracy.