# About NDSolve with piecewise inputs

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

• 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.) – Pillsy Oct 28 '15 at 23:34
• 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. – Lloyd Oct 29 '15 at 9:14
• About the numerical values, just take them as positive-defined. – Lloyd Oct 29 '15 at 9:20

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.

• 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 – Lloyd Oct 29 '15 at 9:38
• @Lloyd By the way, the calculation becomes very slow when the argument of the square root becomes negative. – bbgodfrey Oct 29 '15 at 11:38
• 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? – Lloyd Oct 29 '15 at 12:57
• @Lloyd Are you saying that NDSolve::litarg occurs for n < 0? I cannot reproduce this. Instead, the Sqrt with negative argument reoccurs. – bbgodfrey Oct 29 '15 at 13:16
• 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. – Lloyd Oct 29 '15 at 14:04
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7111917734146118, "perplexity": 1843.5317253832281}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 5, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-51/segments/1575540490743.16/warc/CC-MAIN-20191206173152-20191206201152-00047.warc.gz"}