VECTOR2.f95
PROGRAM VECTOR2
!
! Purpose: Reads in two vectors and calculates their dot product and
! their angle.
!
IMPLICIT NONE
REAL:: R1(3),R2(3)
REAL:: DOT_PRODUCT,RLEN1,RLEN2,COSINE,ANGLE,PI
INTEGER:: I
!
! Read in the components of vector 1, then the components of vector 2.
DO I=1,3
WRITE(*,11,ADVANCE='NO')I
READ(*,*)R1(I)
END DO
DO I=1,3
WRITE(*,12,ADVANCE='NO')I
READ(*,*)R2(I)
END DO
!
! Dot product and lengths of the two vectors
DOT_PRODUCT=R1(1)*R2(1)+R1(2)*R2(2)+R1(3)*R2(3)
WRITE(*,20)DOT_PRODUCT
!
! Calculate the angle. First calculate the length of the two vectors
! and check that neither R1 nor R2 has zero length
RLEN1=SQRT(R1(1)**2+R1(2)**2+R1(3)**2)
RLEN2=SQRT(R2(1)**2+R2(2)**2+R2(3)**2)
IF(RLEN1*RLEN2 == 0.0)THEN
WRITE(*,*)'R1 or R2 has zero length.'
ELSE
! Cosine of the angle between the two vectors.
COSINE=DOT_PRODUCT/(RLEN1*RLEN2)
! Angle (in radians) between the two vectors.
ANGLE=ACOS(COSINE)
! Convert to degrees and print the result.
PI=4.0*ATAN(1.0)
ANGLE=ANGLE*180.0/PI
WRITE(*,30)ANGLE
END IF
!
11 FORMAT('Enter component ',I2,' of vector 1 : ')
12 FORMAT('Enter component ',I2,' of vector 2 : ')
20 FORMAT('The dot product of the two vectors is ',E15.7)
30 FORMAT('The angle between the two vectors is ',E15.7, ' deg.')
STOP
END PROGRAM VECTOR2