Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compiled functions in NDSolve.

143 views
Skip to first unread message

Daniel Lisak

unread,
Oct 18, 2000, 2:54:05 AM10/18/00
to
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



Jens-Peer Kuska

unread,
Oct 20, 2000, 11:06:03 PM10/20/00
to
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

0 new messages