3 simplified answer
source | link

The order of the expressions matters. DefiningEquation2 before defining gg[x] yields

Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, whilewhen g[x] is subsequently defined, Derivative[1][g][f[x]] is unable to interpret it and returns unevaluated.

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

This looks like an additional dependent variable to NDSolve, which produces an error message. On the other hand, if g[x] is defined first, then D[g[f[x]], f[x]] processes it immediately when Equation2 is subsequently defined

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

asproducing the desired, result.

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluatedSo, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

The order of the expressions matters. DefiningEquation2 before defining g yields

Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, while

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

as desired,

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluated, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

The order of the expressions matters. DefiningEquation2 before defining g[x] yields

Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, when g[x] is subsequently defined, Derivative[1][g][f[x]] is unable to interpret it and returns unevaluated.

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

This looks like an additional dependent variable to NDSolve, which produces an error message. On the other hand, if g[x] is defined first, then D[g[f[x]], f[x]] processes it immediately when Equation2 is subsequently defined

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

producing the desired result.

So, to avoid this problem, define g[x] before defining Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

2 simplified answer
source | link

The order of the expressions matters. DefiningEquation2 before defining g yields

Equation1 = a'[x]/a[x] - Sqrt[(1/3)*((1/2)*(f'[x])^2 + g[f[x]])]
Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* Derivative[1][a][x]/a[x] - Sqrt[Piecewise[{{f[x]^2, x0 <= x <= a1}, 
   {-(n*f[x]), a1 <= x <= a2}, {f[x]^2, x >= a2}}, 0] + Derivative[1][f][x]^2/2]/Sqrt[3] *)
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note, in particular, that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, while

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

as desired,

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluated, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation1 and Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

The order of the expressions matters.

Equation1 = a'[x]/a[x] - Sqrt[(1/3)*((1/2)*(f'[x])^2 + g[f[x]])]
Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* Derivative[1][a][x]/a[x] - Sqrt[Piecewise[{{f[x]^2, x0 <= x <= a1}, 
   {-(n*f[x]), a1 <= x <= a2}, {f[x]^2, x >= a2}}, 0] + Derivative[1][f][x]^2/2]/Sqrt[3] *)
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note, in particular, that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, while

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

as desired,

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluated, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation1 and Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

The order of the expressions matters. DefiningEquation2 before defining g yields

Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, while

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

as desired,

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluated, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.

1
source | link

The order of the expressions matters.

Equation1 = a'[x]/a[x] - Sqrt[(1/3)*((1/2)*(f'[x])^2 + g[f[x]])]
Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]]
(* Derivative[1][a][x]/a[x] - Sqrt[Piecewise[{{f[x]^2, x0 <= x <= a1}, 
   {-(n*f[x]), a1 <= x <= a2}, {f[x]^2, x >= a2}}, 0] + Derivative[1][f][x]^2/2]/Sqrt[3] *)
(* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] 
   + Derivative[2][f][x] *}

Note, in particular, that D[g[f[x]], f[x]] has been converted to Derivative[1][g][f[x]]. So, while

D[g[f[x]], f[x]]
(* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || 
   (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *)

as desired,

Derivative[1][g][f[x]]
(* Derivative[1][g][f[x]] *)

returns unevaluated, which looks like an additional function to NDSolve. To avoid this problem, define g[x] before defining Equation1 and Equation2. Also, remember to

Clear[Equation1, Equation2]

before starting.