Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how can one use mathematica get the approximate derivative of {x,y} data points?

1,100 views
Skip to first unread message

Michael B. Heaney

unread,
Jan 14, 2012, 2:55:22 AM1/14/12
to
Hi,

I have a set of {x,y} data points:

{{0.03512, -0.5}, {0.0351181, -0.499}, ... {-0.113972, 0.699}, {-0.115072,
0.7}}

These data points look like a function y=f(x) when plotted on the x-y axes.
However, I do not know what the function f(x) is. But I need to get the
approximate derivative df/dx, as another set of data points. How can one
use Mathematica to do this?

Thanks,

Michael

--

Peter Breitfeld

unread,
Jan 14, 2012, 5:09:33 PM1/14/12
to
You can try Interpolation.

fun[x_]:=x^2Cos[x]

These will give approximate data for this function:

data=Table[{x,fun[x]+RandomReal[{0,0.25}]},{x,0,1,0.5}]

iF=Interpolation[data]

Derivative:

iF'[3]

Plot[{iF[x],iF'[x]},{x,0,5}]


--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

Bob Hanlon

unread,
Jan 14, 2012, 5:13:38 PM1/14/12
to
data = {#, Cos[#]} & /@ RandomReal[{0, 2 Pi}, 20];

f = Interpolation[data];

Plot[{f[x], f'[x]}, {x, Min[data[[All, 1]]], Max[data[[All, 1]]]}]


Bob Hanlon

Nasser M. Abbasi

unread,
Jan 14, 2012, 5:06:29 PM1/14/12
to
On 1/14/2012 1:55 AM, Michael B. Heaney wrote:
> Hi,
>
> I have a set of {x,y} data points:
>
> {{0.03512, -0.5}, {0.0351181, -0.499}, ... {-0.113972, 0.699}, {-0.115072,
> 0.7}}
>
> These data points look like a function y=f(x) when plotted on the x-y axes.
> However, I do not know what the function f(x) is. But I need to get the
> approximate derivative df/dx, as another set of data points. How can one
> use Mathematica to do this?
>
> Thanks,
>
> Michael
>
> --

one way:

?Fit

data = {{0.03512, -0.5}, {0.0351181, -0.499}, {-0.113972,0.699}, {-0.115072, 0.7}}
f = Fit[data, {1, x, x^2}, x];
D[f, x]

--Nasser



Michael Stern

unread,
Jan 15, 2012, 4:50:40 AM1/15/12
to
Mathematica will cheerfully take derivatives of InterpolationFunctions.

In[1]:= data = {{0.03512, -0.5}, {0.0351181, -0.499}, {-.05, 1}, {0, 9}, {-0.113972,
0.699}, {-0.115072, 0.7}};
In[2]:= ifun = Interpolation[data]

You may want to play with the InterpolationOrder in step 2, but something along these lines should work.
You can sanity check the results with a graph of this sort:

In[3]:= Plot[{ifun'[x], ifun[x]}, {x, -.115, .15}, PlotRange -> All]

And if you want the answer as a table of points rather than a continuous function, you can produce such a table with

In[4]:= Table[{x, ifun'[x]}, {x, -.115, .15, .005}]


or the equivalent.

Cheers,

Michael Stern

cedric.fuhrer

unread,
Jan 15, 2012, 4:51:42 AM1/15/12
to
hello!
you can do this too:

pointsList = {{0.03512, -0.5},{0.0351181, -0.499},...{-0.113972, 0.699},{-0.115072, 0.7}}
f = Interpolation[pointsList]

then
Plot[f'[x], {x,xmin,xmax}]
... or Integrate[f, x] or....all what you want to do with an other function.

> Message du 14/01/12 09:26
> De : "Michael B. Heaney"
> A : math...@smc.vnet.net
> Copie =C3 :
> Objet : how can one use mathematica get the approximate derivative of {x,y}
data points?
>
> Hi,
>
> I have a set of {x,y} data points:
>
> {{0.03512, -0.5}, {0.0351181, -0.499}, ... {-0.113972, 0.699}, {-0.115072,
> 0.7}}
>
> These data points look like a function y=f(x) when plotted on the x-y axes.
> However, I do not know what the function f(x) is. But I need to get the
> approximate derivative df/dx, as another set of data points. How can one
> use Mathematica to do this?
>
> Thanks,
>
> Michael
>
> --
>

Une messagerie gratuite, garantie =C3 vie et des services en plus, =C3=
=A7a vous tente ?
Je cr=C3=A9e ma bo=C3=AEte mail www.laposte.net

Murray Eisenberg

unread,
Jan 16, 2012, 6:57:45 AM1/16/12
to
Using Interpolation to find an interpolation function for the discrete
{x,y} data points is begging the question: what method of numerical
approximation should one use.

Unlike with numerical integration, good numerical differentiation is a
very delicate matter. There are all kinds of schemes for doing it, from
the very naive to the more sophisticated. The difficulty is that the
methods are generally unstable: reducing the step-size to control
truncation error typically allows the roundoff error to grow.

At least, one must worry about what Interpolation is actually doing. The
difficulty is perhaps highlighted by several features of this function:

- you may specify in the argument not just the {x,y} values but also
the intended values of the derivative at the x-values;

- the option InterpolationOrder allows you to specify what order
polynomials are used to interpolate (the default order being 3); and

- interpolation has a Method option.

In practice, Richardson extrapolation is often used for numerical
differentiation. There's a nice introduction to this in Skeel and
Kuiper's book "Elementary Numerical Computing with Mathematica."

All that said, for the purposes of the O.P., naively using Interpolation
may suffice.


On 1/14/12 5:10 PM, Bob Hanlon wrote:
> data = {#, Cos[#]}& /@ RandomReal[{0, 2 Pi}, 20];
>
> f = Interpolation[data];
>
> Plot[{f[x], f'[x]}, {x, Min[data[[All, 1]]], Max[data[[All, 1]]]}]
>
>
> Bob Hanlon
>
>
> On Sat, Jan 14, 2012 at 2:53 AM, Michael B. Heaney<mhe...@alum.mit.edu> wrote:
>> Hi,
>>
>> I have a set of {x,y} data points:
>>
>> {{0.03512, -0.5}, {0.0351181, -0.499}, ... {-0.113972, 0.699}, {-0.115072,
>> 0.7}}
>>
>> These data points look like a function y=f(x) when plotted on the x-y axes.
>> However, I do not know what the function f(x) is. But I need to get the
>> approximate derivative df/dx, as another set of data points. How can one
>> use Mathematica to do this?
>>
>> Thanks,
>>
>> Michael
>>
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

dr DanW

unread,
Jan 16, 2012, 7:03:52 AM1/16/12
to
The derivative curve you will get with Interpolation[]' can look very weird. I find I get better results using Interpolation[..., Method->"Spline"]'. The Spline function is continuous in first and second derivatives, so the resulting derivative is smoother.

Daniel

Barrie Stokes

unread,
Jan 16, 2012, 7:06:55 AM1/16/12
to
HI Michael

Here is one way, that I think is transparent enough for you to see what is happening:

(* create some data *)
data = Table[ {x, Sin[ Exp[ x ] ]}, {x, 0, 1.7, 0.05} ]

(* visualize the underlying function *)
p1 = ListPlot[ data ];
p2 = ListPlot[ data, Joined -> True, PlotStyle -> {PointSize[ 0.5 ]} ];
Show[ {p1, p2} ]

(* create an InterpolatingFunction object for this data *)
interp = Interpolation[ data, InterpolationOrder -> 5, Method -> "Spline" ]

(* check that it looks right, i.e., represents the data *)
p3 = Plot[ interp[x], {x, 0, 1.7} ];
Show[ {p1, p3} ]

(* create a derivative of this InterpolatingFunction object for this \
data *)interpD[x_] := \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\ \(interp[t]\)\) /. {t -> x}
interpD[x]

(* visualize the derivative *)
p4 = Plot[ interpD[x], {x, 0, 1.7} ]

(* the true derivative behind our data *)
\!\(\*SubscriptBox[\(\[PartialD]\), \(x\)]\ \(Sin[\ Exp[\ x\ ]\ \ ]\)\)

p5 = Plot[ E^x Cos[E^x], {x, 0, 1.7} ]

(* the derivative of the Interpolation gives a good approximation *)
p6 = Plot[ E^x Cos[E^x] - interpD[x], {x, 0, 1.7} ]

(* create a list of derivative values at the original abscissae *)
dataD = Table[ {x, interpD[x]}, {x, 0, 1.7, 0.02} ]

You'll doubtless get a variety of suggestions, as this is a fairly straightforward task for an experienced Mathematican.

Best

Barrie

>>> On 14/01/2012 at 6:53 pm, in message <2012011407...@smc.vnet.net>,

da...@wolfram.com

unread,
Jan 17, 2012, 3:30:40 AM1/17/12
to
Depends on what specifically you have for input, and what specifically you require from a result. Any of interpolation, finite differencing, or Fourier methods might be appropriate for the task at hand.

Some of this was discussed once here.

http://forums.wolfram.com/mathgroup/archive/2003/Apr/msg00627.html

https://groups.google.com/forum/?hl=en#!searchin/comp.soft-sys.math.mathematica/lichtblau$20derivative$20fourier/comp.soft-sys.math.mathematica/mBV0fAqOUmE/pA4ueiXmc5wJ

Daniel Lichtblau
Wolfram Research

Scot T. Martin

unread,
Jan 17, 2012, 3:33:13 AM1/17/12
to
Setting InterpolationOrder->1 within your Interpolation function, and you can often fix the undesirable outcome you have identified.
________________________________________
From: dr DanW [dmaxw...@gmail.com]
Sent: Monday, January 16, 2012 17:12
Subject: how can one use mathematica get the approximate derivative of {x,y} data points?
0 new messages