# Use NDSolve with conditional coefficients?

**URL:** <https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654>\
**Category:** Questions\
**Tags:** control-systems, equation-solving, numerical-computation\
**Created:** [May 28, 2016, 9:18pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654 "2016-05-28T21:18:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![clejaniuval](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@clejaniuval](https://community.wolfram.com/u/clejaniuval)\
**Post date:** [May 28, 2016, 9:18pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/1 "2016-05-28T21:18:41Z")

</div>

I have two coupled first order non-linear ODEs, which have coefficients that depend on the functions and their first derivatives. I tried a few conditionals such as If and Piecewise, but NDSolve claims that: “NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. \>\>”.

This must come up all the time in control systems–do I need to use that formalism (I only have version 8 home edition), or can I get “under the hood” of NDSolve to do this? It works if A is defined without conditions, but I need to find the best A (according to different utility functions) subject to certain constraints.

```
A[i_, h_] := Piecewise[{{1 - i*0.1, h >= 0.0}, {0, h < 0}}]
sol[i_] := 
 NDSolve[{n'[t] == 
    kg + n[t] (A[i, hc - hi[y[t], y'[t]]] (qc - qi[hi[y[t], y'[t]]]) +
         qi[hi[y[t], y'[t]]] - 1), 
   y'[t] == (p[i, y[t], y'[t]] - A[i, hc - hi[y[t], y'[t]]]) b, 
   n[0] == ninit, y[0] == 1}, {n[t], y[t], n'[t], y'[t]}, {t, 0, 100}]

```

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [May 29, 2016, 5:55am UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/2 "2016-05-29T05:55:00Z")

</div>

Numerical solving requires that all variables, other than those being solved for, have been previously assigned constant numeric values.

Making up substitutes for all the functions and values for variables that I assume you have

```
ninit= .2; kg= .3; hc= .7; b= .6; qc= .5; hi[x_, y_]:=x+y; qi[x_]:=x^2; p[i_, x_, y_]:=i+x+y; 
A[i, h] := Piecewise[{{1 - i*0.1, h >= 0.0}, {0, h < 0}}]; 
sol[i_] := NDSolve[{n'[t] == kg + n[t] (A[i, hc - hi[y[t], y'[t]]] (qc - qi[hi[y[t], y'[t]]]) +
    qi[hi[y[t], y'[t]]] - 1), y'[t] == (p[i, y[t], y'[t]] - A[i, hc - hi[y[t], y'[t]]]) b, 
   n[0] == ninit, y[0] == 1}, {n[t], y[t], n'[t], y'[t]}, {t, 0, 100}];
sol[4]

```

gives

```
{{n[t] -> InterpolatingFunction[{{0., 100.}}, <>][t], 
  y[t] -> InterpolatingFunction[{{0., 100.}}, <>][t], 
  Derivative[1][n][t] -> InterpolatingFunction[{{0., 100.}}, <>][t], 
  Derivative[1][y][t] -> InterpolatingFunction[{{0., 100.}}, <>][t]}}

```

with no error messages. And the resulting four functions plot.

Are you sure you have assigned constants to all your other variables and defined all your other functions?

If so then perhaps there is a zero lurking in one of your denominators somewhere.

Or show all the missing information and perhaps someone can track down the source of the problem.

---

<div class="post-metadata">

**Author:** ![clejaniuval](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@clejaniuval](https://community.wolfram.com/u/clejaniuval)\
**Post date:** [May 29, 2016, 7:01pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/3 "2016-05-29T19:01:42Z")

</div>

The problem is parameter dependent. Here is the rest of the code so you can repeat it:  
ninit = 10; qc = 0.2; hc = 0.1; kg = 7; k1 = 0.01; k2 = -0.01; b = 1;a=0.79  
qi[hi\_] := (Tanh[hi - a] + 1)/2  
hi[y\_, yp\_] := k1 y + k2 Max[0, yp]  
A[i\_, h\_] := Piecewise[{{1 - i\*0.1, h \>= 0.0}, {0, h \< 0}}]  
p[i\_, y\_, yp\_] := 1  
sol[i\_] :=  
NDSolve[{n’[t] ==  
kg + n[t] (A[i, hc - hi[y[t], y’[t]]] (qc - qi[hi[y[t], y’[t]]]) +  
qi[hi[y[t], y’[t]]] - 1),  
y’[t] == (p[i, y[t], y’[t]] - A[i, hc - hi[y[t], y’[t]]]) b,  
n[0] == ninit, y[0] == 1}, {n[t], y[t], n’[t], y’[t]}, {t, 0, 100}]  
sol0 = sol[0]  
NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. \>\>

Here is the input form of the output:  
NDSolve[{Derivative[1][n][t] ==  
7 + n[t]_(-1 +  
Piecewise[{{1., 0.1 + 0.01_Max[0, Derivative[1][y][t]] -  
0.01_y[t] \>= 0.}}, 0]_  
(0.2 + (-1 + Tanh[0.79 + 0.01_Max[0, Derivative[1][y][t]] -  
0.01_y[t]])/2) +  
(1 - Tanh[0.79 + 0.01_Max[0, Derivative[1][y][t]] -  
0.01_y[t]])/2), Derivative[1][y][t] ==  
1 - Piecewise[{{1., 0.1 + 0.01_Max[0, Derivative[1][y][t]] -  
0.01_y[t] \>= 0.}}, 0], n[0] == 10, y[0] == 1},  
{n[t], y[t], Derivative[1][n][t], Derivative[1][y][t]}, {t, 0, 100}]  
It works for |k2|\< about 0.001.

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [May 29, 2016, 8:24pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/4 "2016-05-29T20:24:54Z")

</div>

Fix a few things

```
ninit = 10; qc = 0.2; hc = 0.1; kg = 7; k1 = 0.01; k2 = -0.01; b = 1; a = 0.79;
qi[hi_] := (Tanh[hi - a] + 1)/2;
hi[y_, yp_] := k1 y + k2 Max[0, yp]; 
A[i_, h_] := Piecewise[{{1 - i*0.1, h >= 0.0}, {0, h < 0}}]; 
p[i_, y_, yp_] := 1; 
sol[i_] := NDSolve[{n'[t]==kg+n[t] (A[i, hc- hi[y[t], y'[t]]] (qc-qi[hi[y[t], y'[t]]])+qi[hi[y[t],y'[t]]]-1), 
   y'[t] == (p[i, y[t], y'[t]] - A[i, hc - hi[y[t], y'[t]]]) b, 
   n[0] == ninit, y[0] == 1}, {n[t], y[t], n'[t], y'[t]}, {t, 0, 100}];
sol0 = sol[0]

```

which results in

```
During evaluation of In[1]:= NDSolve::tddisc: NDSolve cannot do a discontinuity replacement for event surfaces that depend only on time. >>

During evaluation of In[1]:= NDSolve::tddisc: NDSolve cannot do a discontinuity replacement for event surfaces that depend only on time. >>

During evaluation of In[1]:= NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. >>

```

---

<div class="post-metadata">

**Author:** ![clejaniuval](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@clejaniuval](https://community.wolfram.com/u/clejaniuval)\
**Post date:** [May 29, 2016, 9:47pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/5 "2016-05-29T21:47:28Z")

</div>

I need to learn how to post in nice format like you did–I see that option now. But the input looks the same as mine, except for the formatting here? So you get the same error I do, with some preliminary errors from tddisc, which is probably because you have a newer version. What does it mean? Why is there a problem for a certain range of k2?

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [May 30, 2016, 12:18am UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/6 "2016-05-30T00:18:56Z")

</div>

Look closely at the differences between yours

```
hi[y, yp] := k1 y + k2 Max[0, yp]; 
A[i, h] := Piecewise[{{1 - i*0.1, h >= 0.0}, {0, h < 0}}]; 
p[i, y, yp_] := 1;

```

and mine

```
hi[y_, yp_] := k1 y + k2 Max[0, yp];
A[i_, h_] := Piecewise[{{1 - i*0.1, h >= 0.0}, {0, h < 0}}];
p[i_, y_, yp_] := 1;

```

which can often make a very large difference to Mathematica.

As for the error message from Mathematica, I expect this is the result of introducing things with abrupt changes like Max and Piecewise into NDSolve. Someone with more experience with NDSolve than I have will need to address that issue.

---

<div class="post-metadata">

**Author:** ![clejaniuval](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@clejaniuval](https://community.wolfram.com/u/clejaniuval)\
**Post date:** [May 30, 2016, 4:15pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/7 "2016-05-30T16:15:13Z")

</div>

That is not the difference that actually exists. My code does have the underlines, it’s an artefact of copying from Mathematica and pasting into the browser somehow.

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [May 30, 2016, 4:22pm UTC](https://community.wolfram.com/t/use-ndsolve-with-conditional-coefficients/8654/8 "2016-05-30T16:22:06Z")

</div>

Now I understand. Again we have been bitten by the desktop publishing done to posts by the forum software.
