'************************************************************************* '* EVALUATE A POLYNOMIAL AND ITS DERIVATIVES BY HORNER'S METHOD * '* --------------------------------------------------------------------- * '* SAMPLE RUN: * '* * '* EVALUATE A POLYNOMIAL AND ITS DERIVATIVES BY HORNER'S METHOD * '* * '* Example: P(X) = X^4 + 2X^3 +3X^2 + 4X + 5 * '* * '* X P(X) P'(X) P"(X) P"'(X) * '* ------------------------------------------------------------------ * '* 0.00000 5.00000 4.00000 6.00000 12.00000 * '* 0.20000 5.93760 5.47200 8.88000 16.80000 * '* 0.40000 7.23360 7.61600 12.72000 21.60000 * '* 0.60000 9.04160 10.62400 17.52000 26.40000 * '* 0.80000 11.55360 14.68800 23.28000 31.20000 * '* 1.00000 15.00000 20.00000 30.00000 36.00000 * '* 1.20000 19.64960 26.75200 37.68000 40.80000 * '* 1.40000 25.80960 35.13600 46.32000 45.60000 * '* 1.60000 33.82560 45.34400 55.92000 50.40000 * '* 1.80000 44.08160 57.56800 66.48000 55.20000 * '* 2.00000 57.00000 72.00000 78.00000 60.00000 * '* ------------------------------------------------------------------ * '* * '* Press any key to continue * '* * '* --------------------------------------------------------------------- * '* Reference: From Numath Library By Tuan Dang Trong in Fortran 77 * '* [BIBLI 18]. * '* * '* Basic Release 1.0 By J-P Moreau, Paris * '* (www.jpmoreau.fr) * '************************************************************************* 'PROGRAM TEST_HORNER DEFDBL A-H, O-Z DEFINT I-N 'LOGICAL IB N = 4 'ORDER OF POLYNOMIAL NP = 11 'NUMBER OF POINTS K = 3 'NUMBER OF DERIVATIVES DIM A(N + 1), R(K + 1) 'define coefficients A(1) = 1# A(2) = 2# A(3) = 3# A(4) = 4# A(5) = 5# X1 = 0# 'X BEGIN X2 = 2# 'X END IB = 1 'EVALUATE ONLY DX = (X2 - X1) / (NP - 1) X0 = X1 - DX F\$ = "####.#####" CLS PRINT PRINT " EVALUATE A POLYNOMIAL AND ITS DERIVATIVES BY HORNER"; "S METHOD" PRINT PRINT " Example: P(X) = X^4 + 2X^3 +3X^2 + 4X + 5" PRINT PRINT " X P(X) P'(X) P''(X) P'''(X)" PRINT " ---------------------------------------------------------------------" FOR I1 = 1 TO NP X0 = X0 + DX GOSUB 1000 'CALL HORNER(N,A,X0,K,IFLG,R) PRINT USING F\$; X0; FOR J = 1 TO K + 1 PRINT " "; PRINT USING F\$; R(J); NEXT J PRINT NEXT I1 PRINT " ---------------------------------------------------------------------" PRINT END 1000 'SUBROUTINE HORNER (N,A,X0,K,B,R) '--------------------------------------------------------------------- ' EVALUATE A POLYNOMIAL AND ITS DERIVATIVES BY HORNER"S METHOD ' ' INPUTS: ' N ORDER OF POLYNOMIAL ' A VECTOR OF SIZE N+1 STORING THE COEFFICIENTS OF ' POLYNOMIAL IN DECREASING ORDERS OF POWERS ' I.E. P(X) = A(1)*X**N+A(2)*X**(N-1)+...+A(N)*X+A(N+1) ' X0 GIVEN ARGUMENT ' K MAXIMUM ORDER OF DERIVATIVES ASKED FOR ' IB FLAG ' = TRUE, EVALUATION ONLY ' = FALSE, DETERMINATION OF POLYNOMIAL COEFFICIENTS ' NEAR X0 ' R VECTOR OF SIZE K+1 CONTAINS: ' - VALUES P(X0), P"(X0), P""(X0),..PK(X0), ' IF B IS TRUE AND IF K < N ' - THE N+1 COEFFICIENTS OF POLYNOMIAL ' Q(X-X0) = R(1)+R(2)*(X-X0)+...+R(N+1)*(X-X0)**(N), ' IF B IS FALSE AND IF K = N. ' ' REFERENCE: ' ALGORITHM 337, COLLECTED ALGORITHMS FROM CACM, W.PANKIEWICKZ '--------------------------------------------------------------------- RR = A(1) FOR I = 1 TO K + 1 R(I) = RR NEXT I FOR J = 2 TO N + 1 R(1) = R(1) * X0 + A(J) NMJ = N - J + 1 IF NMJ > K THEN L = K ELSE L = NMJ END IF FOR I = 2 TO L + 1 R(I) = R(I) * X0 + R(I - 1) NEXT I NEXT J IF IB <> 0 THEN L = 1 FOR I = 2 TO K + 1 L = L * (I - 1) R(I) = R(I) * L NEXT I END IF RETURN 'end of file thorner.bas