Mathematical Formula Optimization

In mathematics, the Taylor series of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point. For most common functions, the function and the sum of its Taylor series are equal near this point. If zero is the point where the derivatives are considered, a Taylor series is also called a Maclaurin series. The Taylor series of a real or complex-valued function f (x) that is infinitely differentiable at a real or complex number α is the power series. In the more compact sigma notation, this can be written as

where, denotes the factorial of , denotes the th derivative of f evaluated at the point .

Although the expanded expansion converges, the function may not be equal to its Taylor series. In actual application, the Taylor series needs to be truncated, taking only finite terms. The Taylor series expansion order may be selected according to the allowed maximum error. When function is expanded, the fitting error around the expansion point is relatively small. Generally, a farther distance between a definition domain and the expansion point indicates a slower convergence and larger error. To simplify the series expression, Taylor series expansion (or Maclaurin series expansion) is performed at for fitting. For example:

They can achieve double 0.01% accuracy by using 6th-order Maclaurin series expansion and 7th-order Maclaurin series expansion respectively.

However, the convergence of some functions in some ranges is slow. The following figure shows the fitting curve of the arcsin function. When x is close to 1, the fitting error is large and cannot be directly Maclaurin series expanded (because the convergence is too slow and even if the expansion order is increased the error is still large). Therefore, segmented fitting needs to be performed for such functions. In this case, to meet the accuracy requirement, perform segmented Taylor series expansion around different expansion points, or map the fitting result of an expansion range whose accuracy meets the requirement to another range by using a mathematical formula.

Figure 1 arcsin function and Taylor series expansion fitting

The following takes the arcsin function as an example to introduce three methods for solving the fitting accuracy problem.

arcsin x Definition

arcsin x is the inverse function of sin x that limits the value range to [–π/2, +π/2]. The domain is [–1, +1]. The function is symmetrical with respect to the y-axis and therefore it is an odd function. The Maclaurin series expansion is expressed as:

In addition, it meets the following requirements:

Method 1: Range Mapping

Because y = arcsin x converges quickly around zero, and fitting accuracy is high, we can map the range around zero to a range around x = 1 by using a formula. After analyzing arcsin x, the formula may be used to perform range mapping. When the range boundary point is , the Maclaurin expansion fitting result in the range may be mapped to the range by using the foregoing formula.

The following figure shows the fitting result.

Figure 2 Fitting diagram of arcsin function

However, when the Maclaurin series expansion order in the range is less than or equal to 13 (with seven coefficients), the error is high from x = 0.68 to x = 0.73 (that is, the relative error requirement of 0.01% is not met), as shown in the following figure.

Figure 3 Relative error curve using 13th-order Maclaurin series expansion

At this point, we can consider using higher-order Maclaurin series expansion to reduce the error.

Method 2: Higher-order Maclaurin Series Expansion

Generally, you can use tools such as MATLAB and Octave to simulate the lowest expansion orders required to meet the accuracy requirement. The higher the expansion order is, the higher the accuracy is. However, more multiply-accumulate operations are required, which deteriorates the execution performance.

During simulation with Octave, the error is less than 0.01% using 15th-order Maclaurin series expansion (with eight coefficients), as shown in the following figure.

Figure 4 Relative error curve using 15th-order Maclaurin series expansion

However, for some functions that are difficult to converge in some ranges, even with higher-order Taylor series expansion, the accuracy requirement cannot be met. Try to map the calculation result of a high-accuracy range (for example, mapping calculation result of range [0, 0.5] to using ) to accuracy unqualified range again. Alternatively, perform Taylor series expansion at different points. The following describes the segmented Taylor series expansion method.

Method 3: Segmented Taylor Series Expansion

When Taylor series expansion is performed on arcsin x at , the infinite series may be represented as:

We may consider using Maclaurin series expansion to perform fitting in the range [0, 0.5], and using Taylor series expansion at to approximate where Maclaurin series expansion is directly used to perform fitting and yielding low accuracy. The result of the range is still obtained by Maclaurin result mapping of range .

As shown in the following figure, the red curve is the Taylor series expansion curve. The fitting effect around 0.6 is satisfactory.

Figure 5 arcsin function and Taylor series expansion at 0.6

The maximum relative error is 0.000070770, meeting the accuracy requirement.

# y is the approximation of arcsin x, and z is the fitting value.
>> max(abs(y-z)./z)
ans =  0.000070770

The error curve can also be viewed in a visualized manner. The code is as follows.

# x is an independent variable in the range [0, 1], y is the approximation of arcsin x, z is the fitting value, and m is a vector whose element sizes are all 0.0001.
figure
plot(x,abs(y-z)./z)
hold on
plot(x,m)

Restrictions

Pay attention to the following points during formula expansion:
  • Using expanded formulas increases building and running time and reduces performance. Therefore, do not expand formulas if the precision error is acceptable.
  • Comply with the following principles during formula expansion:

    Determine the precision fulfillment range of the formula before the replacement. The computation within the fulfillment range does not need to be expanded. For the unfulfilled range, only the precision in the fixed range can be met after formula expansion. Therefore, determine the precision fulfillment range for the expanded formula first, and then deduce the formula. Map the computations of the unfulfillment range to the computations of the fulfillment range.

  • You are advised to encapsulate the expanded formulas that are repeatedly called into functions for computation. The advantages are as follows:
    • Reduce the number of intermediate variables of a single function.
    • Easily rectify errors.