2次函数通过梯度下降法不断逼近临界值
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 27 16:46:06 2017
@author: Administrator
"""
import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
def Create_Function():
return x**2+3*x+9
#对创建的函数进行绘图
x=np.arange(-10, 10, 0.01)
y=Create_Function()
plt.plot(x,y)
plt.show()
#对创建的函数进行求导,并获取最后的导函数
x=sym.Symbol("x")
derivation = sym.diff(Create_Function(), x)
print derivation
#对生成的导函数进行赋值,通过f_func计算出最后的结果
f_func = sym.lambdify(x, derivation)
print f_func(10)
old = 20
new= 30
step = 0.01
precision = 0.000001
while abs(new - old) > precision:
old = new
new = new - step * f_func(new)
print new
print new