# Fortran - Help with Truncated Infinite Series Calculator

• Comp Sci
dss91

## Homework Statement

I have an assignment to write a program for calculating the sine (and various other functions) using the method of truncated infinite series using DO statements. The DO statement is supposed to run until the difference between the current and last iterations are less than 1.0E-6. Saying that I am lost is an understatement. I have done some research on the internet and seem to have been able to piece together some code which makes sense to me, however I am just getting random junk out of it when I run it. Any help would be greatly appreciated! There are multiple menu options for different functions, which I have yet to start on, as I am trying to get the sine function to work correctly first.

I put a "!" in front of the IF statement on the accuracy checker to see if it would even compute the sine to 10000 terms correctly, but that doesn't even seem to work!

## The Attempt at a Solution

Code:
PROGRAM PROJECT2
IMPLICIT NONE
INTEGER::i,n
REAL::x,z,sinx,cosx
CHARACTER::choice

WRITE(*,*) 'Please enter a number x:'

DO
WRITE(*,*) ''
WRITE(*,*) '  Iterative Function Calculator'
WRITE(*,*) '-------------------'
WRITE(*,*) '  A) SIN(x)'
WRITE(*,*) '  B) COS(x)'
WRITE(*,*) '  C) e^x'
WRITE(*,*) "  D) Lambert's W function Wo(x)"
WRITE(*,*) '  E) Enter a new x'
WRITE(*,*) '  Q) Quit'
IF(CHOICE .EQ. 'Q' .or. CHOICE .EQ. 'q') THEN
EXIT
ELSE IF(CHOICE .EQ. 'A' .or. CHOICE .EQ. 'a') THEN
DO i = 1,n
n=1
sinx=0.
sinx=sinx+z
n=n+1
z=z+(-1)**n*x**(2*n+1)/((2*n-2)*(2*n-1))
IF(n==10000) THEN
EXIT
!IF((abs(sinx-(sinx+z))) < (1.E-6)) THEN
!EXIT
END IF
END DO
WRITE(*,*) z
!WRITE(*,'(A,F6.4,A,F6.4 )') 'SIN(',x,') = ',sinx
ELSE IF(CHOICE .EQ. 'D' .or. CHOICE .EQ. 'd') THEN
ENDIF
END DO
END PROGRAM

## The Attempt at a Solution

Mentor
n is uninitialized in your DO i = 1, n loop, AND you are attempting to change the upper limit of your loop, n, inside the body of the loop.

Also, z is uninitialized. You have a statement sinx = sinx + z, and this is guaranteed to add garbage to a known value, which results in garbage.

dss91
Ok, I have edited my code a bit and I am now getting 0.0000 consistantly out when I compile and run. That is much better than what I had before, but I am still confused at what might be wrong.

Code:
WRITE(*,*) 'Please enter a number x:'

DO
WRITE(*,*) ''
WRITE(*,*) '  Iterative Function Calculator'
WRITE(*,*) '-------------------'
WRITE(*,*) '  A) SIN(x)'
WRITE(*,*) '  B) COS(x)'
WRITE(*,*) '  C) e^x'
WRITE(*,*) "  D) Lambert's W function Wo(x)"
WRITE(*,*) '  E) Enter a new x'
WRITE(*,*) '  Q) Quit'

SELECT CASE(choice)

CASE('Q','q')
EXIT
CASE('A','a')
sindec=x

sinx=0.
DO i=1,n
fact=1.
DO j=1,2*i-1
fact=fact*j
END DO
IF(abs(sindec-sinx) <1.0E-6) THEN
EXIT
END IF
sinx=sinx+sindec
sindec=sindec+(-1)**(i-1)*x**(2*i-1)/fact
END DO
WRITE(*,'(A,F6.4,A,F6.4 )') 'SIN(',x,') = ',sinx
CASE('B','b')
WRITE(*,'(A,F6.4,A,F6.4 )') 'COS(',X,') = ',sinx
CASE('C','c')
CASE('D','d')
CASE('E','e')
WRITE(*,*) 'Please enter a number x:'
CASE default
WRITE(*,*) 'Not Recognized'

END SELECT

ENDDO !end do loop for menu

END PROGRAM

Mentor
Code:
CASE('A','a')
sindec=x
sinx=0.
DO i=1,n <---
fact=1.
DO j=1,2*i-1
fact=fact*j
END DO
IF(abs(sindec-sinx) <1.0E-6) THEN
EXIT
END IF
sinx=sinx+sindec
sindec=sindec+(-1)**(i-1)*x**(2*i-1)/fact
END DO
WRITE(*,'(A,F6.4,A,F6.4 )') 'SIN(',x,') = ',sinx
Your outer loop runs from i = 1 to i = n. How many times will this loop run?

dss91
I would like to run it until the desired accuracy is satisfied (1.0E-6), I guess I am a bit unsure how to specify this. Should I get rid of that part completely and just let the DO loop run until the IF statement is satisfied or do I need to define n to be a certain value?

Mentor
I would like to run it until the desired accuracy is satisfied (1.0E-6), I guess I am a bit unsure how to specify this. Should I get rid of that part completely and just let the DO loop run until the IF statement is satisfied or do I need to define n to be a certain value?

If you are going to run a counting DO loop (i.e., DO i = 1, n), you HAVE to define n. If your desired accuracy is reached, you can get out of the loop with EXIT, as you are doing. In any case, though, you always need to define starting and ending values for your counting loop.