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

  • Context: Comp Sci 
  • Thread starter Thread starter mrous27
  • Start date Start date
  • Tags Tags
    Fortran Pi
Click For Summary

Discussion Overview

The discussion revolves around calculating the value of pi in Fortran 90/95 without utilizing intrinsic functions, specifically through the use of the sine and cosine functions derived from their infinite series representations. Participants explore various methods and approaches to achieve this calculation while adhering to the project constraints.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) seeks to calculate pi using the condition where sin(x) equals cos(x) with a specified error margin of 10-4.
  • One participant suggests a basic looping method to increment x and compare sin(x) and cos(x) until the condition is met, noting that this approach may be inefficient.
  • Another participant emphasizes the need for an outer loop to try different values of x while calculating sin(x) and cos(x) within inner loops.
  • There is a clarification that the use of intrinsic sin(x) and cos(x) functions is not allowed, and the OP must rely on infinite series to compute these values.
  • One participant proposes that using Taylor series expansions for sine and cosine could be a viable approach, suggesting that fewer terms than 9000 might suffice for the required accuracy.
  • Another participant mentions alternative methods for calculating pi without using trigonometric functions, referencing external resources for potential formulas.

Areas of Agreement / Disagreement

Participants express differing views on the implementation details and efficiency of the proposed methods. There is no consensus on a single approach, and the discussion remains unresolved regarding the best way to calculate pi under the given constraints.

Contextual Notes

Participants note limitations regarding the use of intrinsic functions and the necessity to rely on infinite series for sine and cosine calculations. The discussion also highlights the potential overuse of terms in series expansions, suggesting that fewer terms may achieve the desired accuracy.

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 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K