Silverfrost Forums

Welcome to our forums

Trigonometric Functions COS and SIN.

31 Oct 2011 12:17 #9164

My program is calculating incorrect numbers for the following code:

DLS1(K)=COS(INC(K-1)) DLS2(K)=COS(INC(K)) DLS3(K)=SIN(INC(K-1)) DLS4(K)=SIN(INC(K)) DLS5(K)=COS(AZM(K)-AZM(K-1))

DLS(K)=ACOS(DLS1(K)*DLS2(K)+DLS3(K)*DLS4(K)DLS5(K))(100/CL(K))

K is an array of angles. Any help with trig functions?

I took the COS of several angles on my calculator and they are not the same as what the program calculates.

Jim

31 Oct 2011 1:35 #9165

Specified in degrees.

31 Oct 2011 2:56 #9167

Trig functions in fortran use radians not degrees.

31 Oct 2011 3:09 #9168

As John mentioned, you need radians. Use

alfa_new = alfa_old*datan(1.D0)/x with

x = 45.D0 for input values in degrees x = 50.D0 for ... grads x = 800.D0 for ... mils

Regards - Wilfried

31 Oct 2011 5:41 #9169

In addition to the other points that have been noted, make sure that the arguments to the trigonometric functions are REAL or DOUBLE PRECISION, not INTEGER (which INC would be, by default).

18 Aug 2012 8:37 #10650

I have used COSD(Arg) and SIND(Arg) with Arg in Degrees with Intel compilers, but I guess these are extensions not supported by FTN95. They must be non-standard extensions.

19 Aug 2012 9:25 #10651

... but they must be among the easiest routines to write yourself!

(Well, they were in Fortran 77. Now, you have to make them generic, I suppose)

Eddie

20 Aug 2012 3:51 #10654

Here is a possible way to have it generic using a module, although I wonder how useful it is ? module degrees ! real10, parameter :: deg_to_rad = 0.0174532925199432957692_3 ! radians per degree ! interface sin_d module procedure sin_d4, sin_d8, sin_d10 end interface sin_d interface cos_d module procedure cos_d4, cos_d8, cos_d10 end interface cos_d ! contains ! real4 function sin_d4 (x) real4 :: x sin_d4 = sin (xdeg_to_rad) end function sin_d4 !
real8 function sin_d8 (x) real8 :: x sin_d8 = sin (xdeg_to_rad) end function sin_d8 !
real
10 function sin_d10 (x) real10 :: x sin_d10 = sin (xdeg_to_rad) end function sin_d10 ! real4 function cos_d4 (x) real4 :: x cos_d4 = cos (xdeg_to_rad) end function cos_d4 !
real
8 function cos_d8 (x) real8 :: x cos_d8 = cos (xdeg_to_rad) end function cos_d8 !
real10 function cos_d10 (x) real10 :: x cos_d10 = cos (x*deg_to_rad) end function cos_d10 !
end module degrees

PROGRAM deg
!
use  degrees
!
INTEGER i
real*4 x4
real*8 x8
real*10 x10
!
real*10 one, forty_five
  one = 1
  forty_five = 45
  write (*,*) deg_to_rad, atan (one) / forty_five, deg_to_rad - atan (one) / forty_five
!
DO i=0,90,5
  x4 = i
  x8 = i
  x10 = i
  write (*,*) i, sin_d(x4), sin_d(x8), sin_d(x10)
end do
DO i=0,90,5
  x4 = i
  x8 = i
  x10 = i
  write (*,*) i, cos_d(x4), cos_d(x8), cos_d(x10)
end do

end
Please login to reply.