help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Numerical Differentiation and Integration of Array Data


From: Jordi Gutiérrez Hermoso
Subject: Re: Numerical Differentiation and Integration of Array Data
Date: Fri, 2 Dec 2011 15:54:41 -0500

On 2 December 2011 15:44, syberraith <address@hidden> wrote:
> I can find only one brief example of how to use diff to estimate a derivative
> and that omits an explanation of how exactly diff computes it results.

Try this:

    t = linspace(0,2*pi,200);
    s = sin(t);
    c = diff(sin(t)) ./ diff(t);

    plot(t, s, "-r;;sin(t)", t(2:end), c, "-b;;cos(t)");

All that diff does is elementwise differences of consecutive entries
in vectors.

> I think I just outright code a central difference algorithm along
> with a trapezoid integration algorithm.  Iike it to run fast so I
> guess I use c or fortran.

You don't neec C or Fortran for something this simple. You can use the
trapz function to integrate and central divides differences of the
vector pair {t,f} representing the function f(t) you can do

    (f(3:end) - f(1:end-3))
             ./
    (diff(f)(1:end-1) + diff(f)(2:end))

You might also be interesting in using and inspecting the source of
the deriv function in the optim package:

    http://octave.sourceforge.net/optim/function/deriv.html

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]