|
最小値を求める(最急降下法)
PUBLIC NUMERIC H
LET XX=5 !'初期値
LET YY=5
LET ZZ=5
LET H=1/256
LET ALPHA=1/128
LET EPS=1E-8
DO WHILE ABS(DIFF(XX,1,YY,0,ZZ,0))>EPS AND ABS(DIFF(XX,0,YY,1,ZZ,0))>EPS AND DIFF(XX,0,YY,0,ZZ,1)>EPS
LET X=XX-ALPHA*DIFF(XX,1,YY,0,ZZ,0)
LET Y=YY-ALPHA*DIFF(XX,0,YY,1,ZZ,0)
LET Z=ZZ-ALPHA*DIFF(XX,0,YY,0,ZZ,1)
LET XX=X
LET YY=Y
LET ZZ=Z
LOOP
PRINT "MIN-X,Y,Z=";X;Y;Z
PRINT "最小値=";FUNC(X,Y,Z)
END
EXTERNAL FUNCTION FUNC(X,Y,Z)
LET FUNC=(X^2-2)^2+(X^2-Y)^2+(X*Z-1)^2+1
END FUNCTION
EXTERNAL FUNCTION DIFF(X,M,Y,N,Z,O)
IF M>0 THEN
LET DIFF=(-DIFF(X+2*H,M-1,Y,N,Z,O)+8*DIFF(X+H,M-1,Y,N,Z,O)-8*DIFF(X-H,M-1,Y,N,Z,O)+DIFF(X-2*H,M-1,Y,N,Z,O))/(12*H)
EXIT FUNCTION
END IF
IF N>0 THEN
LET DIFF=(-DIFF(X,M,Y+2*H,N-1,Z,O)+8*DIFF(X,M,Y+H,N-1,Z,O)-8*DIFF(X,M,Y-H,N-1,Z,O)+DIFF(X,M,Y-2*H,N-1,Z,O))/(12*H)
EXIT FUNCTION
END IF
IF O>0 THEN
LET DIFF=(-DIFF(X,M,Y,N,Z+2*H,O-1)+8*DIFF(X,M,Y,N,Z+H,O-1)-8*DIFF(X,M,Y,N,Z-H,O-1)+DIFF(X,M,Y,N,Z-2*H,O-1))/(12*H)
EXIT FUNCTION
END IF
IF M=0 OR N=0 OR O=0 THEN LET DIFF=FUNC(X,Y,Z)
END FUNCTION
|
|