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.4
Starting formula: integral from 0 to 1 of 4 times the square root of 1 minus x squared, equal to pi. 4*sqrt(1-x^2)

Approximations · Application

Set up the integral

Use x as the variable and always write multiplication explicitly, for example 4*x.
Integration interval
a and b must be finite. If a is greater than b, the result changes sign; if a equals b, the integral is 0. You may use pi and e.
Numerical method

The method determines how function values are selected and combined.

More steps generally improve accuracy but require more calculations.
Optional. Enter a non-negative integer to repeat the same sequence, or leave it blank to generate one.
Used to calculate absolute and relative errors. It accepts numbers and constant expressions such as pi or pi/4.
Restore the π example

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 JavaScript algorithm

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.

Ready-made examples

Load an example, then try changing the method or number of steps.

π from a circle

4*sqrt(1-x^2) on [0, 1], with expected value pi.

Load

Sine over half a period

sin(x) on [0, pi], with expected value 2.

Load

π/4 from arctangent

1/(1+x^2) on [0, 1], with expected value pi/4.

Load