Message Boards Message Boards

Use NDSolve with conditional coefficients?

Posted 8 years ago

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}]
POSTED BY: Iuval Clejan
7 Replies
Posted 8 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 BY: Iuval Clejan
Posted 8 years ago

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

POSTED BY: Bill Simpson
Posted 8 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 BY: Iuval Clejan
Posted 8 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 BY: Bill Simpson
Posted 8 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 BY: Bill Simpson
Posted 8 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 BY: Bill Simpson
Posted 8 years ago
POSTED BY: Iuval Clejan
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