```python
import matplotlib.pyplot as plt
import numpy as np
import sympy as sym
```


```python
def f(x, power, coeff):
    summ = 0
    xk = 1
    coeff = list(map(float, coeff))
    for i in range(int(power), -1, -1):
        summ += float(xk) * coeff[i]
        xk *= x
    return summ
```


```python
def f2(x, power, coeff):
    summ = 0
    xk = 1
    coeff = list(map(float, coeff))
    for i in range(int(power), -1, -1):
        summ += xk * coeff[i]
        xk *= x
    return summ
```


```python
def fprime(x, power, coff):
    return sym.diff(f2(x, power, coff), x)
```


```python
def grad_descent(X, Y, power, coff, eps=0.05, max_iter=200, precision=0.00001):
    X_old = 0
    X_new = 4.9
    Xs = np.zeros(max_iter)
    Ys = np.zeros(max_iter)
    i = 0
    x = sym.symbols('x')
    dfr = fprime(x, power, coff) #diferansiyel
    
    while abs(X_new - X_old) > precision and max_iter > i:
        Xs[i] = X_new
        Ys[i] = f(X_new, power, coff)

        X_old = X_new
        
        df = dfr.evalf(subs={x: X_old})
        X_new = X_old - eps * df
        eps *= 0.99
        i += 1
    return Xs, Ys
```


```python
X = np.arange(-5, 5, 0.1)
Y = np.zeros(len(X))
power = input("Enter power of the function: ")
coeff = input("Enter the function's coefficients: ")
coeff = coeff.split()
for i in range(len(X)):
    Y[i] = f(X[i], power,  coeff)
```

    Enter power of the function: 2
    Enter the function's coefficients: 1 0 0



```python
x_list, y_list = grad_descent(X, Y, power, coeff)
```


```python
plt.subplot(1,2,2)
plt.scatter(x_list,y_list,c="g")
plt.plot(X, Y, c="r")
plt.title("Gradient descent")
plt.show()
```


```python
plt.subplot(1,2,1)
plt.scatter(x_list,y_list,c="g")
plt.plot(X, Y, c="r")
plt.xlim([-3.0,0.3])
plt.title("Zoomed in Gradient descent to Key Area")
plt.show()
```


```python

```

702d2b7e0b91574082170b907d74b185d575ff20
ipynb
Jupyter Notebook
GradientDescent1D.ipynb
fzehracetin/optimization-techniques
ff5f91b2b387e89e75d6727f3185552b064e280e
Apache-2.0
2020-07-21T12:59:51.000Z
2020-07-21T12:59:51.000Z
GradientDescent1D.ipynb
fzehracetin/optimization-techniques
ff5f91b2b387e89e75d6727f3185552b064e280e
Apache-2.0
GradientDescent1D.ipynb
fzehracetin/optimization-techniques
ff5f91b2b387e89e75d6727f3185552b064e280e
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES