help-octave
[Top][All Lists]
Advanced

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

Re: Numerical differentiation, recursion of functions not working


From: Stefan Neumann
Subject: Re: Numerical differentiation, recursion of functions not working
Date: Mon, 29 Mar 2010 12:32:08 +0200



2010/3/29 Thomas Shores <address@hidden>
The problem is in your coding.  You have f(x) hardwired into dx, so you're not really recursing on a finite difference formula when you call it via dx(dx(x)).  What you're actually calculating is the first difference of f(x) evaluated at the first difference of f(x).

Yes, I noticed.

Coding-wise dx(dx(x)) does not work.

What bugs me is that mathematically the second derivative does equal the first derivative of the first derivative.
So it should be possible to code that using a language modeled on mathematical thinking.

Obviously you can always work around the problem by using the established formulas with differential quotients, but you have to use different formulas for 1st, 2nd, 3rd etc derivative.

Not really elegant.
So I was wondering how an elegant solution would look.

Regards,
Stefan


 


On 03/27/2010 08:29 PM, Stefan Neumann wrote:
Hi,

I am trying to numerically differentiate functions. That is no problem, using differential quotients works fine.

But differentiating 2nd order leads to some strange results.
If I calculate by a formula I get correct results, but if I do the (to me) obvious and differentiate the differentiation, meaning dx(dx(x)) , the the results are way off.



This is an example, differentiating 3 different ways:

function y=f(x) y=(x-2).*(x-1).*(x+2) ; endfunction

function dx=dx(x)     dx =(f(x+1e-5)-f(x-1e-5))/2/1e-5 ; endfunction            % central diff-quotient 1st order

function d2x=d2x(x)   d2x=(f(x+1e-5)-2*f(x)+f(x-1e-5))/(1e-5*1e-5) ; endfunction  % central 2nd diff-quotient
function e2x=e2x(x)   e2x=(dx(x+1e-5)-dx(x-1e-5))/2/1e-5 ; endfunction                   % central diff-quotient of central diff-quotient
function f2x=f2x(x)   f2x=dx(dx(x)) ; endfunction                                                                     % calculate d2x() by recursing dx()

Result:    d2x() = e2x()   but f2x() is completely different.

Why is that?

Stefan


_______________________________________________ Help-octave mailing list



reply via email to

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