高階数値微分式
OPTION BASE 0 PUBLIC NUMERIC N LET MAXLEVEL=15 DIM X(1),Y(MAXLEVEL),L(MAXLEVEL),M(MAXLEVEL) INPUT PROMPT "微分階数=":NN IF MOD(NN,2)=0 THEN LET N1=1 ELSE LET N1=2 FOR N=NN+N1 TO MAXLEVEL STEP 2 LET P=INT(N/2+1) !'中央差分式 !' FOR P=1 TO N FOR K=1 TO N FOR I=1 TO N LET Y(I)=0 NEXT I LET Y(0)=1 FOR I=1 TO N IF K<>I THEN LET X(0)=-I LET X(1)=1 CALL MUL(Y,X) END IF NEXT I LET L(K)=HORNER(Y,K) FOR I=1 TO NN CALL DERIVATIVE(Y) NEXT I LET M(K)=HORNER(Y,P) NEXT K PRINT "d^";STR$(NN);"/dx^";STR$(NN);"F(X)="; FOR I=1 TO N LET GM=GCD(M(I),L(I)) LET M(I)=M(I)/GM LET L(I)=L(I)/GM NEXT I LET LM=L(1) FOR I=2 TO N LET LM=LCM(LM,L(I)) NEXT I FOR I=1 TO N LET B=LM/L(I) LET M(I)=M(I)*B LET L(I)=L(I)*B NEXT I PRINT "("; FOR I=1 TO N IF M(I)*L(I)<0 THEN PRINT "-"; ELSE PRINT "+"; PRINT STR$(ABS(M(I)));"*F(X"; IF (P-I)<0 THEN PRINT "+"; ELSE PRINT "-"; PRINT STR$(ABS(P-I));"*H)"; NEXT I PRINT ")/(";STR$(ABS(L(1)));"*H^";STR$(NN);")" !'NEXT P !'PRINT NEXT N END
EXTERNAL SUB MUL(A(),B()) OPTION BASE 0 DIM C(N) FOR I=0 TO 1 FOR J=0 TO N-I LET C(I+J)=C(I+J)+A(J)*B(I) NEXT J NEXT I FOR I=0 TO N LET A(I)=C(I) NEXT I END SUB
EXTERNAL SUB DERIVATIVE(A()) OPTION BASE 0 DIM B(N) FOR I=N TO 1 STEP -1 LET B(I-1)=I*A(I) NEXT I FOR I=0 TO N LET A(I)=B(I) NEXT I END SUB
EXTERNAL FUNCTION HORNER(A(),XX) LET Y=A(N) FOR I=N-1 TO 0 STEP -1 LET Y=Y*XX+A(I) NEXT I LET HORNER=Y END FUNCTION
EXTERNAL FUNCTION GCD(M,N) DO WHILE N<>0 LET T=MOD(M,N) LET M=N LET N=T LOOP LET GCD=M END FUNCTION
EXTERNAL FUNCTION LCM (A, B) LET LCM=A*B/GCD(A,B) END FUNCTION
|