#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 11 11:25:39 2019

@author: kerkmann
"""
import numpy as np

def FDcoeffV(xbar,x,k):
    # Compute coefficients for finite difference approximation for the
    # derivative of order k at xbar based on grid values at points in x.
    
    x = np.array(x)
    n = len(x)
   
    #if k >= n:
    #    error('***  length(x) must be larger than k')
    
    
    xrow = x-xbar   # displacements x-xbar as a row vector.
    A = np.ones([n,n])
    for i in range(1,n):
        A[i,:] = (xrow**i) / np.math.factorial(i)
    
    b = np.zeros(n);    # b is right hand side,
    b[k] = 1;        # so k'th derivative term remains
    
    c = np.linalg.solve(A,b);           # solve n by n system for coefficients
    
    return c.flatten()