Fortran - Help with Truncated Infinite Series Calculator

Click For Summary

Discussion Overview

The discussion revolves around a programming assignment involving the creation of a Fortran program to calculate the sine function using truncated infinite series. Participants are addressing issues related to the implementation of the algorithm, particularly focusing on the use of DO loops, variable initialization, and achieving the desired accuracy in calculations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about how to implement the sine function using truncated infinite series and mentions issues with getting random outputs from their code.
  • Another participant points out that the variable 'n' is uninitialized in the DO loop and that changing 'n' within the loop could lead to errors.
  • A participant reports that after editing their code, they consistently receive an output of 0.0000, indicating further issues with the implementation.
  • Concerns are raised about the need to define 'n' for the counting loop and whether the loop should run until the desired accuracy is achieved, rather than a fixed number of iterations.
  • There is a suggestion that if the desired accuracy is reached, the loop can exit, but the necessity of defining starting and ending values for the counting loop is emphasized.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper initialization of variables and the structure of the DO loop. However, there is no consensus on the best approach to implement the accuracy condition or how to handle the variable 'n' within the loop.

Contextual Notes

There are unresolved issues regarding the initialization of variables and the control flow of the loops, which may affect the accuracy of the calculations. The discussion also highlights the dependence on the correct definition of 'n' and the handling of the exit conditions for the loops.

dss91
Messages
5
Reaction score
0

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:'
READ(*,*) 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'
WRITE(*,*) 'Please enter choice:'
READ(*,*) choice
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
	READ(*,*) x
ENDIF
END DO
END PROGRAM
 
Physics news on Phys.org
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.
 
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.

Thanks in advance!

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

!enter do loop for menu

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'
WRITE(*,*) 'Please enter choice:'
READ(*,*) choice

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:'
	READ(*,*) x
CASE default
	WRITE(*,*) 'Not Recognized'

END SELECT


ENDDO !end do loop for menu

END PROGRAM
 
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?
 
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?
 
dss91 said:
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.
 

Similar threads

Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K