Calculate 'pi' in Fortran

  • Comp Sci
  • Thread starter mrous27
  • Start date
  • Tags
    Fortran Pi
In summary, the original poster is trying to find pi using formulas for sin and cos. They received a helpful reply, so they will not be deleting the thread. They need to make changes to their programs to calculate sin and cos correctly.
  • #1
mrous27
4
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
  • #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)
 
  • #3
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.
 
  • #4
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
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:
  • #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.
 
  • #7
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.
 
  • #8
Thanks for this also :) I will try to fix them.
 
  • #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).
 
  • #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.
 

1. How do I calculate 'pi' in Fortran?

To calculate 'pi' in Fortran, you can use the built-in function PI() or the constant 3.141592653589793.

2. Can I use a loop to calculate 'pi' in Fortran?

Yes, you can use a loop to calculate 'pi' in Fortran. One common method is the Leibniz formula, which uses a series of alternating fractions to approximate 'pi'.

3. What is the precision of the 'pi' value in Fortran?

The precision of the 'pi' value in Fortran depends on the data type used. If you use the REAL data type, which is the default for 'pi', the precision is 6-7 digits. If you need a higher precision, you can use the DOUBLE PRECISION data type.

4. Can I use the 'pi' value in Fortran for scientific calculations?

Yes, the 'pi' value in Fortran is suitable for most scientific calculations. However, if you need a higher precision, you may want to consider using a more accurate value or a different data type.

5. Are there any special considerations when using 'pi' in Fortran?

One consideration when using 'pi' in Fortran is to make sure you are using the correct data type for your calculations. As mentioned before, the REAL data type may not provide enough precision for some applications. Additionally, you should be aware of the potential for rounding errors when using 'pi' in calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
1K
  • Introductory Physics Homework Help
Replies
28
Views
221
  • Engineering and Comp Sci Homework Help
Replies
3
Views
938
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
969
  • Calculus and Beyond Homework Help
Replies
1
Views
56
Back
Top