Re: Getting values from a Plot

• To: mathgroup at smc.vnet.net
• Subject: [mg7419] Re: [mg7370] Getting values from a Plot
• From: Daniel Lichtblau <danl>
• Date: Sat, 31 May 1997 15:07:31 -0400 (EDT)
• Organization: Wolfram Research, Inc.
• Sender: owner-wri-mathgroup at wolfram.com

```Lou Talman wrote:
>
> Pere LLOSAS
>
> > Does anyone know how to get a list with the values calculated to display
> > a graph with the Plot function?  {{x0,y0},{x1,y1},...{xn,yn}}
> >
>
> Try this:
>
> pointlist = {}; Plot[AppendTo[pointlist, x];Sin[x], {x, 0, Pi}]
>
> After execution, the list "pointlist" will contain the abscissae of the
> points that Mathematica chose to construct its plot of the sine function.
>
> --Lou Talman

One caveat, noted in the fine print under the Ref guide entry for
Append/AppendTo, is that this is inefficient for large problems. If you
have alot of points (several thousand, say), a much faster way would be

pointlist = {};
Plot[pointlist = {pointlist,x}; Sin[x], {x, 0, Pi}]
pointlist = Flatten[pointlist];

it a good opportunity to again draw attention to this method).

Thought the ref guide does not mention it, this is also an issue with
Prepend/PrependTo. The reason in all cases, whether prepending or
appending, is that one is constantly rebuilding new (fixed-size) lists
of length one greater than before. This is an O(n) operation, repeated
O(n) times, giving an O(n^2) algorithm.

The method that uses Flatten instead keeps the old lest, but nests it
one level into the new one that has the new value at the end. This is
O(1), and flattening once at the end is O(n), for an O(n) algorithm.

Daniel Lichtblau
Wolfram Research
danl at wolfram.com

```

• Prev by Date: Re: Won't Read Entire Soundfile; Why?
• Next by Date: Re: List manipulation