fork(2) download
  1. program TraceExponential
  2. !-----------------------------------------------------------------------
  3. !Christina Urbanczyk Lab5 February 16, 2010
  4. ! This algorithm uses a factorial subprogram to calculate the &
  5. ! approximate value of exp(x) using the infinite series.
  6. ! Variables used are:
  7. ! count: DO-loop control variable
  8. ! FactorialNum: function subprogram to calculate factorials
  9. ! X: User determined value to find infinite series of
  10. ! Numits:Number of iterations
  11. ! Countits: Count iterations
  12. ! Tol:Convergence criteria
  13. ! Esum:Sum of Exp(x)
  14. ! Number:Subprogram variable
  15. ! FactorialNum:Factorial of integer
  16. !
  17. ! Input:X,Tol,Numits
  18. ! Output:FactorialNum
  19. !-----------------------------------------------------------------------
  20.  
  21. IMPLICIT NONE
  22. integer,parameter::dp=selected_real_kind(15) !15 significant figures
  23. integer:: count, Numits,Countits
  24. real(dp)::X, Esum, Tol
  25. character (len=1):: Method
  26.  
  27. Esum=1
  28.  
  29. write(*,*) "Please enter a value that you would like to find the infinite series of:"
  30. read(*,*) X
  31.  
  32. write(*,*) "Would you like to define the number of iterations (Y or N)?"
  33. read (*,*) Method
  34. IF (Method == "Y" .OR. Method== "y") THEN
  35. write (*,*) "What is your number of iterations?"
  36. read (*,*) Numits
  37.  
  38. !Display Trace Table
  39. write (*,'(T1,A7,T20,A9,T43,A8,T58,A5,T73,A6/)') "Count","X^count","Count!","Esum","Exp(X)"
  40. DO count=1,Numits
  41. Esum= Esum+(x**count)/(real(FactorialNum(Count)))
  42. write(*,'(T1,I2,T7,F20.1,T30,F20.1,T51,F15.5,T67,F15.5)') count,X**count,FactorialNum(Count),Esum, exp(X)
  43. END DO
  44. write(*,*) "Given",Numits,"iterations, the estimated value is",Esum,"."
  45. write(*,*) "The estimated value had",abs(exp(X)-Esum)/exp(X)*100.0,"% error from the actual value of",exp(x)
  46. ELSE IF (Method == "N" .OR. Method == "n") THEN
  47. write (*,*) "What is your convergence criteria?"
  48. read (*,*) Tol
  49.  
  50. !Display Trace Table
  51. write (*,'(T1,A7,T20,A9,T40,A8,T58,A5,T73,A6/)') "Count","X^count","Count!","Esum","Exp(X)"
  52. DO count=1,Countits
  53. Esum= Esum+(x**count)/(real(FactorialNum(Count)))
  54. write(*,'(T1,I2,T7,F20.1,T30,F20.1,T51,F15.5,T67,F15.5)') count,X**count,FactorialNum(Count),Esum, exp(X)
  55. IF ((abs(exp(X)-Esum))<Tol) EXIT
  56. Countits=Countits+1
  57. END DO
  58. write(*,*) "Given a tolerance of",Tol,"it took",Countits,"iterations."
  59. write(*,*) "The estimated value had",abs(exp(X)-Esum)/exp(X)*100.0
  60. write(*,*) "% error from the actual value of",exp(x),"."
  61. ELSE
  62. write(*,*) "Please define iterations or convergence criteria."
  63. END IF
  64.  
  65.  
  66. CONTAINS
  67. !--FactorialNum--------------------------------------------------------
  68. ! This program calculates the factorial Number! of Number which is 1 &
  69. ! if Number=0, 1*2*...*Number if Number>0.
  70. !
  71. ! Accepts:Number
  72. ! Returns:FactorialNum
  73. !----------------------------------------------------------------------
  74. FUNCTION FactorialNum (Number)
  75. Integer, INTENT (IN)::Number
  76. Integer::count
  77. real:: FactorialNum
  78.  
  79. !Calculate FactorialNum
  80. FactorialNum=1
  81. DO count=2, Number
  82. FactorialNum=FactorialNum*count
  83. END DO
  84.  
  85. END FUNCTION FactorialNum
  86.  
  87.  
  88. END
Success #stdin #stdout 0s 2624KB
stdin
0.8
-1
/
stdout
 Please enter a value that you would like to find the infinite series of:
 Would you like to define the number of iterations (Y or N)?
 Please define iterations or convergence criteria.