You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
toratoratora/函数求最值上课学生代码.py

89 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 8 17:19:05 2021
@author: hzh
"""
# 例2求函数y=x2-10x-30 的最小值
# 绘图
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x ** 2 - 10 * x - 30
x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))
plt.grid()
plt.show()
# 梯度下降法求函数最值
np.random.seed(3)
x = np.random.randn(1); # 10代表数据的维度
# print(x)
learning_rate = 0.1
err = 0.000001;
max_iters = 10000
def f(x):
return x ** 2 - 10 * x - 30
def df(x):
return 2 * x - 10
for i in range(max_iters):
print("%d 次迭代x=%.8f y=%.8f" % (i, x, f(x)))
if abs(df(x)) < err:
break
x = x - df(x) * learning_rate # xk+1=xk- η* f(xk) (迭代公式)
# 练习题1求函数y=x2-8x-25的最小值
# 练习题2
# 已知函数y=x+sqrt(2*R*x-x**2), R为大于0的常数x的取值范围在[R,2*R]之间。
# 当R=10时求该函数在x取值区间内函数的最大值。
# 绘图
# 梯度下降法求函数最大值
# 给定一个二元函数f(x,y)= - exp(x-y)*(x**2-2*y**2)
# 用梯度下降法求其在x<0,y<0范围上的极小值
x = -1;
y = -1
learning_rate = 0.1
err = 0.000001;
max_iters = 10000
def f(x, y):
return -np.exp(x - y) * (x ** 2 - 2 * y ** 2)
def dx(x, y):
return -(np.exp(x - y) * (2 * x) + (x ** 2 - 2 * y ** 2) * np.exp(x - y))
def dy(x, y):
return -(np.exp(x - y) * (-4 * y) + (x ** 2 - 2 * y ** 2) * np.exp(x - y) * (-1))
for t in range(max_iters):
if t % 100 == 0:
print("Iter %d, x=%.8f,y=%.8f,z=%.8f,dx=%.8f,dy=%.8f" % (t, x, y, f(x, y), dx(x, y), dy(x, y)))
if abs(dx(x, y)) < err and abs(dy(x, y)) < err:
print("Iter %d, x=%.8f,y=%.8f,z=%.8f,dx=%.8f,dy=%.8f" % (t, x, y, f(x, y), dx(x, y), dy(x, y)))
break
x = x - learning_rate * dx(x, y);
y = y - learning_rate * dy(x, y) # 迭代公式
'''#练习题:
二元函数f(x,y)=x3-y3+3x2+3y2-9x求最值
三元函数f(x,y,z)=x2+2y2+3z2+2x+4y-6z求最值
'''