Monday, August 22, 2016

Dot and Cross Product

Write a function to evaluate the cross product and dot product of two vectors V1 and V2 , where
 V= V1x+ V1y+ V1z and  V= V2x+ V2y+ V2zk .
 Note that this function returns a real array as its result .
Use this function to calculate the cross product and dot product of  V1=[-2 , 4 , 0.5]  and V2=[0.5 , 3 , 2] .

Program Dot_and_Cross_Product
    implicit none
    Integer :: i
    real :: a(3) , b(3) , dot , crossX , crossY , crossZ
    read(*,*) (a(i),i=1,3)
    read(*,*) (b(i),i=1,3)
    write(*,11) 'Dot Product =' , dot(a,b)
    11 format (A13 , F8.3)
    Write(*,12) 'Cross Product=' , crossX(a,b) , crossY(a,b) , crossZ(a,b)
    12 format ( A14 , f5.2 , 'i+' , f5.2 , 'j' , f5.2, 'k')
    end program

real function dot (a,b)
implicit none
real :: a(3) , b(3)
dot = a(1)*b(1)+a(2)*b(2)+a(3)*b(3)
end function

real function crossX(a,b)
implicit none
real :: a(3) , b(3)
Crossx = b(3)*a(2)-b(2)*a(3)
end function

real function crossY(a,b)
implicit none
real :: a(3) , b(3)
CrossY = b(1)*a(3)-a(1)*b(3)
end function

real function crossZ(a,b)
implicit none
real :: a(3) , b(3)
CrossZ = a(1)*b(2)-a(2)*b(1) 
end function



No comments:

Post a Comment