Projectile motion where gravity is a function of $y$ I know what are the laws that describe the projectile motion, and in particular I need to compute the vertical displacement of the projectile at any time $t$:
$$ y = y_0 + v_0\sin(\theta)\,t - \frac{1}{2}gt^2 $$
But (I assume in my problem that) the gravity acceleration $g$ is not a constant:
$$ g = \frac{Gm}{r^2} $$
Therefore, I can write the first equation as:
$$ y = y_0 + v_0\sin(\theta)\,t - \frac{K}{y^2}t^2 \quad , \quad K = \frac{Gm}{2} $$
$$ y^3 - (y_0 + v_0\sin(\theta)\,t) \cdot y^2 + Kt^2 = 0 $$
Now I always need to get the vertical displacement of the projectile at any time $t$, and I tried to solve the equation by means of the cubic function formula, but I get unfeasible values, so I think there must be some error.
I'm wondering where it is.
 A: Presumably, you are taking your $y$ value to be from the ground, however the value of $g$ depends on the value of $r$, which is the radius of the earth from the center, not the distance from the ground. You should do something like $r = r_{earth} + y$
A: The problem can be solved using differential equations. If at any point the acceleration is a function of height such as
$$ a(y) = - \frac{G M}{(R+y)^2} $$
with initial conditions $t_0$, $y_0$ and $v_0$ the equations that govern the motion are
$$  a(y) = \frac{{\rm d}v}{{\rm d}t} = \frac{{\rm d}v}{{\rm d}y} \frac{{\rm d}y}{{\rm d}t} = v \frac{{\rm d}v}{{\rm d}y}\Rightarrow \int \limits_{v_0}^v v {\rm d}v= \int \limits_{y_0}^y a(y)\,{\rm d}y$$
$$ \frac{1}{2} v^2 - \frac{1}{2} v_0^2 =-GM \frac{y-y_0}{(R+y_0)(R+y)} $$
(see https://physics.stackexchange.com/a/41759/392)
Once you solve in terms of $v(y)= \sqrt{ v_0^2 + \frac{2 GM}{R+y} + \frac{2 GM}{R+y_0}}$ you can find the time by
$$ t-t_0 = \int \limits_{y_0}^{y} \frac{1}{v(y)}\,{\rm d}y $$ which is not an easy integral to solve.
A: *

*Increment time $t = t + dt$

*Increment coordinates. $x = x + v_x dt, y = y + v_y dt$

*Recompute force $a(x,y)=F/m$ 

*Increment speed $v_i=v_i + a_i dt$

*Add here whatever gravity you want with any forces.

*Close above in loop on any computer.

*Do this every time teachers want you to solve yet another problem of projectiles and save your time.

