'************************************************************* '* Integration of a discrete function by the weighting * '* coefficients Method * '* --------------------------------------------------------- * '* SAMPLE RUN: * '* * '* Number of points: 7 * '* * '* Input the 7 points, example: 1.5, 0.666666 * '* * '* 0 ? 1, 1 * '* 1 ? 1.5, 0.666666 * '* 2 ? 2, 0.5 * '* 3 ? 2.5, 0.4 * '* 4 ? 3, 0.333333 * '* 5 ? 3.5, 0.285714 * '* 6 ? 4, 0.25 * '* * '* Coefficients A(I): * '* A( 0 ) = 0.1464285714285247 * '* A( 1 ) = 0.7714285714288245 * '* A( 2 ) = 9.642857142799777D-02 * '* A( 3 ) = 0.9714285714292666 * '* A( 4 ) = 9.642857142809665D-02 * '* A( 5 ) = 0.7714285714287445 * '* A( 6 ) = 0.1464285714285451 * '* * '* Integral of F(X) from 1 to 4: * '* I = 1.386657 * '* * '* --------------------------------------------------------- * '* Reference: "Methodes de calcul numerique - Tome 1 * '* By Claude Nowakowski, PS1 1981" [BIBLI 07]. * '* * '* Basic Release By J-P Moreau, Paris. * '* (www.jpmoreau.fr) * '************************************************************* ' See explanations in file dinteg.txt ' ----------------------------------- 'Program weighting_Coeffs DEFDBL A-H, O-Z DEFINT I-N OPTION BASE 0 CLS PRINT INPUT " Number of points: ", N DIM X(N + 1), Y(N + 1), A(N + 1), T(N + 1, N + 1), B(N + 1) PRINT PRINT " Input the "; N; " points, example: 1.5, 0.666666" PRINT N = N - 1 FOR K = 0 TO N PRINT " "; K; : INPUT " ? ", X(K), Y(K) NEXT K 'Calculate the coefficients of the linear system FOR J = 0 TO N T(0, J) = 1# FOR I = 1 TO N T(I, J) = T(I - 1, J) * X(J) NEXT I NEXT J 'right side coefficients AA = 1#: BB = 1# FOR I = 0 TO N AA = AA * X(0): BB = BB * X(N) B(I) = (BB - AA) / (I + 1) NEXT I 'Calculate coefficients A(I) and triangularize FOR K = 0 TO N - 1 FOR I = K + 1 TO N B(I) = B(I) - T(I, K) / T(K, K) * B(K) FOR J = K + 1 TO N T(I, J) = T(I, J) - T(I, K) / T(K, K) * T(K, J) NEXT J NEXT I NEXT K 'Solve triangular linear system T * A = B A(N) = B(N) / T(N, N) FOR I = N - 1 TO 0 STEP -1 S = 0# FOR K = I + 1 TO N S = S + T(I, K) * A(K) NEXT K A(I) = (B(I) - S) / T(I, I) NEXT I PRINT PRINT " Coefficients A(I):" FOR I = 0 TO N PRINT " A("; I; ") = "; A(I) NEXT I 'Calculate Integral of discrete F(X) S = 0# FOR I = 0 TO N S = S + A(I) * Y(I) NEXT I F$ = " I=####.######" 'Print final result PRINT PRINT " Integral of F(X) from "; X(0); " to "; X(N); ":" PRINT USING F$; S PRINT END