0

how to find slope at certain points circled in blue in below curve ? Are these below 2 approaches valid ? though they give different results . How to automatically find the points where the slope changes drastically in curve like around at point 5,6 in below graph

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  ]
  1. Numpy gradient give below result

np.gradient(y)

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

can we use numpy.gradient to find the slope of curve ? since finding slope of line and curve is bit different Shown in this link

2.Using custom slope function

def slope(x1, y1, x2, y2):
    m = (y2-y1)/(x2-x1)
    return m


slope_value=[]
for i in range(len(y)):
    i += 1
    v=slope(y[i], x[i], y[i-1], x[i-1])
    print(i,v)
    slope_value.append(v)



result: [-0.00390625,  -0.0078125,  -0.015625,  -0.03125,  -0.0625,  -0.125,  -1.0,  -1.0,  -1.0,  -1.0,  -1.0,  -1.0,  -1.0]

enter image description here

star
  • 244
  • 1
  • 2
  • 10
  • 1
    To find a slope that makes sense, you'd need to fit a function to your datapoints. Otherwise they are just finite differences (if that's what you are satisfied with - that's okay). – amzon-ex Jul 06 '20 at 18:09
  • @ amzon-ex ,why slope make sese by fitting a function to datapoints as i just want to find slope and not interested to optimized any parameters of any function . np.gradient(y) dose not fit any function we are passing just an array . – star Jul 06 '20 at 18:14
  • @star have you tried some solutions from https://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy? An issue with using `np.gradient` is that it is an approximation of derivative using a convolution. here's one that's directly applicable: https://stackoverflow.com/questions/16841729/how-do-i-compute-the-derivative-of-an-array-in-python – M Z Jul 06 '20 at 20:07

0 Answers0