用Pycharm实现梯度下降法求余弦曲线的极小值出现了line 13, in <module>
while deta_h< error_rate:
TypeError: '<' not supported between instances of 'tuple' and 'float'
这是代码 ,怎么改,求解,快哭了,小白
import math
import numpy as np
def f(x):
return 0.03693* math.sin ( 1.165 *x- 1.538 ), math .sin (1.165 *x- 1.538)
def h(x):
return 1.165*0.03693*math.cos(1.165*x-1.538), math .cos (1.165 *x- 1.538)
a = 4.32517219917013 # 初始点(初始横坐标)
step = 0.01 #(步长)
count =0 # 记录迭代次数
deta_h = h(a) # a更新前后的差值(初始值设定为起始点,也可以设置为大于阈值的任意的数)
error_rate = 1e-5 # 给定的阈值
while deta_h< error_rate: # 这里怎么改,求解。
b = a-step* h(a) # 更新a,用新的变量接收
deta_h = np.abs(h(b)-h(a))
count += 1
a = b-step*h(b)
y = f( b )
print('迭代次数%d'%count)
print(a)
print(y)
print('极值点为(%f,%f)'%(a, y ))