Message Boards Message Boards

GROUPS:

Use NDSolve with conditional coefficients?

Posted 4 years ago
4739 Views
|
7 Replies
|
0 Total Likes
|

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}]
7 Replies
Posted 4 years ago

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.

Posted 4 years ago

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.01Max[0, Derivative[1][y][t]] - 0.01y[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.

Posted 4 years ago

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.`. >>
Posted 4 years ago

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?

Posted 4 years ago

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.

Posted 4 years ago

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.

Posted 4 years ago

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

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract