This post is Private. Only a writer or those who know its URL can access this post.

Article information
Show article in Markdown
Report article
Help us understand the problem. What is going on with this article?

# プログラミング問題集解答例（問３１）

```import numpy as np
f = lambda x: x**2 + 2 * x + 1
g = lambda x: x**4 - 4 * x**3 - 36 * x**2
```
```%matplotlib inline
import matplotlib.pyplot as plt
x_latent = np.linspace(-5, 5, 100)

plt.plot(x_latent, f(x_latent))
plt.grid()
```

```def numerical_differentiation(f, x):
h = 1e-4
return (f(x+h) - f(x-h)) / (2 * h)

x_history = []
x = init_x
for i in range(step_num):
x = x - lr * grad
x_history.append(x)
return x
```
```grad_history = []
minimum = f(x_optimum)
x_optimum, minimum
```
```(-0.997667494332044, 5.4405826910297606e-06)
```
```plt.plot(grad_history)
plt.grid()
```

```plt.plot(x_history)
plt.grid()
```

```grad_history = []
x_history = []
minimum = f(x_optimum)
x_optimum, minimum
```
```(-1.0000000000000564, 0.0)
```
```plt.plot(grad_history)
plt.grid()
```

```plt.plot(x_history)
plt.grid()
```

```%matplotlib inline
import matplotlib.pyplot as plt
x_latent = np.linspace(-8, 10, 100)

plt.plot(x_latent, g(x_latent))
plt.grid()
```

```grad_history = []
x_history = []
minimum = g(x_optimum)
x_optimum, minimum
```
```(5.106003984774603, -791.3346632735172)
```
```plt.plot(grad_history)
plt.grid()
```

```plt.plot(x_history)
plt.grid()
```

```grad_history = []
x_history = []
x_optimum = gradient_decent(g, 0, lr=0.02, step_num=100)
minimum = g(x_optimum)
x_optimum, minimum
```
```(-2.4538370070007725, -121.40981830661639)
```
```plt.plot(grad_history)
plt.grid()
```

```plt.plot(x_history)
plt.grid()
```

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away