i want to define a function that calculates the " integral" of a function

Dani AI

Generated

Clarify whether a symbolic antiderivative or a numeric definite integral is required. For the C tag, the usual task is numeric definite integration (an algorithm that computes ∫_a^b f(x) dx for a given f). As noted, a general-purpose integrator is nontrivial; 's pointer was useful for question framing, but the technical design still needs choices.

A minimal C pattern is to accept a function pointer and an optional params pointer. Example signature:

typedef double (*func_t)(double x, void *params);

double simpson_integrate(func_t f, void *params, double a, double b, int n);

Choose the algorithm by integrand behavior:

  • Smooth functions: composite Simpson or high-order Newton-Cotes works well.
  • Unknown or erratic behavior: adaptive Simpson or Romberg gives automatic refinement.
  • Very high accuracy or difficult integrands: Gauss-Kronrod quadrature (15/21 point) or established libraries.

Practical tips and pitfalls:

  • Always specify an absolute and relative tolerance and stop when both are satisfied.
  • Use even n for Simpson; adaptive routines handle this automatically.
  • Split intervals at known discontinuities. For integrable singularities or infinite limits, apply a variable transform (for example t = 1/x) or split and map to finite intervals.
  • Test against integrals with known answers (polynomials, exp, sin) to validate error behavior.
  • Prefer a well-tested library (QUADPACK via wrappers, or GNU Scientific Library) over ad-hoc code for production.

References for algorithms and implementations:

Recommended Answers

All 2 Replies

i want to define a function that calculates the " integral" of a function

Not an easy job to define that for any given input function if you are a newbie..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.