Re: NDSolve useless?
- To: mathgroup at smc.vnet.net
- Subject: [mg63909] Re: NDSolve useless?
- From: rknapp at wolfram.com
- Date: Sat, 21 Jan 2006 01:51:12 -0500 (EST)
- References: <dqd9oi$mc5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
The problem here is that you are comparing apples and oranges.
ode45 requires that you set up a function that returns the right hand
side of the ODEs in first order normal form, which I assume is what you used to
make comparisons.
You have the option to do this in Mathematica (though it is not required),
and as I show below, there is very little speed difference between NDSolve
and ode45 with this specification.
The reason Mathematica is so much slower for the problem specification you
have set up is that you have created a large symbolic expression that
Mathematica is required to process and optimize. We have made
improvements so that this processing will be faster in the next release
of Mathematica, but even so, it is still much faster, particularly in
a problem of this type, to just specify the right hand sides of the equations:
Define:
NV = 128;
tf = 1.0;
delta = 0.1;
a = 0.01;
x0=Table[n/NV+a Sin[2p n/NV],{n,1,NV}];
y0=Table[-a Sin[2p n/NV],{n,1,NV}];
(* This gives the right hand side of the equations *)
cf = Compile[{{x, _Real,
1}, {y, _Real, 1}}, Module[{difx, dify, deno,NV = Length[x]},
Transpose[Table[
difx = 2.*Pi*(x[[n]] - x);
dify = 2.*Pi*(y[[n]] -y);
deno = 2.*NV*(Cosh[dify] - Cos[difx] +delta^2);
{Total[-Sinh[dify]/deno], Total[Sin[difx]/deno]},
{n, 1,NV}]]
]];
(* Using this prevents evaluation except during the solution process *)
g[{x_?VectorQ, y_?VectorQ}] := cf[x, y];
then, I get
In[10]:=
Timing[sol=First[NDSolve[{xy'[t] == g[xy[t]],xy[0] == {x0,
y0}},xy,{t,0,tf}]]]
Out[10]=
{2.984 Second,{xy->InterpolatingFunction[{{0.,1.}},<>]}}
the reason that this is slower than your time for ode45 is that the
default tolerances
for Mathematica are set more stringently. Essentially the some
tolerance settings
as for ode45 can be achieved with
In[11]:=
Timing[sol=First[NDSolve[{xy'[t] == g[xy[t]],xy[0] == {x0, y0}},xy,
{t,0,tf}, PrecisionGoal->3, AccuracyGoal->6]]]
Out[11]=
{1.39 Second,{xy->InterpolatingFunction[{{0.,1.}},<>]}}
which is quite comparable in time.
- Follow-Ups:
- Re: Re: NDSolve useless?
- From: Pratik Desai <pdesai1@umbc.edu>
- Re: Re: NDSolve useless?