Saturday, May 11, 2013

Trigonometric Functions Using Degree


Trigonometric Functions Using Degree

Problem Statement

Trigonometric functions (i.e.sin(x) and cos(x)) use radian for their argument. Using modules, you can design your own trigonometric functions that use degree. Write a module that contains functions for converting radian to degree and degree to radian and sin(x) and cos(x) with arguments in degree rather than in radian. 

Solution

The following is the desired module:
! --------------------------------------------------------------------
! MODULE  MyTrigonometricFunctions:
!    This module provides the following functions and constants
!    (1) RadianToDegree()     - converts its argument in radian to
!                               degree
!    (2) DegreeToRadian()     - converts its argument in degree to
!                               radian
!    (3) MySIN()              - compute the sine of its argument in
!                               degree
!    (4) MyCOS()              - compute the cosine of its argument
!                               in degree
! --------------------------------------------------------------------

MODULE  MyTrigonometricFunctions
   IMPLICIT   NONE

   REAL, PARAMETER :: PI        = 3.1415926       ! some constants
   REAL, PARAMETER :: Degree180 = 180.0
   REAL, PARAMETER :: R_to_D    = Degree180/PI
   REAL, PARAMETER :: D_to_R    = PI/Degree180

CONTAINS

! --------------------------------------------------------------------
! FUNCTION  RadianToDegree():
!    This function takes a REAL argument in radian and converts it to
! the equivalent degree.
! --------------------------------------------------------------------

   REAL FUNCTION  RadianToDegree(Radian)
      IMPLICIT  NONE
      REAL, INTENT(IN) :: Radian

      RadianToDegree = Radian * R_to_D
   END FUNCTION  RadianToDegree

! --------------------------------------------------------------------
! FUNCTION  DegreeToRadian():
!    This function takes a REAL argument in degree and converts it to
! the equivalent radian.
! --------------------------------------------------------------------

   REAL FUNCTION  DegreeToRadian(Degree)
      IMPLICIT  NONE
      REAL, INTENT(IN) :: Degree

      DegreeToRadian = Degree * D_to_R
   END FUNCTION  DegreeToRadian

! --------------------------------------------------------------------
! FUNCTION  MySIN():
!    This function takes a REAL argument in degree and computes its
! sine value.  It does the computation by converting its argument to
! radian and uses Fortran's sin().
! --------------------------------------------------------------------

   REAL FUNCTION  MySIN(x)
      IMPLICIT  NONE
      REAL, INTENT(IN) :: x

      MySIN = SIN(DegreeToRadian(x))
   END FUNCTION  MySIN

! --------------------------------------------------------------------
! FUNCTION  MySIN():
!    This function takes a REAL argument in degree and computes its
! cosine value.  It does the computation by converting its argument to
! radian and uses Fortran's cos().
! --------------------------------------------------------------------

   REAL FUNCTION  MyCOS(x)
      IMPLICIT  NONE
      REAL, INTENT(IN) :: x

      MyCOS = COS(DegreeToRadian(x))
   END FUNCTION  MyCOS

END MODULE  MyTrigonometricFunctions
Click here to download this program.
Here is the main program:
! -----------------------------------------------------------------------
! PROGRAM  TrigonFunctTest:
!    This program tests the functions in module MyTrigonometricFunctions.
! Module MyTrigonometricFunctions is stored in file trigon.f90.
! Functions in that module use degree rather than radian.  This program
! displays the sin(x) and cos(x) values for x=-180, -170, ..., 0, 10, 20,
! 30, ..., 160, 170 and 180.  Note that the sin() and cos() function
! in module MyTrigonometricFunctions are named MySIN(x) and MyCOS(x).
! -----------------------------------------------------------------------

PROGRAM  TrigonFunctTest
   USE  MyTrigonometricFunctions        ! use a module

   IMPLICIT  NONE

   REAL :: Begin = -180.0               ! initial value
   REAL :: Final =  180.0               ! final value
   REAL :: Step  =   10.0               ! step size
   REAL :: x

   WRITE(*,*)  'Value of PI = ', PI
   WRITE(*,*)
   x = Begin                            ! start with 180 degree
   DO
      IF (x > Final)  EXIT              ! if x > 180 degree, EXIT
      WRITE(*,*)  'x = ',  x, 'deg   sin(x) = ', MySIN(x), &
                  '   cos(x) = ', MyCOS(x)
      x = x + Step                      ! advance x
   END DO

END PROGRAM  TrigonFunctTest
Click here to download this program. 

Program Input and Output

The following is the output from the above program.
Value of PI = 3.1415925

x = -180.deg   sin(x) = 8.742277657E-8   cos(x) = -1.
x = -170.deg   sin(x) = -0.173648298   cos(x) = -0.98480773
x = -160.deg   sin(x) = -0.342020214   cos(x) = -0.939692616
x = -150.deg   sin(x) = -0.50000006   cos(x) = -0.866025388
x = -140.deg   sin(x) = -0.642787635   cos(x) = -0.766044438
x = -130.deg   sin(x) = -0.766044438   cos(x) = -0.642787635
x = -120.deg   sin(x) = -0.866025388   cos(x) = -0.50000006
x = -110.deg   sin(x) = -0.939692616   cos(x) = -0.342020124
x = -100.deg   sin(x) = -0.98480773   cos(x) = -0.173648193
x = -90.deg   sin(x) = -1.   cos(x) = -4.371138829E-8
x = -80.deg   sin(x) = -0.98480773   cos(x) = 0.173648223
x = -70.deg   sin(x) = -0.939692616   cos(x) = 0.342020154
x = -60.deg   sin(x) = -0.866025448   cos(x) = 0.49999997
x = -50.deg   sin(x) = -0.766044438   cos(x) = 0.642787635
x = -40.deg   sin(x) = -0.642787576   cos(x) = 0.766044438
x = -30.deg   sin(x) = -0.5   cos(x) = 0.866025388
x = -20.deg   sin(x) = -0.342020124   cos(x) = 0.939692616
x = -10.deg   sin(x) = -0.173648179   cos(x) = 0.98480773
x = 0.E+0deg   sin(x) = 0.E+0   cos(x) = 1.
x = 10.deg   sin(x) = 0.173648179   cos(x) = 0.98480773
x = 20.deg   sin(x) = 0.342020124   cos(x) = 0.939692616
x = 30.deg   sin(x) = 0.5   cos(x) = 0.866025388
x = 40.deg   sin(x) = 0.642787576   cos(x) = 0.766044438
x = 50.deg   sin(x) = 0.766044438   cos(x) = 0.642787635
x = 60.deg   sin(x) = 0.866025448   cos(x) = 0.49999997
x = 70.deg   sin(x) = 0.939692616   cos(x) = 0.342020154
x = 80.deg   sin(x) = 0.98480773   cos(x) = 0.173648223
x = 90.deg   sin(x) = 1.   cos(x) = -4.371138829E-8
x = 100.deg   sin(x) = 0.98480773   cos(x) = -0.173648193
x = 110.deg   sin(x) = 0.939692616   cos(x) = -0.342020124
x = 120.deg   sin(x) = 0.866025388   cos(x) = -0.50000006
x = 130.deg   sin(x) = 0.766044438   cos(x) = -0.642787635
x = 140.deg   sin(x) = 0.642787635   cos(x) = -0.766044438
x = 150.deg   sin(x) = 0.50000006   cos(x) = -0.866025388
x = 160.deg   sin(x) = 0.342020214   cos(x) = -0.939692616
x = 170.deg   sin(x) = 0.173648298   cos(x) = -0.98480773
x = 180.deg   sin(x) = -8.742277657E-8   cos(x) = -1.

Discussion

  • Module MyTrigonometricFunctions defines four constants:
    1. PI,
    2. Degree180,
    3. R_to_D for radian to degree conversion, and
    4. D_to_R for degree to radian conversion.
  • Module MyTrigonometricFunctions contains four functions:
    1. RadianToDegree() converts its radian argument to degree;
    2. DegreeToRadian() converts its degree argument to radian;
    3. MySIN() takes a degree argument and returns its sine value; and
    4. MyCOS() takes a degree argument and returns its cosine value.
  • In the module, MySIN(x) first converts its degree argument x to radian using function DegreeToRadian() and supplies the result to the Fortran SIN() function for computing the value of sine.
  • The module uses MySIN() and MyCOS() rather than the same Fortran names SIN() and COS() to avoid confusion.
  • As described in the use of modules, the main program can use variables, PARAMETERs and functions declared and defined in a module. Thus, the main program can use the value of PARAMETER PI defined in module MyTrigonometricFunctions. This is why there is no declaration of PI in the main program.
  • In the main program, REAL variable x starts with 180 (in degree) and steps through 180 (in degree) with a step size 10. For each value of x, its SIN() and COS() is computed.
  • If the main program and module MyTrigonometricFunctions are stored in files trigon.f90 and tri-test.f90, respectively, then you can compile them together with the following command:
    f90 trigon.f90 tri-test.90
    
    or with the following that generates an executable called tri-test:
    f90 trigon.f90 tri-test.90 -o tri-test



Subscribe to Our Blog Updates!




Share this article!

No comments:

Post a Comment

=(*_*)------------------------(^_^)=
:::::|berkomentar dengan sopan adalah akhlak kemulian|:::::

Return to top of page
Powered ByBlogger | Design by PARMAN | Blogger Template by UKK As-Siraaj