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: c.
Subject: Re: Numerical Differentiation and Integration of Array Data
Date: Sun, 4 Dec 2011 13:43:28 +0100

On 4 Dec 2011, at 12:15, syberraith wrote:

> Actually I kind of understand what your saying.  I've been playing with this:
> 
> n = 1000000
> p = 2 * pi;
> h = p / (n-1);
> g = h / 2;
> 
> function y = f ( x ) 
>   y = sin( x );
> endfunction
> 
> t = linspace(0 - g, p + g, n + 1);
> a = (f(t+g) - f(t-g))./h;
> a = h.*(a(1:end-1) + a(2:end))./2;
> 
> u = linspace(0, p, n);
> s = sum(a.*h)
> s1 = a(end) - a(1)
> #plot(u, a,"-1")
> 
> The more data points I used the closer s got to s1. 

The comparison you are making here is wrong: s and s1 do not represent the same 
quantity, 
they jus happen to have similar values because f = sin(x) and a is an 
approximation 
of cos (x) which are functions with the same periodicity.

the correct comparison is between s and

s1 = f(p) - f(0)

If you do the comparison correctly you get the maximum possible accuracy 
already with about 50 samples.

---------
n = 100
p = 2 * pi;
h = p / (n-1);
g = h / 2;

function y = f ( x ) 
  y = sin( x );
endfunction

t = linspace(0, p, n);
a = (diff (f (t))) / h;

u = linspace(0, p, n);
s = sum(a * h)
s1 = f(p) - f(0)

figure (1)
plot (t(1:end-1)+g, a, t, cos (t))
title ("compare a and cos(t)")

figure (2)
plot (t, f(t), t, sin (t))
title ("compare f(t) and sin(t)")
---------

n =  50
s = -2.4980e-16
s1 = -2.4493e-16
>> s-s1
ans = -4.8708e-18


This said, I think you should sit back and carefully rethink your model, 
you are spending a lot of time on a numerical method to compute an 
approximation "s"
of the quantity "s1" which you already know analytically from the beginning.

c.






reply via email to

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