Back to General discussions forum
def cal_grad(XX,dx):
Gstore=[]
x1,x2,x3=XX[0],XX[1],XX[2]
y1=8*(x1)-1*(x2)-8*(x3)
y11=8*(x1+dx)-1*(x2)-8*(x3)
g1=(y11-y1)/dx
x1-=dx
Gstore.append(g1)
y2=-4*x1+2*x2+9*x3
y22=-4*(x1)+2*(x2+dx)+9*(x3)
g2=(y22-y2)/dx
x2-=dx
Gstore.append(g2)
y3=-8*x1-9*x2+2*x3
y33=-8*(x1)-9*(x2)+2*(x3+dx)
g3=(y33-y3)/dx
x3-=dx
Gstore.append(g3)
return Gstore
X=[0,0,0]
step=0.01
iteri=0
while(True):
x1,x2,x3=X[0],X[1],X[2]
f1=8*x1-1*x2-8*x3-9
f2=-4*x1+2*x2+9*x3+5
f3=-8*x1-9*x2+2*x3+7
f=f1*f1+f2*f2+f3*f3
print(x1,x2,x3)
Y=f
if (Y<0.0001):
break
dx=step/10
G=cal_grad(X,dx)
Xnew=X
for i in range(0,3):
Xnew[i]-=G[i]*step
x11,x22,x33=Xnew[0],Xnew[1],Xnew[2]
f11=8*x11-1*x22-8*x33-9
f22=-4*x11+2*x22+9*x33+5
f33=-8*x11-9*x22+2*x33+7
fnew=f11*f11+f22*f22+f33*f33
Ynew=fnew
if (Ynew<Y):
X=Xnew
step=min(0.1,step*1.25)
else:
step=step/1.25
iteri+=1
print(iteri)
here after first step Ynew>Y.so X value should be taking the previous value.but somehow X is taking the new X value(Xnew here in my code).Why? plz help
Maybe try Xnew=X.copy()
I checked but it's not working.Is my code wrong?
Sorry. On close inspection your gradient function is another problem. You are supposed to apply the same target function there similar to calculating Y. Also the various x -= dx seem unnecessary.
While you are reading this, may I suggest a matrix-based implementation?
yes you can suggest me