0

I want to find the derivative of a function (x)(x - 1) using the definition of a derivative. I want my increments to be 1e-2. So that it simulates the limit going to zero. I saw on Range for Floats that I could use user-defined functions to create range functions that take float variables.

def frange(x, y, jump):
    while x < y:
        yield x
        x += jump

    def drange(start, stop, step):
     r = start
     while r < stop:
        yield r
        r += step

i = frange(1e-14,1e-2,2)

for k in i:
    set  = []
    x = 1
    dvt = ((x + k ) * (x + k - 1) - x*(x - 1))/k

    set.append(dvt)
    print(set)

When I run the program I only get

[0.9992007221626509]

What is going on that I am not getting more than one derivative added to the list?

Community
  • 1
  • 1
  • 2
    `jump` is `2` so you go from `1e-14` to `1e-2` immediately. – Peter Wood Jan 27 '16 at 21:12
  • 1
    You can also differentiate symbolically using [sympy](http://docs.sympy.org/latest/tutorial/calculus.html#derivatives) `(x*(x-1)).diff(x)` -> `2*x - 1` -> `.subs({x: 1})` -> `1`, and using a few [other methods](http://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy) – SiggyF Jan 27 '16 at 21:34

2 Answers2

1

set You are saying

x += jump

This sets the value of x to 2 + 1e-14 which is greater than 1e-2

As I read the code, it seems that you may mean

myjump = pow(10, jump) #outside the loop

x *= myjump # inside the loop

This will multiply each loop through by 100 in the example and process 1e-14, 1e-12, 1e-10 ... 1e-2

Alternatively, if you meant to add it, then you should have said

x += myjump # inside the loop

or you need to test that jump is actually a fraction that is small enough to be processed.

sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • I think they want to step by a decimal fraction. – Peter Wood Jan 27 '16 at 21:25
  • @PeterWood if that was the case, he would not have used the value 2 in his example. Based on the example, That is why I read his code as wanting 1e2 as the jump and using it as a multiplyer. If he wanted to add a value he would have put in a fraction explicitly. I will add the possibility to my answer – sabbahillel Jan 27 '16 at 21:55
0

Here is a cleaned-up version:

def fn(x):
    return x * (x - 1)

def numerical_diff(fn, x, delta):
    return (fn(x + delta) - fn(x)) / delta

def geometric_series(a, r, n):
    value = a
    for i in range(n):
        yield value     # a * r ** i
        value *= r

def main():
    x = 1.
    for delta in geometric_series(0.01, 0.01, 7):
        print(
            "x = {:5.3f}  f(x) = {:5.3f}  delta = {:16.14f}  f'(x) = {:16.14f}"
            .format(x, fn(x), delta, numerical_diff(fn, x, delta))
        )

if __name__ == "__main__":
    main()

which produces

x = 1.000  f(x) = 0.000  delta = 0.01000000000000  f'(x) = 1.01000000000000
x = 1.000  f(x) = 0.000  delta = 0.00010000000000  f'(x) = 1.00009999999989
x = 1.000  f(x) = 0.000  delta = 0.00000100000000  f'(x) = 1.00000099991773
x = 1.000  f(x) = 0.000  delta = 0.00000001000000  f'(x) = 1.00000000392253
x = 1.000  f(x) = 0.000  delta = 0.00000000010000  f'(x) = 1.00000008284037
x = 1.000  f(x) = 0.000  delta = 0.00000000000100  f'(x) = 1.00008890058334
x = 1.000  f(x) = 0.000  delta = 0.00000000000001  f'(x) = 0.99920072216265
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99