Re: Approximating the function from its plot
- To: mathgroup at smc.vnet.net
- Subject: [mg56131] Re: Approximating the function from its plot
- From: Peter Pein <petsie at arcor.de>
- Date: Sat, 16 Apr 2005 03:52:20 -0400 (EDT)
- References: <d3o0dm$bn5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Siddharth Jain wrote: > Hello > > I want find an approximate function for a ListPlot. Is it possible to > do this using mathematica ? > > Thanks > Siddharth > The method to use depends on whether the plot has been generated with PlotJoined->True or False: In[1]:= (* a simple example *) tb = Table[{x, Cos[x]}, {x, 0, Pi, Pi/5}]; pl = ListPlot[tb]; (*extract the coordinates of the points*) data = Cases[pl[[1]], Point[{x_, y_}] -> {x, y}]; In[3]:= (* verify result *) data == tb Out[3]= True If PlotJoined->True has been used, we need the coordinates that are used by Line[]: In[4]:= pl = ListPlot[tb, PlotJoined -> True, PlotStyle -> Red]; data = Cases[pl[[1]], Line[lst_] -> lst, {0, Infinity}][[1]]; In[6]:= (* verification again: *) data == tb Out[6]= True In[7]:= (* now build an interpolating function:*) f = Interpolation[data]; In[8]:= (* and show the result *) Show[Block[{$DisplayFunction = #1 & }, Plot[f[x], {x, 0, Pi}]], pl]; -- Peter Pein Berlin