# Solving a differential equation for a variable in a function

This may sound like a very trivial question, but I need to solve a differential equation as follows:

a := 3.24077*10^-20 (* km \[Rule] Mpc *)
b := 3.16888*10^-14 (* s \[Rule] MYear *)
c := a/b*(2.99792*10^5) (*Mpc/ MYear*)
\[HBar] := a^2/b (1.05457*10^-37)(*kgMpc^2/MYear*)
H0 := a/b*71 (*1/MYear*)
G := a^3/b^2*6.67398*10^-20 (*Mpc^3/(Kg*MYear^2)*)
\[CapitalOmega]M := 0.27
\[CapitalOmega]\[Gamma] := 8.24*10^-4
\[CapitalOmega]\[CapitalLambda] := 0.73
\[Lambda] := 1/a^2 1*10^-58(*1/Mpc^2*)
f := 1*10^-4
y := 10
\[Rho]pert[r_] := \[Rho]*(1 + f*Exp[-(r/y)^2]) // N
M2[r_] := 4 \[Pi]*Integrate[\[Rho]pert[r1]*(r1)^2, r1] /. r1 -> r
E2[r_] :=
1/2*((H*r)/(c*(1000)))^2 - ((G *M2[r] (1000))/(
c^2 (r))) - (\[Lambda]*r^2)/(3 (1000)^2)
ScaleFactor2 =
NDSolve[SetPrecision[{(D[R2[r, t], t]) ==
Sqrt[(2*G*M2[r])/R2[r, t] + 2*c^2*E2[r] + (
2 c^2 \[Lambda]*R2[r, t]^2)/3], R2[r, 0] == r/1000}, 200],
R2, {r, 1, 20000}, {t, 1, 15000}, AccuracyGoal -> 2]
Rad2[r_, t_] := Evaluate[R2[r, t] /. ScaleFactor2]
RadPrime2[r_, t_] := D[Rad2[r1, t], r1] /. r1 ->  r
Raddot2[r_, t_] := D[Rad2[r, t], t]
***chi = DSolve[(c Sqrt[1 + 2 E2[r]])/RadPrime2[r, t], r, t]***


I am looking for a solution for the differential equation chi, but would like an answer whose subject is the variable r that appears in RadPrime2[r,t]. In other words, I am looking for the functional form of r(t)...

I apologise in advance if this seems really badly phrased and/or trivial, but I couldn't find anything in the literature that helped...

## 1 Answer

This constitutes only a beginning (if that) of an answer, maybe more an extended comment. You seem to have some problems with your code. First, I don't think you need to define any of the following as functions (i.e., defined using := ):

a, b, c, \[HBar], H0, G, \[CapitalOmega]M, \[CapitalOmega]\[Gamma], \
\[CapitalOmega]\[CapitalLambda], \[Lambda], f, y


A simple = ought to work fine for these as you don't pass them any inputs.

In your function E2[r_] you appear to have an undefined function, which you supply with an input: H. What do you intend H[] to return?

In ScaleFactor2 you use another function R2[r,t] that you have not defined.

You use R2[r,t] again in Rad2[]

I haven't thought through all of it but I also suggest you look at your uses of the following:

\[Rho] in \[Rho]pert

r1 in M2[r_]

r in the DSolve

I then suggest you methodically run the code line by line and determine where the problem(s) starts.

Update you question or clarify any of this in comments. It will make it easier to help you.