Author: Drishika Nadella

Date: 4th March 2021


```python
import numpy as np
from sympy import *
```


```python
def func(x):
    return x*(x-1)
```


```python
def derivative(x, delta):
    f_ = (func(x+delta) - func(x))/delta
    return f_
```


```python
# Analytical derivative

x = Symbol('x')
y = func(x)
yprime = y.diff(x)

f = lambdify(x, yprime, 'numpy')
f(1)
```




    1




```python
print(derivative(1, 10**-2))
```

    1.010000000000001



```python
print(derivative(1, 10**-4))
```

    1.0000999999998899



```python
print(derivative(1, 10**-6))
```

    1.0000009999177333



```python
print(derivative(1, 10**-8))
```

    1.0000000039225287



```python
print(derivative(1, 10**-10))
```

    1.000000082840371



```python
print(derivative(1, 10**-12))
```

    1.0000889005833413



```python
print(derivative(1, 10**-14))
```

    0.9992007221626509


Until $10^{-8}$, the accuracy got better, and then it got worse again. This is because of the multiplication of very small floating point numbers being multiplied with each other, which increases the round-off error.

35813893029e342accb4b028f4437cae4d4d00ca
ipynb
Jupyter Notebook
Week 2/HW2_3.ipynb
drkndl/PH354-IISc
e1b40a1ed11fb1967cfb5204d81ee237df453d39
MIT
Week 2/HW2_3.ipynb
drkndl/PH354-IISc
e1b40a1ed11fb1967cfb5204d81ee237df453d39
MIT
Week 2/HW2_3.ipynb
drkndl/PH354-IISc
e1b40a1ed11fb1967cfb5204d81ee237df453d39
MIT
Qwen/Qwen-72B
1. YES
2. YES