【Python讲优化】S06E07 一元函数的极值：运用迭代法

### 1.起步：用牛顿法解方程

#### 1.1.原理分析

$f(x)\approx f(x_0)+(x-x_0)f'(x_0)$

$f(x_0)+(x-x_0)f'(x_0)=0\Rightarrow x=x_0-\frac{f(x_0)}{f'(x_0)}$

#### 1.2.实际举例

import matplotlib.pyplot as plt
import numpy as np
import seaborn
seaborn.set()

def f(x):
return x**3-12*x**2+8*x+40

def df(x):
return 3*x**2-24*x+8

def newton(x):
return x - f(x)/df(x)

x0 = 14
x_list = []
x_list.append(x0)

while(True):
x = newton(x_list[-1])
if abs(x-x_list[-1])<=1e-5:
break
x_list.append(x)

fig, ax = plt.subplots(2, 1)

x = np.linspace(0, 15, 1000)
plt.xlim(0, 15)
ax[0].plot(x, f(x))

x = np.linspace(10.5, 14.5, 1000)
plt.xlim(10.5, 14.5)
ax[1].plot(x, f(x))
ax[1].plot(x_list, [0]*len(x_list), 'ko')
print('x={},f(x)={}'.format(x_list[-1], f(x_list[-1])))
print(x_list)
plt.show()

x=10.933723301726161,f(x)=0.0003467730414286052
[14, 11.907692307692308, 11.079933151128879, 10.937805495069655, 10.933723301726161]

$[14, 11.9076, 11.0799, 10.9378, 10.9337]$。他们逐步逼近方程的一个根，最终收敛到一个方程的近似根，即：$x=10.9337$。经过验算，此时的函数取值$f(x)$已经相当接近于$0$了。这个过程，恰是计算机程序所擅长的。

### 2.利用牛顿法求一元函数极值

#### 2.2.实际举例

`
import matplotlib.pyplot as plt
import numpy as np
import seaborn
seaborn.set()

def f(x):
return x*2-2np.sin(x)

def df(x):
return 2x-2np.cos(x)

def d2f(x):
return 2+2*np.sin(x)

def newton(x):
return x - df(x)/d2f(x)

x0 = 3
x_list = []
x_list.append(x0)