19 January 2020 09:15:22 AM
divdif_test:
C version
Test DIVDIF.
TEST01
DATA_TO_DIF_DISPLAY sets up a difference table
and displays intermediate calculations;
DIF_APPEND appends a new data point;
DIF_ANTIDERIV computes the antiderivative;
DIF_DERIV_TABLE computes the derivative;
DIF_SHIFT_ZERO shifts all the abscissas to 0;
DIF_VAL evaluates at a point;
The divided difference table:
1.000000 2.000000 3.000000 4.000000
0 1.000000 4.000000 9.000000 16.000000
1 3.000000 5.000000 7.000000
2 1.000000 1.000000
3 0.000000
The divided difference polynomial:
p(x) = 1.000000
+ ( x - 1.000000 ) * ( 3.000000
+ ( x - 2.000000 ) * ( 1.000000
+ ( x - 3.000000 ) * ( 0.000000
)))
DIF_APPEND can add the data (5,25) to the table.
The updated divided difference polynomial:
p(x) = 25.000000
+ ( x - 5.000000 ) * ( 6.000000
+ ( x - 1.000000 ) * ( 1.000000
+ ( x - 2.000000 ) * ( -0.000000
+ ( x - 3.000000 ) * ( -0.000000
))))
DIF_VAL can evaluate the table at a point.
DIF_VAL reports P(2.500000) = 6.250000
The divided difference table after DIF_SHIFT_ZERO:
p(x) = 0.000000
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( 1.000000
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( -0.000000
))))
The divided difference table for the derivative:
p(x) = 0.000000
+ ( x - 0.000000 ) * ( 2.000000
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( -0.000000
)))
DIF_VAL reports P'(2.500000) = 5.000000
The divided difference table for the antiderivative:
p(x) = 0.000000
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( 0.333333
+ ( x - 0.000000 ) * ( 0.000000
+ ( x - 0.000000 ) * ( -0.000000
)))))
DIF_VAL reports (Anti)P(2.500000) = 5.208333
TEST02
Approximate Y = EXP(X) using orders 1 to 8.
Original data:
X Y
0.000000 1.000000
1.000000 2.718282
2.000000 7.389056
3.000000 20.085537
4.000000 54.598150
5.000000 148.413159
6.000000 403.428793
7.000000 1096.633158
Evaluate at X = 2.500000 where EXP(X) = 12.182494
Order Approximate Y Error
1 1.000000 -11.182494
2 5.295705 -6.886789
3 10.831628 -1.350866
4 12.417007 0.234513
5 12.076491 -0.106003
6 12.252022 0.069528
7 12.126351 -0.056143
8 12.234320 0.051826
dif_basis_test
dif_basis() computes Lagrange basis polynomials
in difference form.
The base points:
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
The table of difference vectors defining the basis polynomials.
Each ROW represents a polynomial.
1.000000 -1.000000 0.500000 -0.166667 0.041667
0.000000 1.000000 -1.000000 0.500000 -0.166667
0.000000 0.000000 0.500000 -0.500000 0.250000
0.000000 0.000000 0.000000 0.166667 -0.166667
0.000000 0.000000 0.000000 0.000000 0.041667
Evaluate basis polynomial #3 at a set of points.
X Y
1.000000 0.000000
1.500000 -0.546875
2.000000 0.000000
2.500000 0.703125
3.000000 1.000000
3.500000 0.703125
4.000000 0.000000
4.500000 -0.546875
5.000000 0.000000
TEST05
DIF_TO_R8POLY converts a difference table to a polynomial;
DIF_SHIFT_ZERO shifts a divided difference table to 0;
These are equivalent operations
The divided difference table:
1.000000 2.000000 3.000000 4.000000
0 -2.000000 2.000000 14.000000 40.000000
1 4.000000 12.000000 26.000000
2 4.000000 7.000000
3 1.000000
The divided difference table:
1.000000 2.000000 3.000000 4.000000
0 -2.000000 2.000000 14.000000 40.000000
1 4.000000 12.000000 26.000000
2 4.000000 7.000000
3 1.000000
The divided difference table:
p(x) = -2.000000
+ ( x - 1.000000 ) * ( 4.000000
+ ( x - 2.000000 ) * ( 4.000000
+ ( x - 3.000000 ) * ( 1.000000
)))
The polynomial using DIF_SHIFT_ZERO:
p(x) = 1.000000 * x ^ 3
- 2.000000 * x ^ 2
+ 3.000000 * x
- 4.000000
The polynomial using DIF_TO_R8POLY:
p(x) = 1.000000 * x ^ 3
- 2.000000 * x ^ 2
+ 3.000000 * x
- 4.000000
TEST06
R8POLY_ANT_COF computes the coefficients of the
antiderivative of a polynomial;
R8POLY_ANT_VAL evaluates the antiderivative of
a polynomial;
R8POLY_DER_COF computes the coefficients of the
derivative of a polynomial;
R8POLY_DER_VAL evaluates the derivative of
a polynomial;
R8POLY_PRINT prints a polynomial;
R8POLY_VAL evaluates a polynomial.
The initial polynomial:
p(x) = 5.000000 * x ^ 4
+ 4.000000 * x ^ 3
+ 3.000000 * x ^ 2
+ 2.000000 * x
+ 1.000000
The antiderivative polynomial:
p(x) = 1.000000 * x ^ 5
+ 1.000000 * x ^ 4
+ 1.000000 * x ^ 3
+ 1.000000 * x ^ 2
+ 1.000000 * x
The derivative polynomial:
p(x) = 20.000000 * x ^ 3
+ 12.000000 * x ^ 2
+ 6.000000 * x
+ 2.000000
Evaluate the polynomial, antiderivative and
derivative, using only the original polynomial
coefficients:
X P(X) Anti_P(X) P'(X)
0.000000 30.000000 0.000000 2.000000
1.000000 44.000000 34.000000 40.000000
2.000000 158.000000 120.000000 222.000000
r8poly_basis_test
r8poly_basis() computes Lagrange basis polynomials
in standard form.
The table of difference vectors defining the basis polynomials.
Each ROW represents a polynomial.
5.000000 -6.416667 2.958333 -0.583333 0.041667
-10.000000 17.833333 -9.833333 2.166667 -0.166667
10.000000 -19.500000 12.250000 -3.000000 0.250000
-5.000000 10.166667 -6.833333 1.833333 -0.166667
1.000000 -2.083333 1.458333 -0.416667 0.041667
One basis polynomial in standard form:
p(x) = 0.250000 * x ^ 4
- 3.000000 * x ^ 3
+ 12.250000 * x ^ 2
- 19.500000 * x
+ 10.000000
Evaluate basis polynomial #3 at a set of points.
X Y
1.000000 0.000000
1.500000 -0.546875
2.000000 0.000000
2.500000 0.703125
3.000000 1.000000
3.500000 0.703125
4.000000 0.000000
4.500000 -0.546875
5.000000 0.000000
r8poly_shift_test
r8poly_shift shifts polynomial coefficients.
Polynomial coefficients for argument X
0 6.000000
1 -1.000000
2 2.000000
SCALE = 2.000000
SHIFT = 3.000000
Polynomial coefficients for argument Z = SCALE * X + SHIFT:
0 12.000000
1 -3.500000
2 0.500000
ncc_rule_test
ncc_rule() computes closed Newton Cotes formulas;
Newton-Cotes Closed Quadrature Rule:
Abscissa Weight
1 -1.000000 0.086921
2 -0.714286 0.414005
3 -0.428571 0.153125
4 -0.142857 0.345949
5 0.142857 0.345949
6 0.428571 0.153125
7 0.714286 0.414005
8 1.000000 0.086921
nco_rule_test
nco_rule() computes open Newton Cotes formulas.
Newton-Cotes Open Quadrature Rule:
Abscissa Weight