1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Calculate 'pi' in Fortran

  1. Dec 20, 2012 #1
    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. jcsd
  3. Dec 20, 2012 #2
    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)
  4. Dec 20, 2012 #3


    User Avatar

    Staff: Mentor

    Welcome to the PF.

    We normally delete threads where the original poster (OP) shows zero effort as you have done. But you have received a helpful reply, so we will not delete this thread. Please show your work based on the hints that you have received.
  5. Dec 23, 2012 #4
    My attempt ;
    DO i = 1,n

    print*, sinx

    END DO

    DO i = 1,n

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

    print*, (abs(sinx-cosx))
    end if
    end program asd
  6. Dec 23, 2012 #5


    User Avatar
    Homework Helper

    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
  7. Dec 23, 2012 #6
    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.
  8. Dec 23, 2012 #7


    User Avatar
    Homework Helper

    I only meant to use sin(x) and cos(x) to check your infinite series loops. Those loops need to be fixed.
  9. Dec 23, 2012 #8
    Thanks for this also :) I will try to fix them.
  10. Dec 23, 2012 #9
    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).
  11. Dec 26, 2012 #10


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    There are several ways without using trig functions..


    not all appear converge quickly. You would need to check how many terms are required to get to the required accuracy.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook