FD at arbitrary nodesΒΆ

We try to estimate the derivative of \(\cos(x^2)\) at \(x=0.5\) using five nodes.

using FundamentalsNumericalComputation
t = [ 0.35,0.5,0.57,0.6,0.75 ]   # nodes
f = x -> cos(x^2)
dfdx = x -> -2*x*sin(x^2)
exact_value = dfdx(0.5)
-0.24740395925452294

We have to shift the nodes so that the point of estimation for the derivative is at \(x=0\).

w = FNC.fdweights(t.-0.5,1)
5-element Array{Float64,1}:
  -0.5303030303030298
 -21.61904761904763
  45.09379509379508
 -23.333333333333307
   0.38888888888888845
fd_value = dot(w,f.(t))
-0.247307422906135

We can reproduce the weights in the finite difference tables by using equally spaced nodes with \(h=1\). For example, here is a one-sided formula.

Rational.( FNC.fdweights(0:2,1) )
3-element Array{Rational{Int64},1}:
 -3//2
  2//1
 -1//2