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: Carlo de Falco
Subject: Re: Numerical Differentiation and Integration of Array Data
Date: Sat, 3 Dec 2011 13:47:47 +0100

On 3 Dec 2011, at 13:22, c. wrote:

> 
> On 3 Dec 2011, at 11:51, syberraith wrote:
> 
>> My guess is that octave would be noticably slow then a compiled program. 
>> The c program does all three functions even with 1 million data points each
>> as fast as I can release the return key.  :)
> 
> The time taken by Octave is also barely noticeable:
> 
>>> tic,
>>> n = 1000000; # One million samples
>>> p = 0.005;   # Time period
>>> a = 5;    # Just a variable parameter
>>> h = p / (n-1);  # dt
>>> g = h / 2;  # dt/2 for my version of central diff
>>> t = linspace (0, p , n);
>>> tmp = a + sin ((2 * pi / p) * t);
>>> f = diff (tmp) / h;
>>> s = g * sum (f(1:end-1) + f(2:end))
> s = -6.2832e-06
>>> toc
> Elapsed time is 0.1559 seconds.
> 
> c.

and using the centered scheme as suggested by Jordi greatly improves the 
accuracy whithout impact on the efficiency

>> tic,
>> n = 1000000; # One million samples
>> p = 0.005;   # Time period
>> a = 5;    # Just a variable parameter
>> h = p / (n-1);  # dt
>> t = linspace (0, p , n);
>> tmp = a + sin ((2 * pi / p) * t);
>> f = [(tmp(2)-tmp(1))/h, (tmp(3:end)-tmp(1:end-2))/(2*h), 
>> (tmp(end)-tmp(end-1))/h];
>> s = (h/2) * sum (f(1:end-1) + f(2:end))
s = -2.8232e-14
>> toc
Elapsed time is 0.1762 seconds.
>> 

BTW why do you first compute the derivative of  a + sin ((2 * pi / p) * t) and 
then integrate it back?
you could get the same result simply as s = tmp(end) - tmp(1) ...


c.




reply via email to

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