Comp Sci How can I calculate pi in Fortran without using intrinsic functions?

  • Thread starter Thread starter mrous27
  • Start date Start date
  • Tags Tags
    Fortran Pi
AI Thread Summary
The discussion focuses on calculating pi in Fortran 90/95 without using intrinsic functions, specifically through the relationship between sine and cosine. The method suggested involves finding values of x where sin(x) equals cos(x) within a specified error margin, using infinite series to compute these functions. Participants emphasize the need for proper loop structures to calculate sine and cosine accurately, recommending the use of Taylor series expansions. Suggestions include creating separate functions for sine and cosine calculations to streamline the process. Overall, the conversation centers on algorithmic approaches to achieve the desired precision in calculating pi.
mrous27
Messages
4
Reaction score
0

Homework Statement


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.


Homework 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?


The Attempt at a Solution

 
Physics news on Phys.org
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)
 
mrous27 said:

Homework Statement


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.


Homework 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?


The Attempt at a Solution


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.
 
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
 
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:
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.
 
mrous27 said:
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.
I only meant to use sin(x) and cos(x) to check your infinite series loops. Those loops need to be fixed.
 
Thanks for this also :) I will try to fix them.
 
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
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.
 

Similar threads

Replies
7
Views
2K
Replies
4
Views
3K
Replies
3
Views
7K
Replies
2
Views
9K
Replies
4
Views
10K
Replies
7
Views
4K
Replies
23
Views
3K
Back
Top