Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Converting an algorithm into an equation

  1. May 4, 2007 #1
    Hello. I've got a simply ballistics program with a very simply algorithm to deal with air friction. However, I am trying to find a mathematical forumula equivalent. Here is the algorithm:

    G = accel due to gravity (universal constant)
    F = projectile air friction factor (constant for a given projectile)
    T = delta T (delay between loops, varies depending on frame rate but is about 0.01)

    Projectile starts out traveling at velocity Vo fired at angle Ao.

    Velocity is broken into a vector made up of: Vx and Vy


    Ax = Vx * F
    Ay = Vy * F * G
    V = [Vx*Ax*T, Vy*Ay*T]

    Wait T
    goto Loop

    For any given projectile, I will know Vo and F.

    For now, I want an equation where I can specify a point (x,0), and it will give me the required Ao so that the trajectory intersects that point.

    Basically, given a specific distance, I want to know the angle required in order to land the projectile there (assuming the ground is flat and projectile is fired from y=0).

    I the approach I'm trying to take is to find equations that give the projectile's current position (x,y) based on the variables Vo, Ao, and F, and the constant G. I can do this easily in an Excel spreadsheet, but I'm not sure how to do it in an equation.

    I've come up with an acceleration forumula: Ax = Vx^2*F
    Basic physics says that velocity is the integral of acceleration. However, I'm confused about if/how I can integrate this, since acceleration depends on velocity.

    Can anyone help me out here? Thanks!
  2. jcsd
  3. May 7, 2007 #2
    Hmm... did I post this in the wrong forum? Do I need to provide more information on my problem? Or maybe I am just expecting a reply too quickly?
  4. May 7, 2007 #3
    If you want to include friction I don't think there is an easy solution to the equations of motion and you must, as you have done, solve the problem numerically. Without friction, there is an easy solution, which you prabably will be able to find using conservation of energy, or solving the (differential) equation of motion.

    You can also set op a differential equation (much like the program you encoded) including friction, but you will probably find no 'easy' solution.
  5. May 7, 2007 #4


    User Avatar
    Science Advisor

    Your algorithm makes little sense. In particular, Ay = Vy * F * G should almost certainly be something like Ay= G- Vy*F. Then you would have the differential equation
    [tex]\frac{d\vec{V}}{dt}= \vec{G}- F\vec{V}[/tex]
    where [itex]\vec{V}[/itex] is the vector with components Vx and Vy and [itex]\vec{G}[/itex] is the vector with components 0 and G.
  6. May 7, 2007 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I am bothered by the fact that your equations do not seem to be dimensionally consistent. That is your Ax cannot have the same dimensions as your Ay, so how can you combine them to get a velocity? What am I missing?
  7. May 7, 2007 #6
    Assuming that the coefficient F is positive, the correct algorithm should read:

    Code (Text):

    Ax = - Vx^2 * F
    Ay = - Vy^2 * F - G
    V = [Vx + Ax * T, Vy + Ay * T]

    Wait T
    goto Loop
    (where deceleration due to air friction is taken as proportional to square of velocity, as the OP properly mentions afterwards).

    As for finding the elevation Ao which gives desired range x, why not do it numerically as well? Just wrap another loop around this algorithm, which guesses the angle by bracketing/bisection search. For that you also need to detect inside the algorithm when the shot dips below level (use x = x + Vx * T, y = y + Vy * T as position updates).

    Chusslove Illich (Часлав Илић)
  8. May 8, 2007 #7
    Thanks for the replies! It's been years since I've taken any math, so my terminology is all incorrect. I definately didn't show you what was a vector and what was a number, so I understand why it didn't make sense. caslav.ilic is correct in his posting of the algorithm.

    I'll post from my code:

    // simple air friction
    Vector3 accel=_speed*(_speed.Size()*_airFriction); //accel = velocity * (magnitude of velocity) * airfriction
    accel[1] -= Type()->_coefGravity * G_CONST; //subtract G from vertical component of velocity
    _speed += accel*deltaT; //adjust velocity by accel * loop delay
    And the variables:
    _airFriction = depends on projectile type (default -0.0005)
    _coefGravity = 1 in this case
    _speed = vector representing current velocity of projectile in 3 dimensions

    My goal is to calculate or estimate, BEFORE the shot is taken, what angle the projectile needs to be fired in order to hit the ground X meters in front of it. Basically your classic highschool physics problem, only with simple air friction.

    I wanted to have a formula, rather than another loop before hand that guesses/checks, for a number of reasons, mainly processing speed.

    Hopefully that made things a little more clear. I do appreciate the help!
  9. May 9, 2007 #8
    It seems to me that the minus sign is still missing here. If you observe the shot trajectory, it should cover more ground distance before it reaches peak altitude, then on the way down.

    Well, the formula is kind of hard to get, I'm not sure it can be obtained. The square of velocity makes it a non-linear ODE (like in HallsofIvy's post, only with square drag term).

    But it shouldn't take much CPU time to do it numerically (of course, you wouldn't do the wait part of the loop here), something in the order of few millisec. Though, of course, it depends on what speed you really need. At worst, you could simply precompute ranges for, say, each 0.1 degree of elevation, and then linearly interpolate elevations for requested ranges when taking shots -- this would already run in a matter of few microsec.

    Chusslove Illich (Часлав Илић)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook