Fortran Code to calculate dot and cross products of 3D vectors

Here I have written a Fortran code to calculate the dot and cross products of two 3D vectors. Actually this was an assignment given in the Fortran lectures given by Bilkent IEEE/BilCEM. So here is the code for future me and of course for you.

program hw

 real v1(3)
 real v2(3)
 character choice
 double precision r1, r2, r3, magnitude

 write(*,*) 'Welcome to 3-D vector multiplier'
 write(*,*) 'Please enter first vector'

 do 10 i = 1, 3
 write(*,*) 'element ', i, ': '
 read(*,*) v1(i)
10 continue

write(*,*) 'Now enter second vector'

 do 20 j = 1, 3
 write(*,*) 'element ', j, ': '
 read(*,*) v1(j)
20 continue

write(*,*) 'Please enter the operation'
 +you want. "c" for cross, "d" for dot.'

 read(*,*) choice

 if(choice .EQ. 'c') then
 r1 = v1(2)*v2(3)-v2(2)*v1(3)
 r2 = v2(1)*v1(3)-v1(1)*v2(3)
 r3 = v1(1)*v2(2)-v2(1)*v1(2)
 write(*,*) 'Result: '
 write(*,*) r1, r2, r3
 write(*,*) 'Magnitude: '
 magnitude = sqrt((abs(r1))**2+(abs(r2))**2+(abs(r3))**2)
 write(*,*) magnitude
 write(*,*) 'Direction: '
 write(*,*) r1/magnitude, r2/magnitude, r3/magnitude
 elseif(choice .EQ. 'd') then
 write(*,*) 'Result of dot product: '
 write(*,*) v1(1)*v2(1)+v1(2)*v2(2)+v1(3)*v2(3)
 else
 write(*,*) 'Sorry this operation is not defined!'
 endif

 stop
 end

Lastly, here you can also download the code as a file which may happen to be more organized.



Bu ve buna benzer yazılardan haberdar olmak için kaydol:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>