I'm trying to Plot a result from NDSolve. The problem is that when I
pass this solution to Plot it takes an incredible long time (four
hours for times between 0 and 1200) to plot. I tried generating an
image file (.jpg, .eps, etc.) but that doesn't change anything. I
tried also giving to Plot the option PerformanceGoal -> "Speed", but
it gives me only half an hour less of waiting time. The other options
(like PlotPoints -> Number, MaxRecursion -> 0 ) give an improvement
but the image is ugly unless PlotPoints is high, which at the end is
the same.
I tried also asking for Plot only the last 200 units of time, trying
to avoid the most oscillating part of the graphic, but that really
doesn't help (and really it's not what I need).
Finally, the resulting InterpolatingFunction, when evaluated outside
Plot, evaluates fast, and a ListPlot using points returned by a Table
works well, but the resulting graphic is insanely big (35, 36 Mb) if I
put the points necessary to give account of the features of the
dynamic.
Unfortunately I can't give you an example because the equations I'm
solving are too long, but I wonder if there is someway to generate the
graphic, either on the interface or in a file, to not be so slow.
Thanks in advance, and sorry for this vague question (which, by the
way, without examples, is useless... by I had to give it a try).
did you try
sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
Plot[Evaluate[f[t]/.sol],{t,0,1200}]
?
Peter
That is, you compute an Interpolation function, you Set (not SetDelayed) a
function equal to it, and you use that function in the Plot.
Here's one way to do it right (for the first example in Help for NDSolve):
f = y /. First@
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}];
t1 = First@Timing[Plot[f@x, {x, 0, 30}, PlotRange -> All];]
0.012584
and here's a simple way to do it wrong:
f := y /.
First@NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1},
y, {x, 0, 30}];
t2 = First@Timing[Plot[f@z, {z, 0, 30}, PlotRange -> All];]
2.29799
t2/t1
182.612
That's a big difference in speed, you'll have to agree.
Bobby
On Sat, 09 Jul 2011 06:35:00 -0500, Iván Lazaro <gami...@gmail.com>
wrote:
Yes, I tried
sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
Plot[Evaluate[f[t]/.sol],{t,0,1200}],
but that was a pain. Thanks to Bobby I managed to solve my speed problem:
Instead of
sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
Plot[Evaluate[f[t]/.sol],{t,0,1200}],
I selected the specific solutions I needed, and Set them to a variable
that then I plot:
sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
a=sol[[1, Something, 2]]
b=sol[[1, Something+1, 2]]
Plot[{a[t],b[t]}],{t,0,1200}],
and that was it. However I don't understand this. Was the problem the
"size" and "amount" of interpolated functions?
So, for example,
{eqns, cond}={f1'[t]==a11*f1[t]+a12*f2[t]+...+a1N*fN[t],...,
fN'[t]==aN1*f1[t]+aN2*f2[t]+...+aNN*fN[t], f1[0]==t01,...,fN[0]==t0N},
and
f={f1,f2,...,fN}.
If Something is, say 1, then
sol[[1, 1]]:
f1[t]->InterpolatingFunction[{{0.`,1200.`}},"<>"][t]
and sol[[1, 1, 2]] is just InterpolatingFunction[{{0.`,1200.`}},"<>"][t].
So, with
sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
a[t_]=sol[[1, 1, 2]]
b[t_]=sol[[1, 2, 2]]
i'm just extracting the solution for two of my variables, f1 and f2.
Plot "a" and "b",
Plot[{a[t], b[t]},{t,0,1200}],
was fast; however this:
Plot[{Evaluate[f1[t]/.sol], Evaluate[f2[t]/.sol]},{t,0,1200}],
was, somehow, imposible.
2011/7/11 DrMajorBob <btr...@austin.rr.com>:
>> and that was it. However I don't understand this. Was the problem the
>> "size" and "amount" of interpolated functions?
>
> I don't understand it either. The two methods seem equivalent, but this code
>
>> sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
>> a=sol[[1, Something, 2]]
>> b=sol[[1, Something+1, 2]]
>
> suggests that you're solving for one function f in the first line, and YET,
> you're extracting two solutions a and b in the next two lines. That's not
> possible, so you're not showing us the code you actually used. (We know that
> anyway, since "eqns", "cond", and "Something" are undefined.)
>
> I suspect in the real code, the two methods that seem equivalent are NOT
> equivalent at all.
>
> Bobby
> --
> DrMaj...@yahoo.com
>
In this code, Evaluate has no effect whatsoever:
Plot[{Evaluate[f1[t]/.sol], Evaluate[f2[t]/.sol]},{t,0,1200}]
because the first argument of Plot is a List, and you're not applying
Evaluate to it. The List is held, not the list elements separately.
In this code, on the other hand:
Plot[Evaluate[f[t]/.sol],{t,0,1200}]
you ARE applying Evaluate to the first argument.
I don't think that explains why the plot is slow, but we still haven't
seen the actual code.
Send me the notebook, if you like.
Bobby
On Mon, 11 Jul 2011 15:25:44 -0500, Iván Lazaro <gami...@gmail.com>
wrote:
> Well, i'm going to try to clarify it a little, but, as I said, is not
>> On Mon, 11 Jul 2011 05:58:03 -0500, Iván Lazaro <gami...@gmail.com>
>> wrote:
>>
>>> Hi!
>>>
>>> Yes, I tried
>>>
>>> sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
>>> Plot[Evaluate[f[t]/.sol],{t,0,1200}],
>>>
>>> but that was a pain. Thanks to Bobby I managed to solve my speed
>>> problem:
>>>
>>> Instead of
>>>
>>> sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
>>> Plot[Evaluate[f[t]/.sol],{t,0,1200}],
>>>
>>> I selected the specific solutions I needed, and Set them to a variable
>>> that then I plot:
>>>
>>>
>>>
>>> sol=NDSolve[{eqns, cond},f,{t,0,1200}][[1]];
>>> a=sol[[1, Something, 2]]
>>> b=sol[[1, Something+1, 2]]
>>>
>>> Plot[{a[t],b[t]}],{t,0,1200}],
>>>
>>> and that was it. However I don't understand this. Was the problem the
>>> "size" and "amount" of interpolated functions?
>>>
>>
>>
>> --
>> DrMaj...@yahoo.com
>>
pl=Plot[Evaluate[f[t]/.sol],{t,0,1200}];
Does that calculation go reasonably fast - if so, it is the process of
transferring the result to the front end and actually plotting it which
is at fault.
It may now help to reduce the upper limit from 1200 to (say) 10, just
to get a simple expression. Now look at:
pl//InputForm
If this expression contains lots of Line structures, rather than a few
Line Structures containing many coordinate values, then this is the
explanation. Large structures of this sort transfer to the front end
very inefficiently.
If this doesn't work, why not post a complete example of a slow plot.
David Bailey
http://www.dbaileyconsultancy.co.uk
Plot[{f1[t, f2[t]}]/.sol,{t,0,60}],
and 0.5 seconds making the plot
Plot[{a[t], b[t]},{t,0,60}],
with the "notation" defined in my last email, while in my machine it
takes 140 seconds to make the first and 0.5 to make the second. I'm
going to try a reinstallation.
Thanks to everyone.