モンテカルロ積分

 投稿者:しばっち  投稿日:2008年12月30日(火)09時46分32秒
  多重積分モンテカルロ

RANDOMIZE
PUBLIC NUMERIC LEVEL
INPUT  PROMPT "多重積分 LEVEL=":LEVEL
DIM A(LEVEL),B(LEVEL),N(LEVEL),AA(LEVEL)
FOR I=1 TO LEVEL
!'INPUT  PROMPT "下限  =":A(I)
!'INPUT  PROMPT "上限  =":B(I)
!'INPUT  PROMPT "分割数=":N(I)
   READ A(I),B(I),N(I)
NEXT I
PRINT MONTE(LEVEL,A,B,10000)
!'PRINT MONTE2(LEVEL,A,B,10000,2000)
!'PRINT MONTESIMPSON(LEVEL,A,B,10000)
!'PRINT MONTERECURSIVE(LEVEL,AA,A,B,N)
DATA 0,1,50
DATA 0,1,50
DATA 0,1,50
DATA 0,1,50
END

EXTERNAL  FUNCTION MONTE(LEV,A(),B(),N)
RANDOMIZE
DIM AA(LEV)
LET H=1
FOR I=1 TO LEV
   LET H=H*(B(I)-A(I))
NEXT I
FOR K=1 TO N
   FOR I=1 TO LEV
      LET  AA(I)=A(I)+(B(I)-A(I))*RND
   NEXT I
   LET S=S+FUNC(AA)
NEXT  K
LET MONTE=H*S/N
END FUNCTION

EXTERNAL  FUNCTION MONTE2(LEV,A(),B(),N,NN)
RANDOMIZE
DIM AA(LEV)
LET HMAX=-MAXNUM
LET HMIN=MAXNUM
LET HH=1
FOR I=1 TO LEV
   LET HH=HH*(B(I)-A(I))
NEXT I
FOR K=1 TO NN
   FOR J=1 TO LEV
      LET  AA(J)=A(J)+(B(J)-A(J))*RND
   NEXT J
   LET H=FUNC(AA)
   LET HMIN=MIN(HMIN,MIN(H,0))
   LET HMAX=MAX(H,HMAX)
NEXT K
LET H=HMAX-HMIN
FOR K=1 TO N
   FOR J=1 TO LEV
      LET  AA(J)=A(J)+(B(J)-A(J))*RND
   NEXT J
   LET Z=H*RND+HMIN
   IF FUNC(AA) > 0 THEN
      IF Z > 0 AND Z < FUNC(AA) THEN LET M1=M1+1
   ELSE
      IF Z < 0 AND Z > FUNC(AA) THEN LET M2=M2+1
   END IF
NEXT K
LET MONTE2=HH*(M1-M2)/N*H
END FUNCTION

EXTERNAL  FUNCTION MONTERECURSIVE(LEV,AA(),A(),B(),N()) !'再帰式モンテカルロ
IF LEV=0 THEN
   LET  MONTERECURSIVE=FUNC(AA)
ELSE
   LET  H=B(LEV)-A(LEV)
   FOR K=1 TO N(LEV)
      LET  AA(LEV)=A(LEV)+H*RND
      LET S=S+MONTERECURSIVE(LEV-1,AA,A,B,N)
   NEXT K
   LET MONTERECURSIVE=H*S/N(LEV)
END IF
END FUNCTION

EXTERNAL  FUNCTION MONTESIMPSON(LEV,A(),B(),N)  !'モンテカルロ+シンプソン則
RANDOMIZE
DIM AA(LEV),T(LEV),HH(LEV)
LET H=1
FOR I=1 TO LEV
   LET H=H*(B(I)-A(I))
   LET HH(I)=(B(I)-A(I))/N/2
NEXT I
FOR K=1 TO N
   FOR I=1 TO LEV
      LET T(I)=(B(I)-A(I))*RND+A(I)
      LET AA(I)=A(I)+T(I)
   NEXT I
   LET S=S+1/3*H*FUNC(AA)
   FOR I=1 TO LEVEL
      LET AA(I)=A(I)+HH(I)+T(I)
   NEXT I
   LET S=S+4/3*H*FUNC(AA)
   FOR I=1 TO LEVEL
      LET AA(I)=A(I)+2*HH(I)+T(I)
   NEXT I
   LET S=S+1/3*H*FUNC(AA)
NEXT  K
LET MONTESIMPSON=S/N/2
END FUNCTION

EXTERNAL  FUNCTION FUNC(X())
LET S=1
FOR I=1 TO LEVEL
   LET S=S-X(I)*X(I)
NEXT I
IF S > 0 THEN
   LET FUNC=SQR(S)
ELSE
   LET FUNC=0
END IF
END FUNCTION

-----------------------------------------------
無限区間 多重積分モンテカルロ

PUBLIC NUMERIC LEVEL
INPUT  PROMPT "多重積分 LEVEL=":LEVEL
DIM X(LEVEL)
PRINT MONTEDE(LEVEL,X,10000);PI^(LEVEL/2)
END

EXTERNAL  FUNCTION MONTEDE(LEV,X(),N) !'モンテカルロ + DE法
RANDOMIZE
LET M=4 !'(要) 調整
LET H=2*M/N
FOR J=1 TO N
   LET V=1
   FOR I=1 TO LEV
      LET K=RND*M*2-M
      LET X(I)=Q(K)
      LET V=V*QQ(K)
   NEXT I
   LET S=S+H^LEV*FUNC(X)*V
NEXT J
LET MONTEDE=S*N^(LEV-1)
END FUNCTION

EXTERNAL  FUNCTION FUNC(X())
FOR I=1 TO LEVEL
   LET S=S-X(I)*X(I)
NEXT I
LET  FUNC=EXP(S)
END FUNCTION

EXTERNAL  FUNCTION Q(X)
LET Q=SINH(PI/2*SINH(X))
END FUNCTION

EXTERNAL  FUNCTION QQ(X)
LET QQ=PI/2*COSH(X)*COSH(PI/2*SINH(X))
END FUNCTION
 

戻る