FivePointFormulaTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace MathPHP\Tests\NumericalAnalysis\NumericalDifferentiation;
  3. use MathPHP\NumericalAnalysis\NumericalDifferentiation\FivePointFormula;
  4. class FivePointFormulaTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test differentiate zero error using callback - Check that the endpoint formula agrees with f'(x) at x = $_
  8. * @dataProvider dataProviderForTestDifferentiateZeroError
  9. * @param int $x
  10. * @throws \Exception
  11. *
  12. * f(x) = 13x² -92x + 96
  13. * f’(x) = 26x - 92
  14. *
  15. * h⁴
  16. * Error term for the Midpoint Formula: - f⁽⁵⁾(ζ₁)
  17. * 30
  18. *
  19. * where ζ₁ lies between x₀ - 2h and x₀ + 2h
  20. *
  21. * h⁴
  22. * Error term for the Endpoint Formula: - f⁽⁵⁾(ζ₀)
  23. * 5
  24. *
  25. * where ζ₀ lies between x₀ and x₀ + 4h
  26. *
  27. * f'(x) = 26x - 92
  28. * f''(x) = 26
  29. * f⁽³⁾(x) = 0
  30. * ...
  31. * f⁽⁵⁾(x) = 0
  32. * Thus, our error is zero in both formulas for our function $f
  33. */
  34. public function testDifferentiateZeroError(int $x)
  35. {
  36. // Given f(x) = 13x² -92x + 96
  37. $f = function ($x) {
  38. return 13 * $x ** 2 - 92 * $x + 96;
  39. };
  40. // And f’(x) = 26x - 92
  41. $f’ = function ($x) {
  42. return 26 * $x - 92;
  43. };
  44. $expected = $f’($x);
  45. // And
  46. $n = 5;
  47. $a = 0;
  48. $b = 4;
  49. // When
  50. $actual = FivePointFormula::differentiate($x, $f, $a, $b, $n);
  51. // Then
  52. $this->assertEquals($expected, $actual);
  53. }
  54. /**
  55. * @return array (x)
  56. */
  57. public function dataProviderForTestDifferentiateZeroError(): array
  58. {
  59. return [
  60. [0],
  61. [2],
  62. [4],
  63. ];
  64. }
  65. /**
  66. * @test differentiate non-zero error using callback - Check that the endpoint formula agrees with f'(x) at x = $_
  67. * @dataProvider dataProviderForTestDifferentiateNonZeroError
  68. * @param int $x
  69. * @param int $tol
  70. * @throws \Exception
  71. *
  72. * f(x) = x⁵ - 13x² -92x + 96
  73. * f’(x) = 5x⁴ -26x - 92
  74. *
  75. * h⁴
  76. * Error term for the Midpoint Formula: - f⁽⁵⁾(ζ₁)
  77. * 30
  78. *
  79. * where ζ₁ lies between x₀ - 2h and x₀ + 2h
  80. *
  81. * h⁴
  82. * Error term for the Endpoint Formula: - f⁽⁵⁾(ζ₀)
  83. * 5
  84. *
  85. * where ζ₀ lies between x₀ and x₀ + 4h
  86. *
  87. * f'(x) = 5x⁴ - 26x - 92
  88. * f''(x) = 20x³ - 26
  89. * f⁽³⁾(x) = 60x²
  90. * f⁽⁴⁾(x) = 120x
  91. * f⁽⁵⁾(x) = 12 *
  92. * Error in Midpoint Formula on [0,4] (where h=1) < 4
  93. * Error in Endpoint Formula on [0,4] (where h=1) < 24
  94. */
  95. public function testDifferentiateNonZeroError(int $x, int $tol)
  96. {
  97. // Given f(x) = x⁵ - 13x² -92x + 96
  98. $f = function ($x) {
  99. return $x ** 5 - 13 * $x ** 2 - 92 * $x + 96;
  100. };
  101. // And f’(x) = 5x⁴ -26x - 92
  102. $f’ = function ($x) {
  103. return 5 * $x ** 4 - 26 * $x - 92;
  104. };
  105. $expected = $f’($x);
  106. // And
  107. $n = 5;
  108. $a = 0;
  109. $b = 4;
  110. // When
  111. $actual = FivePointFormula::differentiate($x, $f, $a, $b, $n);
  112. // Then
  113. $this->assertEqualsWithDelta($expected, $actual, $tol);
  114. }
  115. /**
  116. * @return array (x, tol)
  117. */
  118. public function dataProviderForTestDifferentiateNonZeroError(): array
  119. {
  120. return [
  121. [0, 24],
  122. [2, 4],
  123. [4, 24],
  124. ];
  125. }
  126. /**
  127. * @test differentiate zero error using points - Check that the endpoint formula agrees with f'(x) at x = $_
  128. * @dataProvider dataProviderForTestDifferentiateZeroError
  129. * @param int $x
  130. * @throws \Exception
  131. *
  132. * f(x) = 13x² -92x + 96
  133. * f’(x) = 26x - 92
  134. *
  135. * h⁴
  136. * Error term for the Midpoint Formula: - f⁽⁵⁾(ζ₁)
  137. * 30
  138. *
  139. * where ζ₁ lies between x₀ - 2h and x₀ + 2h
  140. *
  141. * h⁴
  142. * Error term for the Endpoint Formula: - f⁽⁵⁾(ζ₀)
  143. * 5
  144. *
  145. * where ζ₀ lies between x₀ and x₀ + 4h
  146. *
  147. * f'(x) = 26x - 92
  148. * f''(x) = 26
  149. * f⁽³⁾(x) = 0
  150. * ...
  151. * f⁽⁵⁾(x) = 0
  152. * Thus, our error is zero in both formulas for our function $f
  153. */
  154. public function testDifferentiateZeroErrorUsingPoints(int $x)
  155. {
  156. // Given f(x) = 13x² -92x + 96
  157. $f = function ($x) {
  158. return 13 * $x ** 2 - 92 * $x + 96;
  159. };
  160. $points = [[0, $f(0)], [1, $f(1)], [2, $f(2)], [3, $f(3)], [4, $f(4)]];
  161. // And f’(x) = 26x - 92
  162. $f’ = function ($x) {
  163. return 26 * $x - 92;
  164. };
  165. $expected = $f’($x);
  166. // When
  167. $actual = FivePointFormula::differentiate($x, $points);
  168. // Then
  169. $this->assertEquals($expected, $actual);
  170. }
  171. }