I am trying to solve numerically two differential equations with NDSolve.

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]]

where a[x] and f[x] are both the functions I want to get, and where I want to define g[f[x]] as a Piecewise function of f[x]:

g[f[x]] = Piecewise[{{f[x]^2, x0 <= x <= a1}, {-n*f[x], a1 <= x <= a2}, {f[x]^2, x >= a2}}]

where n is a constant value.

The problem arises when I try to solve this equation numerically (let us assume e.g. x0 = 0, a1 = 5000, a2 = 10000, xf = 10^6):

nsol = NDSolve[{Equation1 == 0, Equation2 == 0, a[x0] == 1, f[x0] == K1, f'[x0] == -n*K1}, {a, f}, {x, x0, xf}, MaxSteps -> 10000000, PrecisionGoal -> 10, AccuracyGoal -> 90]  

with K1 another number and n the same before defined. (Note that I have tried to simplify my notation here used for more generality).

Unfortunately, there appears the following mistake when computing:

NDSolve::litarg: "To avoid possible ambiguity, the arguments of the dependent variable in ... should literally match the independent variables."

I have already searched for similar threads as mine unsuccesfully. My specific question is the following one: how could I solve those differential equations dependent on Piecewise functions numerically, without needing to solve them each time for each separate interval of x as defined in g[f[x]]?

  • $\begingroup$ I took the liberty of editing your post to correct the (apparent) mistake where you used EQ1 and EQ2 in the NDSolve and defined Equation1 and Equation2. Nonetheless, we still are going to need numerical values for K, et c. if we're going to be able to reproduce your problem (since as you probably know, changing coefficients in ODEs can drastically change how hard they are to numerically integrate.) $\endgroup$
    – Pillsy
    Commented Oct 28, 2015 at 23:34
  • $\begingroup$ Allright, I am sorry for that. EQ1 and EQ2 were my former notation. I just wanted to make my notation as clear as possible for everybody; this included writing Equation1 and Equation2 instead of EQ1 and EQ2. Thank you for your indications and for the correction, MichaelE2 and Pillsy. $\endgroup$
    – Lloyd
    Commented Oct 29, 2015 at 9:14
  • $\begingroup$ About the numerical values, just take them as positive-defined. $\endgroup$
    – Lloyd
    Commented Oct 29, 2015 at 9:20

1 Answer 1


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]] *)

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.

  • $\begingroup$ Thank you for your answer, @bbgodfrey. You are right, g[f[x]] has to be defined before Equation2 and its derivative is only evaluated when written as D[g[f[x]],f[x]]. However $\endgroup$
    – Lloyd
    Commented Oct 29, 2015 at 9:38
  • $\begingroup$ @Lloyd By the way, the calculation becomes very slow when the argument of the square root becomes negative. $\endgroup$
    – bbgodfrey
    Commented Oct 29, 2015 at 11:38
  • $\begingroup$ Thanks again, @bbgodfrey. Originally I had my code in the right way you have also insisted and this worked properly (I checked it again by the way). Actually, I also tried for n>0, so that the argument of the square root becomes negative and the expression is very slow evaluated, just as you have pointed out. However, the mistake I referred about in my first post reappears when numerically DSolving for g[f[x]] positive-defined for x>0, i.e., taking n<0. How could I handle this? $\endgroup$
    – Lloyd
    Commented Oct 29, 2015 at 12:57
  • $\begingroup$ @Lloyd Are you saying that NDSolve::litarg occurs for n < 0? I cannot reproduce this. Instead, the Sqrt with negative argument reoccurs. $\endgroup$
    – bbgodfrey
    Commented Oct 29, 2015 at 13:16
  • $\begingroup$ Not necessarily, @bbgodfrey, this is just another situation I would like to reproduce (apart from the case n>0, which gives a negative argument in the Sqrt). I mean, as I defined in the Piecewise function one interval in which g[f[x]]=-n*f[x], n<0 -> g[f[x]]>0in that interval if f[x]>0there. My last attempts in that situation were unsuccesful, but perhaps this is due to mathematical reasons as e.g. the Sqrtor discontinuities, I do not know. $\endgroup$
    – Lloyd
    Commented Oct 29, 2015 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.