Numerical Computation Project
1. Parametric Equation
Suppose we are machining a curve on a CNC machine. This curve is typically defined by a parametric equation (e.g., a cubic Bézier curve).
- Curve Position Definition:
Let the parameter be
- Velocity Vector (Tangent Vector):
Differentiating with respect to
- Instantaneous Speed (The Speed Function):
This is the most critical function. The magnitude of the velocity is:
Challenge: Even if
and are simple polynomials, the term under the square root in makes the function extremely complex. It is usually impossible to find an analytical antiderivative (closed-form solution). This is why Numerical Integration is required.
2. The Arc-Length Function
We need to calculate the actual distance traveled (arc length)
In this project, you need to implement a function calculate_arc_length(t).
Since this cannot be integrated manually, this function must internally call the Adaptive Simpson's Integration method to compute the definite integral.
3. Adaptive Simpson's Integration (Method & Derivation)
In numerical control (CNC), machining paths require high precision. A simple fixed-step integration (e.g., dividing the curve into 1000 equal segments) is inefficient: it wastes computation on straight lines and loses precision on sharp corners.
To solve this, we use Adaptive Simpson's Quadrature. This method automatically detects where the function changes rapidly (high curvature) and refines the grid locally using a "Divide and Conquer" strategy.
3.1 The Foundation: Simpson's 1/3 Rule
Simpson's rule improves upon the Trapezoidal rule (which fits straight lines) by fitting a quadratic polynomial (parabola) through three points.
For an arbitrary interval
Let the step size be half the interval width:
. Let the midpoint be
.
The integral is approximated by the area under the parabola passing through
Theoretical Error:
From Taylor series analysis, the error term
3.2 Derivation of the Error Estimator (Richardson Extrapolation)
In a practical CNC system, we do not know the fourth derivative
Assumption: For a sufficiently small interval
Step 1: The Coarse Estimate ( )
We calculate the integral using one single Simpson step over the full interval
Let
Step 2: The Fine Estimate ( )
We split the interval
- The step size for each half is now
. - The error for the left half is
. - The error for the right half is
.
The total error for
Thus, the fine approximation relates to the true value as:
Key Insight: By halving the step size, the error of Simpson's rule drops to 1/16th of its original value.
Step 3: Extracting the Error
We now have a system of two linear equations with two unknowns (
Subtract (Eq. 1) from (Eq. 2):
Rearranging to solve for the term
3.3 The Recursive Algorithm
We define a tolerance TOL (e.g.,
Compute Estimates: Calculate
(one-step) and (two-step). Evaluate Error: Calculate the estimated error magnitude:
. Check Condition:
Success (
): The interval is smooth enough. - Bonus Accuracy: Instead of returning
, we return . This simple addition (adding the estimated error back to the result) increases the order of accuracy from to . This is known as Lyness's or Boole's Rule.
- Bonus Accuracy: Instead of returning
Failure (
): The function is varying too rapidly (high error). Recursion: We simply cannot trust the result here. We split the interval in half and call the function recursively on the left side
and the right side . Tolerance Halving: We pass
TOL/2to the sub-calls. This ensures that the sum of errors from the sub-intervals () does not exceed the total allowed error ( ).
Fail-Safes:
In the actual code implementation, we include two critical checks to prevent infinite loops:
Max Depth: Stop recursion if the depth exceeds a limit (e.g., 20 levels).
Min Interval: Stop if the interval width
becomes smaller than machine epsilon.
4. The Inverse Problem (Finding Parameter )
Goal: We want the tool head to move at a constant velocity
Assume that at physical time
Problem: We know the target distance
This requires solving the integral equation:
This is a Root-Finding Problem. We define a function
We need to find
Derivation of Newton's Iteration:
The iteration formula is
The Numerator
: Here,
must be computed by calling your Adaptive Simpson's Integration, passing the speed function as the integrand. The Denominator
: According to the Fundamental Theorem of Calculus, the derivative of an accumulation function is simply the integrand itself!
Therefore, the denominator is simply the Instantaneous Speed
.
5. Final Iterative Algorithm Formula
Combining the root-finding logic with the numerical integration:
Expressed mathematically with the explicit function call, including the integrand