Approximations · Application
Approximations · Application
Calculating π
Start with the integral of 4√(1 − x²) over [0, 1] to approximate π, or enter any allowed function and choose the interval.
Secure calculation with PHP 8.44*sqrt(1-x^2)
Method guide
How the methods work
Each method approximates the same integral, but selects and combines the function values in a different way.
In the starting example, f(x) = 4√(1 − x²) on [0, 1]: the integral represents the area of the unit circle and equals π.
Midpoint
It divides [a, b] into n equal subintervals. In each one, it evaluates f at the centre and multiplies that value by the width h; the sum of the contributions approximates the integral.
It uses n function evaluations. For a sufficiently smooth f, the error is of order h squared; doubling n can reduce it by about a factor of four.
Trapezoidal
It evaluates f at the subdivision nodes and replaces the curve with straight line segments. Each pair of consecutive values forms one trapezoidal contribution.
By reusing shared nodes, it uses n plus one function evaluations. For a sufficiently smooth f, the error is of order h squared; doubling n can reduce it by about a factor of four.
Simpson
Over each pair of subintervals, it replaces the curve with a parabola. The interior nodes receive alternating weights of 4 and 2.
It requires an even n and uses n plus one function evaluations. If f has a sufficiently smooth fourth derivative, the error is of order h to the fourth power; doubling n can reduce it by about a factor of sixteen.
Your earlier JavaScript code uses the same composite Simpson rule: coefficiente = -coefficiente + 6 correctly alternates the weights 4 and 2. On a general interval, however, the first interior node is x = a + h, not x = h; when a = 0, the two expressions coincide.
Monte Carlo
This laboratory uses the sample-mean method, not point counting inside a circle. It draws N uniform values between 0 and 1, maps them to N points between the endpoints, and multiplies the mean of the corresponding function values by b minus a.
It uses N function evaluations. When the sampled values have finite variance, the typical statistical uncertainty scales as one divided by the square root of N: halving it requires about four times as many samples. The result remains random; the same seed reproduces the same sequence.
The accuracy statements for the midpoint, trapezoidal and Simpson methods assume that f has the required continuous derivatives throughout the interval. Discontinuities, cusps, singular endpoints or rapid oscillations can change the behaviour. In particular, 4√(1 − x²) has unbounded derivatives near x = 1, so the theoretical orders may not be fully observed in the π example.
Safe syntax
The expression is not executed as code. A parser accepts only the listed elements and does not use eval().
Available operators, constants and functions
- Variable: x
- Operators: +, -, *, / and ^
- Functions: sqrt, sin, cos, tan, asin, acos, atan, abs, exp, log, ln and log10
- Constants: pi and e
- Use a decimal point, for example 0.5.
- Multiplication must be explicit: write 2*x, not 2x.
The function must return finite values throughout the interval. Roots, logarithms, divisions and tangents can have invalid points.