Re: NDSolve with Numeric Function

*To*: mathgroup at smc.vnet.net*Subject*: [mg100233] Re: NDSolve with Numeric Function*From*: dh <dh at metrohm.com>*Date*: Thu, 28 May 2009 19:34:31 -0400 (EDT)*References*: <gvka0b$oo8$1@smc.vnet.net>

Hi Hugh, I have rewritten your code a bit. You can write the function RHS with vector notation, then your problems disappear. ======================== ClearAll[RHS]; RHS[x_, y_, f_] := Module[{}, -{{1, 2, 3, 5}, {3, 2, 1, 2}, {5, 7, 1, 1}, {3, 2, 4, 5}}.x + 3 y ] sol = NDSolve[{x''[t] == RHS[x[t], x'[t], 1], x[0] == {-1, 0, 1, 0}, x'[0] == {0, 0, 0, 0}}, x, {t, 0, 2} ] =========================== Neverthelesse, there is something fishy. The second case should actually work. However: MatchQ[#1, x_ /; NumericQ[ x[[1]] ]] returns True. Maybe you want to report this to Wolfram. Daniel Hugh Goyder wrote: > Please could someone explain why my NDSolve examples below don't work? > In the first case I think it fails because NDSolve attempts to > evaluate the function RHS symbolically. > The second case should remain unevaluated and signal to NDSolve to go > numerical but this fails why? > In the third case a similar check to the second works. Why? > > Many thanks > Hugh Goyder > > ClearAll[RHS]; > RHS[x_, v_, f_] := Module[{x1, y1, x2, y2, x1v, y1v, x2v, y2v}, > {x1, y1, x2, y2} = x; > {x1v, y1v, x2v, y2v} = v; > -{{1, 2, 3, 5}, {3, 2, 1, 2}, {5, 7, 1, 1}, {3, 2, 4, 5}}.{x1, y1, > x2, y2} + 3 {x1v, y1v, x2v, y2v} > ] > > sol = NDSolve[{x''[t] == RHS[x[t], x'[t], 1], x[0] == {-1, 0, 1, 0}, > x'[0] == {0, 0, 0, 0}}, x, {t, 0, 2}] > > ClearAll[RHS]; > RHS[x_ /; NumericQ[x[[1]]], v_, f_] := > Module[{x1, y1, x2, y2, x1v, y1v, x2v, y2v}, > {x1, y1, x2, y2} = x; > {x1v, y1v, x2v, y2v} = v; > -{{1, 2, 3, 5}, {3, 2, 1, 2}, {5, 7, 1, 1}, {3, 2, 4, 5}}.{x1, y1, > x2, y2} + 3 {x1v, y1v, x2v, y2v} > ] > > sol = NDSolve[{x''[t] == RHS[x[t], x'[t], 1], x[0] == {-1, 0, 1, 0}, > x'[0] == {0, 0, 0, 0}}, x, {t, 0, 2}] > > ClearAll[RHS]; > RHS[x_ /; Head[x] == List, v_, f_] := > Module[{x1, y1, x2, y2, x1v, y1v, x2v, y2v}, > {x1, y1, x2, y2} = x; > {x1v, y1v, x2v, y2v} = v; > -{{1, 2, 3, 5}, {3, 2, 1, 2}, {5, 7, 1, 1}, {3, 2, 4, 5}}.{x1, y1, > x2, y2} + 3 {x1v, y1v, x2v, y2v} > ] > > sol = NDSolve[{x''[t] == RHS[x[t], x'[t], 1], x[0] == {-1, 0, 1, 0}, > x'[0] == {0, 0, 0, 0}}, x, {t, 0, 2}] >