// rderiv.swift // compute formulas for numerical derivatives on equal spaced points // order is order of derivative, 1 = first derivative, 2 = second // points is number of points where value of function is known // f(x0), f(x1), f(x2) ... f(x points-1) equal spaced // term is the point where derivative is computed // f'(x0) = cx[0]*f(x0) + cx[1]*f(x1) // + ... + cx[points-1]*f(x points-1) // // algorithm: use divided differences to get polynomial p(x) that // approximates f(x). f(x)=p(x)+error term // f'(x) = p'(x) + error term' // substitute xj = x0 + j*h // substitute x = x0 to get p'(x0) etc func rderiv(_ order:Int,_ npoints:Int,_ point:Int,_ hx:Double) -> [Double]{ // var cx = [Double](repeating:0.0,count:npoints) // just test call // for i in 0..b { b=denom[jterm] // largest denominator } // end if } // end jterm for jterm in 0.. Int { var a1 = 0 var b1 = 0 var r = 0 var q = 0 if a == 0 || b == 0 { a1 = 1 return 1 } if abs(a) > abs(b) { a1 = abs(a) b1 = abs(b) } else { a1 = abs(b) b1 = abs(a) } r = 1 while r != 0 { q = a1 / b1 r = a1 - q * b1 a1 = b1 b1 = r } return a1 } // end gcd // end rderiv.swift