How to find slope of curve at certain points
The numpy calculation is the correct one to use, but may be a bit tricky to understand how it is calculated
Your custom calculation is accidentally returning the inverse slope, the x and y values are reversed in the slope function (x1 -&gt; y[i], etc). The slope should be delta_y/delta_x
def slope(x1, y1, x2, y2):
v=slope(y[i], x[i], y[i-1], x[i-1])

Also, you are calculating the slope at x = 1.5, 2.5, etc but numpy is calculating the slope at x = 1, 2, 3
In the gradient calculation, numpy is calculating the gradient at each x value, by using the x-1 and x+1 values and dividing by the difference in x which is 2. You are calculating the inverse of the x + .5 values.
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ]

y=[512, 256, 128, 64 , 32 , 16 , 8  , 7  , 6  , 5  , 4  , 3  , 2  , 1  ]

numpy answer:
[-256. , -192. ,  -96. ,  -48. ,  -24. ,  -12. ,   -4.5,   -1. ,-1. ,   -1. ,   -1. ,   -1. ,   -1. ,   -1. ]

each calculation in numpy function:
-256 = (-256)/1 there is no x-1 value so the default is to use x
-192 = (128-512)/2
-96 =  (64-256)/2
...