
Simulation Of Projectile Motion
Programming
(Implementing the Algorithm)
Using the physics model (this can be viewed under the Physics / Math Methodology tab) and programming skills we learnt from an external mentor, we will be able to write a computer program to simulate the dropping of bombs by possibly thousands or millions of planes, each with different reaction times, and compute the probability of which the bomb will hit a building:
Source Code
#include <stdio.h>
#include <stdlib.h>
#define last 1000000
#define d 7000.0
#define v 247
#define response_time 0.3
#define building_height 200.0
#define flight_altitude 4016
#define seed 51
main()
{
int i, this1;
float x, y, t, error;
int hits=0;
srand(seed);
for (i=0; i<last; i++)
{
this1 = rand();
if(this1 > 16383)
{
error = ((this1 - 16383)/16383.0)*(v*response_time);
x = d - error;
}
else
{
error = ((16383 - this1)/16383.0)*(v*response_time);
x = d + error;
}
t = x / v;
y = 0.5 * 9.8 * t * t;
if( y > (flight_altitude - building_height) && y < (flight_altitude)) hits++;
}
printf("\n Hit Probability = %.3f \n\n", (hits*100.0)/last);
}
So, what do the different lines of commands mean?
#include <stdio.h>
#include <stdlib.h>
#define last 1000000
#define d 7000.0
#define v 247
#define response_time 0.3
#define building_height 200.0
#define flight_altitude 4016
#define seed 51
main()
{
int i, this1;
float x, y, t, error;
int hits=0;
srand(seed);
for (i=0; i<last; i++)
{
this1 = rand();
These are the input values for some of the variables used in the simulation of the bomb being dropped by the aircraft.
last 1000000 refers to the number of times the simulation is looped / the number of aircrafts with the same reaction time dropping bombs (this value can be increased to increase the accuracy of the hit probability) i.e. there are 1000000 aircrafts dropping bombs
d 7000.0 refers to the horizontal distance (in metres) between the building and the perfect point of release where the hit probability is 100% i.e. the horizontal distance is 7000.0 metres.
v 247 refers to the horizontal velocity of the aircraft which released the bomb in metres per second i.e. the horizontal velocity of the aircraft is 247 metres/second
response_time 0.3 refers to the maximum delay time in which the pilot releases the bomb in seconds i.e. the maximum delay time is 0.3 seconds
building_height 200.0 refers to the height of the target building in metres i.e. the target building is 200.0 metres tall.
flight_altitude 4016 refers to the flight altitude of the aircraft releasing the bomb in metres i.e. the aircraft's flight altitude is 4016 metres.
Looping the simulation the number of times as defined under last (value) at the start of the program
Randomly generating a number for the reaction time of the pilot, which we assign it to the variable this1
Note: The randomly generated number will be an integer between 0 and 32767. We divide the randomly generated numbers into 2 groups, in which the randomly generated numbers more than 16383 would be in 1 group and the numbers less than 16383 would be in another group.
Declaring what variables are going to be used in the program
if(this1 > 16383)
{
error = ((this1 - 16383)/16383.0)*(v*response_time);
x = d - error;
}
The group of numbers larger than 16383 will be taken as a positive error distance i.e. the aircraft released the bomb after the release point in which the hit probability is 100%.
Afterwhich, we calculate the actual horizontal distance travelled by the bomb, which we assign to the variable x.
else
{
error = ((16383 - this1)/16383.0)*(v*response_time);
x = d + error;
}
The group of numbers smaller than 16383 will be taken as a negative error distance i.e. the aircraft released the bomb before the release point in which the hit probability is 100%.
Afterwhich, we calculate the actual horizontal distance travelled by the bomb, which we assign to the variable x.
t = x / v;
We then calculate the actual time taken for the bomb to hit the building, and assign the value to the variable t.
y = 0.5 * 9.8 * t * t;
Next, we calculate the vertical distance in which the bomb has to travel to be able to travel the required horizontal distance to hit the building, and assign the value to the variable y.
if( y > (flight_altitude - building_height) && y < (flight_altitude)) hits++;
}
We then ensure that the bomb hits the building, and if it does, we add a value of 1 to the variable hits++ each time it hits successfully.
printf("\n Hit Probability = %.3f \n\n", (hits*100.0)/last);
}
Lastly, we calculate the probability of which the bomb will hit the building, and provide the user with the hit probability.