import matplotlib.pyplot as plt
import sys
import numpy as np
x1=np.array([2104,1600,2400,1416,3000])
x2=np.array([3,3,3,2,4])
y=np.array([400,330,369,232,540])
e0=0.01
th0=1
th1=1
th2=1
j=0
alpho=0.01
def hy(xx1,xx2):
return th0+th1*xx1+th2*xx2
# th0=th0-(hy(x1[0],x2[0])-y[0])*alpho*1e-1
# print(th0)
# print(hy(x1[0],x2[0])-y[0])
# th0=th0-(hy(x1[0],x2[0])-y[0])*alpho*1e-3
# print((hy(x1[0],x2[0])-y[0])*alpho*1e-3)
# print(th0)
# th1=th1-(hy(x1[0],x2[0])-y[0])*x1[0]*alpho*1e-6
# print((hy(x1[0],x2[0])-y[0])*x1[0]*alpho*1e-6)
# print(th1)
# th2=th2-(hy(x1[0],x2[0])-y[0])*x2[0]*alpho*1e-3
# print((hy(x1[0],x2[0])-y[0])*x2[0]*alpho*1e-3)
# print(th2)
# while (abs(hy(x1[0],x2[0])-y[0])>e0 and abs(hy(x1[0],x2[0])-y[0])*x1[0]>e0 and abs(hy(x1[0],x2[0])-y[0])*x2[0])>e0:
j=0
while(j<20000000):
th0=th0-(hy(x1[j%4],x2[j%4])-y[j%4])*alpho*1e-2
th1=th1-(hy(x1[j%4],x2[j%4])-y[j%4])*x1[j%4]*alpho*1e-5
th2=th2-(hy(x1[j%4],x2[j%4])-y[j%4])*x2[j%4]*alpho*1e-2
if(abs(hy(x1[j%4],x2[j%4])-y[j%4])<e0 and abs((hy(x1[j%4],x2[j%4])-y[j%4])*x1[j%4])<e0 and abs((hy(x1[j%4],x2[j%4])-y[j%4])*x2[j%4])<e0):
print("true")
print(hy(x1[j%4],x2[j%4])-y[j%4])
print(abs((hy(x1[j%4],x2[j%4])-y[j%4])*x1[j%4]))
print(abs((hy(x1[j%4],x2[j%4])-y[j%4])*x2[j%4]))
print(th0,th1,th2)
j=j+1
print("a")
print((hy(x1[0],x2[0])-y[0]))
print(th0,th1,th2)
可行结果
代入结果验证
验证结果和y值相近
y=400,330,369,232,540