Solving for initial velocity required to launch a projectile to a given destination at a different height I need to calculate the initial velocity required to launch a projectile at a given angle from point A to point B. The only force acting on the projectile after launch will be gravity – zero air resistance. The projectile is launched within a simulated, virtual environment; however, I am asking for help with the physics rather than the simulation itself.
I have had success (with a different equation) when point A and point B are at the same height; however, once point B is at a different height, my calculations become less precise – well, wrong in fact.
I researched the following formula for finding the range of a projectile on uneven ground. The parameters are all available to me, except the initial velocity that I need to solve for.

$d$ : range or distance
  $v_i$ : initial velocity
  $g$ : gravity
  $\theta$ : launch angle
  $y_0$ : launch height

$$d = \frac{v_i \cos\theta}{g}(v{_i} \sin \theta + \sqrt{(v_i \sin\theta)^2 + 2gy_0})$$
I attempted to solve for initial velocity (eq: A):
$$v_i = \sqrt{\frac{d^2g}{2\cos\theta^2(y_0+d \tan\theta)}}$$  
Using this equation in my simulation I apply the velocity to a normalised displacement vector and launch the projectile. It gets close to its target but:


*

*$x_{final}$ is always correct

*$z_{final}$ is always incorrect - it is close to the desired $z$ but always offset by a seemingly proportional amount. 


I have spent many hours trying to review the equation but I have been unsuccessful. Any advice would be most appreciated:


*

*is this the correct equation?

*have I solved for $v_i$ correctly? I have looked for an example equation online but I have not found one structured how I need it, i.e. solving for $v_i$

*I have spent hours researching online - perhaps I have missed some good (but entry-level) resources. Do you know of any reference material that may help me?


Update - I have now named the equation above for referencing below
Following on from the answer by @Pygmalion, which I am still gratefully working to understand:
I agree that your derived equation is equivalent to mine (A). Using yours (@Pygmalion's) in my simulation, I therefore observe the same failures: the projectile always lands short of the target.
I have simplified the simulation keeping the launch height and target height the same. I still encounter the same problem with equation A; however, when passing the same parameters to the following equation the projectile always hits the target precisely (eq: B):
$$v_i = \sqrt{\frac{dg}{\sin2\theta}}$$
Given the success of equation B when the launch and target heights are the same and the failure of equation A given the same conditions, I question whether equation A is the correct one to solve the problem since I would expect it to work for any combination of launch|target height.
I think the help I need is around why equation B works. Does it cater for conditions that  equation A overlooks? If equation A is unsuitable, are you able to recommend an alternative?
In short, the $v_i$ calculated by equation A (in my simulations) is always less than $v_i$ from equation B given the same inputs - the projectile therefore always falls short of its target.
 A: This types of problems are solved by observing projectile movements in $x$ and $y$ direction separately.  In $x$ direction you have constant velocity movement
$$v_x = v_{x0} = v_0 \cos(\theta), \; (1)$$
$$x = v_{x0} t +x_0 = v_0 \cos(\theta) \; t +x_0, \; (2)$$
and in $y$ direction you have constant acceleration movement with negative acceleration $-g$
$$v_y = - g t + v_{y0} = - g t + v_0 \sin(\theta), \; (3)$$
$$y = - \frac{1}{2} g t^2 + v_{y0} t + y_0 = - \frac{1}{2} g t^2 + v_0 \sin(\theta) \; t + y_0. \; (4)$$
Your initial conditions are 
$$x_0 = 0, \; y_0 \ne 0,$$
and final conditions (at moment $t=T$ projectile falls back on the ground) are
$$t = T, \; x = d, \; y = 0.$$
If you put initial and final conditions into equations (2) and (4) you end up with two equations and two unknowns $v_0, T$.  By eliminating $T$ you get expression for $v_0$.
My calculations show that 
$$v_0 = \frac{1}{\cos(\theta)}\sqrt{\frac{\frac{1}{2} g d^2}{d \tan(\theta)+y_0}}$$
which is I believe equal to your equation.  Maybe your problem is that $d$ means displacement in direction $x$, while the total displacement is $\sqrt{d^2+y_0^2}$?
