Re: Compiled functions in NDSolve.
- To: mathgroup at smc.vnet.net
- Subject: [mg25680] Re: Compiled functions in NDSolve.
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 19 Oct 2000 04:35:11 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <8sjhed$fo6@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
a) it is useless to compile parts of the equations because
NDSolve[] compile the explicit rhs sides again. The function
call in a compiled function may slow down the computation speed.
b) you need a wrapper function that call the compiled funtion only for
numeric arguments
(* the compiled stuff *)
potx = Compile[{{x, _Real}, {y, _Real}}, x + y]
poty = Compile[{{x, _Real}, {y, _Real}}, x*y]
(* the wrapper patterns that prevent the evaluation with
symbolic arguments *)
PotX[x_?NumericQ, y_?NumericQ] := potx[x, y]
PotY[x_?NumericQ, y_?NumericQ] := poty[x, y]
NDSolve[{x'[t] == PotX[x[t], y[t]],
y'[t] == PotY[x[t], y[t]], x[0] == 1, y[0] == 1}, {x[t],
y[t]}, {t, 0, 1}]
Hope that helps
Jens
Daniel Lisak wrote:
>
> Does anybody know how can I use compiled functions in NDSolve ?
> I have some compiled functions (they all depend on 3 real
> variables: t, ro, v):
>
> potencjalX = Compile[{t, ro, v}, -176.276/(ro^2 + v^2 t^2)^3];
> potencjalA = Compile[{t, ro, v}, -248.279/(ro^2 + v^2 t^2)^3];
> potencjalB = Compile[{t, ro, v}, -319.691/(ro^2 + v^2 t^2)^3];
> funfi = Compile[{t, ro, v}, ArcSin[ro/Sqrt[ro^2 + v^2t^2]]];
> funF = Compile[{t, ro, v}, (potencjalB[t, ro, v] - potencjalA[t, ro, v])/2];
> funv1 = Compile[{t, ro, v}, (potencjalA[t, ro, v] + potencjalB[t, ro,
> v])/2];
>
> and I want to solve ordinary differential equations:
>
> rozw[apocz_, ro_, v_, tend_, nieskoncz_] := NDSolve[{
> a1'[t] == a1[t] potencjalX[t, ro, v]/I,
> a2'[t] == a2[t] potencjalB[t, ro, v]/I,
> a3'[t] == a3[t] funv1[t, ro, v]/I +
> a4[t] funF[t, ro, v] Exp[2I funfi[t, ro, v]]/I,
> a4'[t] == a3[t] funF[t, ro, v] Exp[-2I funfi[t, ro, v]]/I +
> a4[t] funv1[t, ro, v]/I,
> a1[-nieskoncz] == apocz[[1]],
> a2[-nieskoncz] == apocz[[2]],
> a3[-nieskoncz] == apocz[[3]],
> a4[-nieskoncz] == apocz[[4]]},
> {a1, a2, a3, a4}, {t, -tend, tend}, MaxSteps -> 100000]
>
> when I evaluate this function:
>
> rozw[{0, 0, 1, 0}, 3, 0.0003, 75000., 75000.]
>
> I see the following error:
>
> CompiledFunction::cfsa: Argument t at position 1 should be a machine-size
> real number.
>
> Does anybody know how can I use these compiled functions in NDSolve ?
>
> Daniel
>
>