/// <summary> /// Get definite integral using composite trapezium rule /// </summary> /// <param name="f">Integrand</param> /// <param name="a">Lower bound</param> /// <param name="b">Upper bound</param> /// <param name="eps">Error</param> /// <returns>Result</returns> public static double CompositeTrapeziumRule(Program.Function f, double a, double b, double eps) { if (b < a) { throw new Exception("Upper integral bound is less than lower"); } if (a == b) { return(0); } // (?) All the program works only with constant numper of integral steps double n = 200; // 1 + 1 / math.Sqrt(eps); double bounds = (f(a) + f(b)) / 2; double prevResult, nextResult = 0; do { prevResult = nextResult; nextResult = bounds; double h = (b - a) / n; for (int i = 1; i < n; i++) { nextResult += f(a + i * h); } nextResult *= h; n *= 2; } //while (Math.Abs(nextResult - prevResult) > eps); /* Uncomment this and comment line below to use error checking */ while (false); return(nextResult); }
double B; // Upper bound of interval /// <summary> /// Create polinomial instance /// </summary> /// <param name="f">Function to approximate</param> /// <param name="basisFunctions">Basis to use</param> /// <param name="rule">Rule to use for integration</param> /// <param name="solveMethod">Method to solve linear system</param> /// <param name="a">Lower bound of interval</param> /// <param name="b">Upper bound of interval</param> /// <param name="degree">Polinomial degree</param> public Polinomial(Program.Function f, Basis basisFunctions, Integral.Rule rule, LinearSystem.SolveMethod solveMethod, double a, double b, int degree) { // Initialize fields function = f; basis = basisFunctions; integralRule = rule; A = a; B = b; // Fill matrix with integrals - bind linear system double[,] matrix = new double[degree, degree + 1]; for (int i = 0; i < degree; i++) { for (int j = i; j <= degree; j++) { if (j < degree) { matrix[i, j] = matrix[j, i] = rule( delegate(double t) { return(basis[i](t) * basis[j](t)); } , -1, 1, 0.001); } else { matrix[i, j] = rule( delegate(double t) { return(basis[i](t) * f((b + a) / 2 + t * (b - a) / 2)); } , -1, 1, 0.001); } } } // Solve linear system coefficients = solveMethod(matrix).ToArray(); }