Pages

Wednesday, 8 May 2013

Projectile motion in Fortran - just for fun

Projectile motion was interesting for me in grade 11 Physics. Unfortunately at the time we did not have an experimental activity that could prove to me that the calculations work. However, later in grade 12 I used projectile motion calculation to determine the angle for our model rocket to fly the furthest (optimal angle is 45 degrees for furthest displacement). I thought it would be nice to have a Fortran code for the engineers out there involved in designing the parabolic water jets that dance in complicated manners. But I figured a trajectory could be plotted using gnuplot and Fortran. The internet probably has little or no use of this graph but I like its clean and simple look from gnuplot so here goes the graph for an object thrown at 45 degrees with 10 m/s velocity. The x-axis is horizontal distance and y-axis is vertical distance in metres, looks the object did not go that far...


 And the code, of course (credit goes to http://codeformatter.blogspot.ca/ for formatting my code to blog compatible text):

1:  !************************************************  
2:  !This program plots projectile motion of an object.  
3:  !The program requires user input for initial velocity   
4:  !and angle of the object.The algorithm uses a time   
5:  !step of 0.01 second i.e. it calculates object's  
6:  !location in the x and y plane every 0.01 second.  
7:  !**********By: Waleed Ishaque, 2013**************  
8:  program projectile_plot  
9:       implicit none  
10:       !Defining constants:  
11:       real, parameter :: pi = 3.142  
12:       real :: u, a, t  
13:       real, parameter :: g = 9.8  
14:       real:: x(150),y(150)  
15:       !where g is gravity, pi is "pi"  
16:       !u is object's initial velocity  
17:       !a is object's initial angle  
18:       !t is time during the simulation  
19:       !x and y are arrays with 150 rows  
20:       !Seek user input  
21:       write(*,*) 'Enter angle of projectile'  
22:       read*, a  
23:       write(*,*) 'Enter velocity of projectile'  
24:       read*, u  
25:       !Convert angle to radians  
26:       a = a*pi/180.0  
27:       !open .dat file and start writing on it using the algorithm  
28:       open(1, file='proj.dat')  
29:       integer :: i  
30:       do i=1,100  
31:            !displacement of object in x and y direction  
32:            t = (i*0.01)  
33:            x(i) = u*cos(a)*t  
34:            y(i) = u*sin(a)*t - 0.5*g*t*t  
35:            !write output in file "proj.dat" for plotting  
36:            write(1,*) x(i), y(i)  
37:            !kill the loop when the object hits the ground  
38:            if (y(i)<0) exit  
39:       end do  
40:       close(1)  
41:       !close file  
42:  end program projectile_plot