Re: plot
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1807] Re: plot
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Mon, 31 Jul 1995 23:08:36 -0400
- Organization: University of Colorado, Boulder
In article <DCBwvx.544 at wri.com>, yao xiaoqiang <x-yao at ux1.cso.uiuc.edu> wrote: >plot of dy/dx vs. x > >I have a set of data(x,y). I want to get a plot of dy/dx vs x from >the plot of y vs. x. > >Any suggestions? You want to use finite differencing, I suppose. This is easy to do because the arithmetic operators all work on lists. Here's some sample data. In[16]:= data = Table[{x, Sin[x]}, {x,0,3.2,.2}] Out[16]= {{0, 0}, {0.2, 0.198669}, {0.4, 0.389418}, {0.6, 0.564642}, {0.8, 0.717356}, {1., 0.841471}, {1.2, 0.932039}, {1.4, 0.98545}, {1.6, 0.999574}, {1.8, 0.973848}, {2., 0.909297}, {2.2, 0.808496}, {2.4, 0.675463}, {2.6, 0.515501}, {2.8, 0.334988}, {3., 0.14112}, {3.2, -0.0583741}} This computes the difference between successive points, in the form of {dx, dy}. The rule converts those pairs into slopes. In[26]:= slopes = Drop[data,1] - Drop[data,-1] /. {dx_,dy_}->dy/dx Out[26]= {0.993347, 0.953745, 0.876121, 0.763568, 0.620574, 0.452841, 0.267053, 0.0706194, -0.12863, -0.322751, -0.504005, -0.665166, -0.799809, -0.902566, -0.969341, -0.997471} Similarly, this computes the midpoint between successive points, and the rule extracts just the x-components. In[27]:= midpoints = (Drop[data,1] + Drop[data,-1])/2 /. {x_,_}->x Out[27]= {0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9, 3.1} Put 'em all together: In[29]:= derivdata = Transpose[{midpoints, slopes}] Out[29]= {{0.1, 0.993347}, {0.3, 0.953745}, {0.5, 0.876121}, {0.7, 0.763568}, {0.9, 0.620574}, {1.1, 0.452841}, {1.3, 0.267053}, {1.5, 0.0706194}, {1.7, -0.12863}, {1.9, -0.322751}, {2.1, -0.504005}, {2.3, -0.665166}, {2.5, -0.799809}, {2.7, -0.902566}, {2.9, -0.969341}, {3.1, -0.997471}} This plot should look like the cosine function. In[30]:= ListPlot[derivdata, PlotJoined->True] Out[30]= -Graphics- I hope this helps! Dave Wagner Principia Consulting (303) 786-8371 dbwagner at princon.com http://www.princon.com/princon