SecondDerivativeMidpointFormulaTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace MathPHP\Tests\NumericalAnalysis\NumericalDifferentiation;
  3. use MathPHP\NumericalAnalysis\NumericalDifferentiation\SecondDerivativeMidpointFormula;
  4. use MathPHP\Exception;
  5. class SecondDerivativeMidpointFormulaTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test differentiate zero error - callback
  9. * @throws \Exception
  10. *
  11. * f(x) = 13x² -92x + 96
  12. * f'(x) = 26x - 92
  13. * f''(x) = 26
  14. *
  15. * h²
  16. * Error term for the Second Derivative Midpoint Formula: - f⁽⁴⁾(ζ)
  17. * 12
  18. * where ζ lies between x₀ - h and x₀ + h
  19. *
  20. * f'(x) = 26x - 92
  21. * f''(x) = 26
  22. * f⁽³⁾(x) = 0
  23. * f⁽⁴⁾(x) = 0
  24. * Thus, our error is zero in our formula.
  25. */
  26. public function testZeroErrorCallback()
  27. {
  28. // Given f(x) = 13x² -92x + 96
  29. $f = function ($x) {
  30. return 13 * $x ** 2 - 92 * $x + 96;
  31. };
  32. // And $f’’ - 26
  33. $f’’ = function ($x) {
  34. return 26;
  35. };
  36. // And
  37. $n = 3;
  38. $a = 0;
  39. $b = 4;
  40. // And f'(x) at x = 2
  41. $target = 2;
  42. $expected = $f’’($target);
  43. // When
  44. $actual = SecondDerivativeMidpointFormula::differentiate($target, $f, $a, $b, $n);
  45. // Then
  46. $this->assertEquals($expected, $actual);
  47. }
  48. /**
  49. * @test differentiate nonzero error - callback
  50. * @throws \Exception
  51. *
  52. * f(x) = x⁴ - 13x² -92x + 96
  53. * f'(x) = 4x³ - 26x - 92
  54. * f''(x) = 12x² - 26
  55. *
  56. * h²
  57. * Error term for the Second Derivative Midpoint Formula: - f⁽⁴⁾(ζ)
  58. * 12
  59. * where ζ lies between x₀ - h and x₀ + h
  60. *
  61. * f'(x) = 4x³ - 26x - 92
  62. * f''(x) = 12x² - 26
  63. * f⁽³⁾(x) = 24x
  64. * f⁽⁴⁾(x) = 24
  65. * Error in Second Derivative Midpoint Formula on [0,2] (where h=1) < 2
  66. */
  67. public function testNonZeroErrorCallback()
  68. {
  69. // Given f(x) = x⁴ - 13x² -92x + 96
  70. $f = function ($x) {
  71. return $x ** 4 - 13 * $x ** 2 - 92 * $x + 96;
  72. };
  73. // And $f’’(x) = 12x² - 26
  74. $f’’ = function ($x) {
  75. return 12 * $x ** 2 - 26;
  76. };
  77. // And
  78. $n = 3;
  79. $a = 0;
  80. $b = 2;
  81. // And f’’(x) at x = 1
  82. $target = 1;
  83. $tol = 2;
  84. $expected = $f’’($target);
  85. // When
  86. $actual = SecondDerivativeMidpointFormula::differentiate($target, $f, $a, $b, $n);
  87. // Then
  88. $this->assertEqualsWithDelta($expected, $actual, $tol);
  89. }
  90. /**
  91. * @test differentiate zero error - using points
  92. * @throws \Exception
  93. *
  94. * f(x) = 13x² -92x + 96
  95. * f'(x) = 26x - 92
  96. * f''(x) = 26
  97. *
  98. * h²
  99. * Error term for the Second Derivative Midpoint Formula: - f⁽⁴⁾(ζ)
  100. * 12
  101. * where ζ lies between x₀ - h and x₀ + h
  102. *
  103. * f'(x) = 26x - 92
  104. * f''(x) = 26
  105. * f⁽³⁾(x) = 0
  106. * f⁽⁴⁾(x) = 0
  107. * Thus, our error is zero in our formula.
  108. */
  109. public function testZeroErrorPoints()
  110. {
  111. // Given f(x) = 13x² -92x + 96
  112. $f = function ($x) {
  113. return 13 * $x ** 2 - 92 * $x + 96;
  114. };
  115. $points = [[0, $f(0)], [2, $f(2)], [4, $f(4)]];
  116. // And $f’’ - 26
  117. $f’’ = function ($x) {
  118. return 26;
  119. };
  120. // And f'(x) at x = 2
  121. $target = 2;
  122. $expected = $f’’($target);
  123. // When
  124. $actual = SecondDerivativeMidpointFormula::differentiate($target, $points);
  125. // Then
  126. $this->assertEquals($expected, $actual);
  127. }
  128. /**
  129. * @test differentiate target exception
  130. * @throws \Exception
  131. */
  132. public function testTargetException()
  133. {
  134. // Given f(x) = 13x² -92x + 96
  135. $f = function ($x) {
  136. return 13 * $x ** 2 - 92 * $x + 96;
  137. };
  138. $points = [[0, $f(0)], [2, $f(2)], [4, $f(4)]];
  139. // And
  140. $f’’ = function ($x) {
  141. return 26;
  142. };
  143. $target = 87348738473;
  144. // Then
  145. $this->expectException(Exception\BadDataException::class);
  146. // When
  147. $actual = SecondDerivativeMidpointFormula::differentiate($target, $points);
  148. }
  149. }