# Retrieving values from InterpolatingFunction

I have code for finding the flight of a golf ball and I would like to retrieve the values of horizontal displacement x[t] and at the time when the golf ball hits the ground (y[t]=0, when t=tmax)

(* Question 1. (B) *)
(* Experimenting with different life and drag coefficiants cl and cd, respectively, and different starting angles \[Theta] to study the behaviour of the motion of the golf ball *)

Clear["Global*"]

(* Defining the variables density of air at 20C \[Rho], the cross-sectional area of the golf ball A, acceleration due to gravity g, initial velocity u and the mass of the golf ball m *)

\[Rho]=1.2014; A=(0.02^2*Pi); g=9.81; u=60; m=0.05;

(* Defining the proportionality factor \[Alpha] by half the density of air \[Rho] multiplied with the cross-sectional area A *)

\[Alpha]=1/2*\[Rho]*A;

(* Setting the solution of the differential equations to be sol which can be called with the number of the solution n, the drag coefficiant cd, the lift coefficiant cl and the initial angle \[Theta]. Using the Block\[Rule]RecursionLimit funtion to set the limit of recursions to infinity *)

sol[n_,cd_,cl_,\[Theta]_]:=Block[{\$RecursionLimit=Infinity},

(* Using NDSolve function with all the previous differential equations and initial conditions to calculate the numerical solution to the differential equations *)

NDSolve[{
x''[t]==-\[Alpha]*(1/m)*v[t]*x'[t]*(cl*Sin[P[t]]+cd*Cos[P[t]]),
y''[t]==-g-\[Alpha]*(1/m)*v[t]*y'[t]*(cd*Sin[P[t]]-cl*Cos[P[t]]),
v[t]==Sqrt[x'[t]^2+y'[t]^2],
P[t]==ArcTan[y'[t]/x'[t]],P[0]==\[Theta],
y'[0]==u*Sin[P[0]],x'[0]==u*Cos[P[0]],
y[0]==x[0]==0,

(* Using the WhenEvent function to set a new variable tmax that indicates when the solutions dips below the x-axis, which would be when the golf ball hits the ground *)

WhenEvent[y[t]==0,tmax[n]=t]},

(* Telling the NDSolve function to calculate the vertical distance y[t] and the horizontal distance x[t] as a function of time *)

{x[t],y[t]},

(* Setting the time limit to a large number of t=200 as to make sure the *)

{t,0,200}]];


As can be seen, I would like to retrieve the x[tmax[n]] value from the interpolating function returned from NDsolve function. I have tried numerous things, but i can't seem to find a solution.

• have a look at this mathematica.stackexchange.com/a/137787/2079 Oct 3 '17 at 11:53
• I don't understand. Aren't you storing the time in tmax[n]? If I run sol[1, 1, 1, 1] and then evaluate tmax[1], it returns 6.54454, and when I do ParametricPlot[{x[t], y[t]} /. sol[1, 1, 1, 1] // Evaluate, {t, 0, tmax[1]}], I get a nice plot showing the trajectory from launch to when it hits the ground. Finally, just do x[t] /. First@sol[1, 1, 1, 1] /. t -> tmax[1]`. Oct 3 '17 at 15:57