fitChebyshev function
Coefficients c₀…cₙ of the Chebyshev polynomial of degree that best
approximates (least squares) values at points in the interval
−1 ≤ s ≤ 1.
Needs at least degree + 1 points.
Implementation
List<double> fitChebyshev(
List<double> points,
List<double> values,
int degree,
) {
final n = degree + 1;
if (points.length != values.length || points.length < n) {
throw ArgumentError('Too few points for a fit of degree $degree');
}
// Równania normalne (AᵀA)·c = Aᵀ·y, A[k][j] = T_j(s_k).
final ata = List.generate(n, (_) => List.filled(n, 0.0));
final aty = List.filled(n, 0.0);
for (var k = 0; k < points.length; k++) {
final t = chebyshevBasis(points[k], n);
for (var i = 0; i < n; i++) {
aty[i] += t[i] * values[k];
for (var j = 0; j < n; j++) {
ata[i][j] += t[i] * t[j];
}
}
}
return _solve(ata, aty);
}