gaussLegendreFixedOrder function

double gaussLegendreFixedOrder(
  1. double f(
    1. double
    ),
  2. double a,
  3. double b, {
  4. int order = 5,
})

Gauss-Legendre fixed order integrator (orders 2..5).

Implementation

double gaussLegendreFixedOrder(
  double Function(double) f,
  double a,
  double b, {
  int order = 5,
}) {
  final nodesAndWeights = {
    2: [
      [-0.5773502691896257, 1.0],
      [0.5773502691896257, 1.0],
    ],
    3: [
      [0.0, 1.3333333333333333],
      [-0.7745966692414834, 0.5555555555555556],
      [0.7745966692414834, 0.5555555555555556],
    ],
    4: [
      [-0.3399810435848563, 0.6521451548625461],
      [0.3399810435848563, 0.6521451548625461],
      [-0.8611363115940526, 0.3478548451374538],
      [0.8611363115940526, 0.3478548451374538],
    ],
    5: [
      [0.0, 0.5688888888888889],
      [-0.5384693101056831, 0.47862867049936647],
      [0.5384693101056831, 0.47862867049936647],
      [-0.9061798459386640, 0.23692688505618908],
      [0.9061798459386640, 0.23692688505618908],
    ],
  };
  final list = nodesAndWeights[order];
  if (list == null) throw ArgumentError('Order $order not supported');
  final mid = 0.5 * (a + b);
  final half = 0.5 * (b - a);
  var sum = 0.0;
  for (final nw in list) {
    final x = mid + half * (nw[0]);
    final w = nw[1];
    sum += w * f(x);
  }
  return half * sum;
}