Fortran - Help with Truncated Infinite Series Calculator

In summary, the conversation is about a person struggling with an assignment to write a program for calculating various functions using the method of truncated infinite series. They are specifically having trouble with the sine function and getting random output when running their code. They receive help with correcting their code and continue to work on implementing menu options for different functions. The conversation ends with a discussion about how to specify the desired accuracy for the program.
  • #1
dss91
5
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
  • #2
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.
 
  • #3
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
 
  • #4
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?
 
  • #5
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?
 
  • #6
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.
 

Related to Fortran - Help with Truncated Infinite Series Calculator

1. How does the Fortran Truncated Infinite Series Calculator work?

The Fortran Truncated Infinite Series Calculator uses the Fortran programming language to perform calculations on truncated infinite series. It takes in user-defined parameters such as the initial term, the number of terms to be calculated, and the desired accuracy level, and outputs the resulting truncated series.

2. What is a truncated infinite series?

A truncated infinite series is a mathematical series that has been cut off after a certain number of terms. This is done to approximate the value of an infinite series, which cannot be calculated exactly. The more terms that are included in the truncated series, the closer the approximation will be to the actual value of the infinite series.

3. How accurate are the results from the Fortran Truncated Infinite Series Calculator?

The accuracy of the results depends on the user-defined parameters, particularly the number of terms to be calculated. The more terms that are included, the more accurate the result will be. However, it is important to note that due to the nature of truncated infinite series, there will always be some level of error in the approximation.

4. Can the Fortran Truncated Infinite Series Calculator handle all types of infinite series?

Yes, the Fortran Truncated Infinite Series Calculator can handle a wide range of infinite series, including geometric, arithmetic, and power series. However, it may not be able to handle more complex series that require specialized techniques or algorithms.

5. Is the Fortran Truncated Infinite Series Calculator difficult to use?

The Fortran Truncated Infinite Series Calculator may require some basic knowledge of Fortran programming language to use. However, there are many resources available online that can help with learning the language. Once familiar with Fortran, the calculator is straightforward to use and can save time and effort in calculating truncated infinite series.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • 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
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Calculus and Beyond Homework Help
Replies
1
Views
286
  • Programming and Computer Science
Replies
4
Views
635
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
2
Views
1K
Back
Top