Back to Problem Solutions forum
Short description of my problem? I seem to get distances that are about 25-50% too long each time. For example, with the example input...
Accepted answer: 96 68 120 60 88 120 97 68 76
My answer: 159 91 152 82 115 147 148 108 94
I'm using object oriented programming and numeric integration to solve the problem. I assume projectiles start at the origin, fly in quadrant I and positive vectors are to the right and up (e.g. d2y = -9.8).
My projectile class has an x-component and y-component for three dimensions (e.g. x = 0, dx = v * cos a, d2x = 0 at initialization). My pseudo code is this:
My projectile.update(dt) function takes an amount of time to integrate across, then does the following:
My main function has the following pseudocode:
For each elevation profile of 3 profiles:
Read the profile from standard input
Transform profile from a list of n 4 meter blocks to a list of 4n 1 meter blocks
For each shot of 3 shots:
Read the projectile's velocity and angle from standard input
Create an instance of Projectile class for that shot
While the shot is at t=0 or its y > elevation[floor(x)]:
If the shot is within one integration unit of impact:
dt /= 10 //get a more accurate measurement of impact time and location
Update the projectile for dt time
Add the impact x value to the list of impacts
Print the impacts as required by the problem statement
Am I doing this right? Or is there a better way to do this and calculate the impact directly without numeric integration?
For purposes of troubleshooting, here is some data.
The results of transforming the elevation profile (slope) is:
From: 0 0 0 0 0 1 1 1 1 2 2 2 2 2 2 3 4 5 6 6 7 8 9 9 10 10 10 10 10 10 11 11 12 13 14 15 16 16 17 18
To: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15 15 15 16 16 16 16 16 16 16 16 17 17 17 17 18 18 18 18
I think my constructor function is correct. For the first shot, v = 42, a = 42, I get:
t: 0.000 x: 0.00 y: 0.00 dx: 31.21 dy: 28.10 d2x: 0.00 d2y: -9.81 vel: 42 ang: 42 elevation: 0
I also think my update function is correct. For dt = 0.1, after one integration, I get:
t: 0.100 x: 3.12 y: 2.81 dx: 31.21 dy: 27.12 d2x: 0.00 d2y: -9.81 vel: 41 ang: 40 elevation: 0
At impact for the first profile, first shot I have the following data:
t: 5.111 x: 159.52 y: 18.01 dx: 31.21 dy: -22.04 d2x: 0.00 d2y: -9.81 vel: 38 ang: -35 elevation: 18
t: 5.112 x: 159.56 y: 17.99 dx: 31.21 dy: -22.05 d2x: 0.00 d2y: -9.81 vel: 38 ang: -35 elevation: 18
Hi Matthew,
Are your elevation figures in metres or cubes?
At impact for the first profile, first shot, your data shows x=159 & elevation=18.
However the cubes have a side of 4 metres, so at x=159 the height of the cubes is 72 metres.
That was part of it. I updated the part where I parse the slope into a meter-by-meter elevations list. Now the elevations are in 4 meter steps.
Still doesn't solve the problem entirely. It brings me very close. Here's what I got after fixing the elevations problem:
Expected output: 96 68 120 60 88 120 97 68 76
My output: 108 68 120 60 89 120 97 68 76
I had a similar outcome when I used the actual problem data. Matches on most, very close on the rest. Input:
0 0 0 0 0 0 0 1 2 2 2 2 2 2 3 4 4 4 4 5 5 6 7 8 8 9 10 10 10 10 11 11 11 12 13 13 14 15 15 16
30 32
42 44
32 49
0 0 0 0 0 0 0 1 2 2 2 3 3 4 5 5 6 7 7 8 8 9 10 11 11 11 12 12 12 13 13 13 13 13 13 14 15 16 17 17
36 67
45 60
43 62
0 0 0 0 0 1 1 2 2 3 3 4 5 5 5 6 7 7 7 8 8 9 10 11 12 13 14 15 15 15 15 15 15 15 15 16 16 17 17 17
39 51
34 41
39 45
Expected output:
56 114 81 78 140 120 94 64 88
My output:
58 120 84 78 140 120 94 64 88
I remember on "Safe Landing", we got a bit of a 'slop' factor in acceptable ranges of answers. Why do we have to match exactly here?
Let's take the first example and the first shot, where the expected answer is 96 and your answer is 108.
The appropriate equation is
y=y0 + x*tan(a) - (g*x*x)/2*(v*cos(a))^2
where:
y0 is the initial height, i.e. 0
x is the horizontal distance
g is gravity
v is the initial velocity, i.e. 42 m/s
a is the angle, i.e. 42 degrees
y is the height at distance x
If we try x=96, then y is 40.03, which is slightly higher than the cubes at that distance, which is 40.
If we try x=97, then y is 39.96, which is slightly lower than the cubes at that distance, which is 40.
As the answer has to be rounded down, 96 is the correct answer.
So I tried using your direct calculation function of y(x) = y0 + x*tan(a) - (g*x*x)/2*(v*cos(a))^2
. It seems to have fixed the first case, but let's look at slope 0, shot 1 (v=42, a=44).
The accepted answer is impact at x=68. But here's the results of the direct calculation:
x= 67, y = 16.430351241442786, slope elevation = 16.0
x= 68, y = 16.26536937977803, slope elevation = 20.0
So I think that the answer is 67, no? It looks like the projectile hits the side of the step. Same thing happens on slope 0, shot 2. The accepted answer is x = 120. Here are the results of the calculation:
x = 119, y = 43.29930816948277 , slope elevation = 40.0
x = 120, y = 42.73938783509884, slope elevation = 44.0
Anyways, for the sample data, I now return the result 96 67 119 59 88 120 97 67 76
Thanks for the continued help, Quandray.
Hi Matthew,
In those cases, yes, it hits the vertical, but the x values when it hits the vertical are exactly 68 & 120, so no rounding is needed.