Preparing for differentiation and integration

This commit is contained in:
Dennis Eichhorn 2015-12-25 18:41:48 +01:00
parent 2456a58aed
commit 040aebd836
3 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?php
class FiniteDifference
{
const EPSILON = 0.00001;
public static function getNewtonDifferenceQuotient(\string $formula, array $variables, \int $derivative = 1) : \float
{
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x']])) / self::EPSILON;
}
public static function getSymmetricDifferenceQuotient(\string $formula, array $variables, \int $derivative = 1) : \float
{
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON])) / (2 * self::EPSILON);
}
public static function getFivePointStencil(\string $formula, array $variables, \int $derivative = 1) : \float
{
if ($derivative === 1) {
return (-Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) + 8 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - 8 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON);
} elseif ($derivative === 2) {
return (-Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) + 16 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - 30 * Evaluator::evaluate($formula, ['x' => $variables['x']]) + 16 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON ** 2);
} elseif ($derivative === 3) {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) - 2 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) + 2 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (2 * self::EPSILON ** 3);
} elseif ($derivative === 4) {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) - 4 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) + 6 * Evaluator::evaluate($formula, ['x' => $variables['x']]) - 4 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (self::EPSILON ** 4);
}
throw new \Exception('Derivative too high');
}
}

0
Math/Integral/Gauss.php Normal file
View File

View File