# Probability density and average of distance traveled by falling object

ClearAll["Global*"]

time = Solve[x - (1/2)*g*(t^2) == 0, t]

position = Solve[x - (1/2)*g*(t^2) == 0, x]

v = D[(g t^2)/2, t]

T = Sqrt[2*h/g]

dt = D[(Sqrt[2] Sqrt[x])/Sqrt[g], x]

Ptdt1 = dt1/T // PowerExpand

Ptdt =
Replace[Ptdt1, {t -> (Sqrt[2] Sqrt[x])/Sqrt[g],
dt1 -> (1/(Sqrt[2] Sqrt[g] Sqrt[x]))*dx}, All]

rhot = Ptdt/Ptdt[[2]]

Integrate[rhot, {x, 0, h}]

Ex = Integrate[x*rhot, {x, 0, h}]

h = 20

Plot[rhot, {x, 0, h}, AxesOrigin -> {0, 0}]


Please see my code to do a problem in Griffith's quantum mechanics. But I have a feel that my code is really lengthy and is not general. Is there a way to make this general? or any easy alternatives?

You mainly need TransformedDistribution:

xrule = DSolve[{x'[t] == g t, x[0] == 0}, x, t][[1]]
(* {x -> Function[{t}, (g t^2)/2]} *)

grule = Solve[x[T] == h /. xrule, g][[1]]
(* {g -> (2 h)/T^2} *)

dist = TransformedDistribution[x[t] /. xrule,
t \[Distributed] UniformDistribution[{0, T}]];

Assuming[{T > 0, h > 0}, PDF[dist, x] /. grule // Simplify]
(* Piecewise[{{1/(2 Sqrt[h x]), x > 0 && h > x}}, 0] *)

Expectation[x, x \[Distributed] dist] /. grule
(* h/3 *)


If a problem can easily be solved analytically, you should not use the PC. You learn more from the analytical calculation.

Given height: h and the acceleration: g, the distance after time t is:

s[t_]= 1/2 g t^2


That means the maximal falling time is:

tmax= Sqrt[2h/g]


If we now calculate the mean of s[t] over t:

Mean s = 1/tmax Integrate[1/2 g t^2,{t,0,tmax}]
= 1/tmax 1/2 g tmax^3/3 = 1/6 g tmax^2= h/3
`