Re: NDSolve with Numeric Function
- To: mathgroup at smc.vnet.net
- Subject: [mg100215] Re: NDSolve with Numeric Function
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 28 May 2009 06:49:06 -0400 (EDT)
- Organization: Uni Leipzig
- References: <gvka0b$oo8$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi,
RHS[x_List, v_List, 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}]
work fine. And the problem is, that NDSolve[] evaluate its arguments
before it found out that you wish to solve a vector equation.
Regards
Jens
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}]
>