数値微分公式

 投稿者:しばっち  投稿日:2019年10月13日(日)19時25分24秒
  MathMLを使用してWebブラウザー上で数式を美麗に表示します。

https://decimalbasic.ninja-web.net/log/article/b/basic/106/jiiymf/jiiymf.html
数値微分公式をWebブラウザー上で表示させます。

OPTION ARITHMETIC RATIONAL
OPTION BASE 0
PUBLIC NUMERIC MAXLEVEL
LET MAXLEVEL=51
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
OPEN #1:NAME "数値"&STR$(NN)&"次微分公式.html"
ERASE #1
PRINT #1:"<!DOCTYPE html>"
PRINT #1:"<html>"
PRINT #1:"<head>"
PRINT #1:"    <title>数値"&STR$(NN)&"次微分公式</title>"
PRINT #1:"    <script async src=";CHR$(34);"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML";CHR$(34);"></script>"
PRINT #1:"</head>"
PRINT #1:"<body>"
PRINT #1:"<h1>数値"&STR$(NN)&"次微分公式</h1>"
FOR N=NN+N1 TO MAXLEVEL STEP 2
   LET  P=INT(N/2+1) !'中央差分式
   !' FOR P=1 TO N
   FOR K=1 TO N
      MAT Y=ZER
      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 #1:"<math>"
   PRINT #1:"<mfrac>"
   PRINT #1:"    <mrow>"
   PRINT #1:"        <msup>"
   PRINT #1:"            <mi>d</mi>"
   IF NN>1 THEN PRINT #1:"            <mn>";STR$(NN);"</mn>"
   PRINT #1:"        </msup>"
   PRINT #1:"    </mrow>"
   PRINT #1:"    <mrow>"
   PRINT #1:"        <msup>"
   PRINT #1:"            <mi>dx</mi>"
   IF NN>1 THEN PRINT #1:"            <mn>";STR$(NN);"</mn>"
   PRINT #1:"        </msup>"
   PRINT #1:"    </mrow>"
   PRINT #1:"</mfrac>"
   PRINT #1:"<mi>f</mi>"
   PRINT #1:"<mfenced>"
   PRINT #1:"    <mi>x</mi>"
   PRINT #1:"</mfenced>"
   PRINT #1:"<mo>=</mo>"
   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 #1:"<mfrac>"
   PRINT #1:"<mrow>"
   FOR I=1 TO N
      IF ABS(M(I))<>0 THEN
         IF M(I)*L(I)<0 THEN
            PRINT #1:"<mo>-</mo>"
         ELSE
            IF I>1 THEN PRINT #1:"<mo>+</mo>"
         END IF
         IF ABS(M(I))<>1 THEN PRINT #1:"<mn>";STR$(ABS(M(I)));"</mn>"
         PRINT #1:"<mi>f</mi>"
         PRINT #1:"<mfenced>"
         PRINT #1:"<mrow>"
         PRINT #1:"<mi>x</mi>"
         IF ABS(P-I)>0 THEN
            IF P-I<0 THEN PRINT #1:"<mo>+</mo>" ELSE PRINT #1:"<mo>-</mo>"
            IF ABS(P-I)>1 THEN PRINT #1:"<mn>";STR$(ABS(P-I));"</mn>"
            PRINT #1:"<mi>h</mi>"
         END IF
         PRINT #1:"</mrow>"
         PRINT #1:"</mfenced>"
      END IF
   NEXT I
   PRINT #1:"</mrow>"
   PRINT #1:"<mrow>"
   PRINT #1:"<mn>";STR$(ABS(L(1)));"</mn>"
   PRINT #1:"<msup>"
   PRINT #1:"<mi>h</mi>"
   IF NN>1 THEN PRINT #1:"<mn>";STR$(NN);"</mn>"
   PRINT #1:"</msup>"
   PRINT #1:"</mrow>"
   PRINT #1:"</mfrac>"
   !'NEXT P
   PRINT #1:"</math>"
   PRINT #1:CHR$(60);CHR$(98);CHR$(114);CHR$(62)
   PRINT #1:CHR$(60);CHR$(98);CHR$(114);CHR$(62)
NEXT N
PRINT #1:"</body>"
PRINT #1:"</html>"
CLOSE #1
END

EXTERNAL SUB MUL(A(),B())
OPTION ARITHMETIC RATIONAL
OPTION BASE 0
DIM C(MAXLEVEL)
FOR I=0 TO 1
   FOR J=0 TO MAXLEVEL-I
      LET C(I+J)=C(I+J)+A(J)*B(I)
   NEXT J
NEXT I
MAT A=C
END SUB

EXTERNAL SUB DERIVATIVE(A())
OPTION ARITHMETIC RATIONAL
OPTION BASE 0
DIM B(MAXLEVEL)
FOR I=MAXLEVEL TO 1 STEP-1
   LET B(I-1)=I*A(I)
NEXT I
MAT A=B
END SUB

EXTERNAL FUNCTION HORNER(A(),XX)
OPTION ARITHMETIC RATIONAL
LET Y=A(MAXLEVEL)
FOR I=MAXLEVEL-1 TO 0 STEP-1
   LET Y=Y*XX+A(I)
NEXT I
LET HORNER=Y
END FUNCTION

EXTERNAL FUNCTION LCM(A,B)
OPTION ARITHMETIC RATIONAL
LET LCM=A*B/GCD(A,B)
END FUNCTION
 

戻る