Approximates a given function using the Stiefel-Remes method.

You're viewing an older version of this page (#280). View the current version.

View versions (1)

Interface

#include <codecogs/maths/approximation/regression/stiefel.h>

using namespace Maths::Approximation::Regression;

Overview

This algorithm gives the best approximation of a continuous function with real argument in the sense of maximum norm. It uses the Stiefel-Remes method.

An important detail when using this class is that the abscissas array given as argument to the constructor needs to be sorted in ascending order and its elements need to be equally spaced, meaning that:

X[i] - X[i - 1] = \alpha \in \mathcal{R} \mbox{ a constant } \qquad 1 \leq i \leq N - 1
(1)

where N is the size of the array. In other words, the associated function needs to be discrete.

Below you will find the regression graphs for a set of points obtained by evaluating the function f(x) = \sin(2x) / x, displayed in light blue, at particular abscissas. The Stiefel-Remes polynomial, displayed in red, has been calculated using this class. In the first graph there had been chosen a number of 12 points and degree of polynomial 9, while in the second 36 points were considered with the polynomial degree 16. You may notice the root mean squared error in each of the cases.

1/stiefel1-378.png

Choosing too high a degree, numerical rounding errors lead to high-frequency contamination of the results, which become increasingly evident with large values of x. This is visible with the following graph.

1/stiefel2-378.png

References

  • "Procedures Algol en Analyse Numerique", Editions du CNRS, 1967

Example 1

#include <codecogs/maths/regression/stiefel.h>

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

#define PI  3.1415
#define N   12

int main() 
{
    // Declare and initialize two arrays to hold the coordinates of the initial data points
    double x[N], y[N];

    // Generate the points
    double xx = PI, step = 4 * PI / (N - 1);
    for (int i = 0; i < N; ++i, xx += step) {
        x[i] = xx;
        y[i] = sin(2 * xx) / xx;
    }

    // Initialize the Stiefel-Remes approximation routine with known data points
    Maths::Regression::Stiefel A(N, x, y, 7);

    // Interrogate Stiefel polynomial to find approximated values
    int N_out = 20;
    xx = PI, step = (3 * PI) / (N_out - 1);
    for (int i = 0; i < N_out; ++i, xx += step)
        cout << "x = " << setw(7) << xx << "  y = " << setw(13) << A.getValue(xx) << endl;
    return 0;
}

Output:

x =  3.1415  y =       0.11495
x = 3.63753  y =      0.154609
x = 4.13355  y =     0.0875854
x = 4.62958  y =    0.00476885
x = 5.12561  y =    -0.0516197
x = 5.62163  y =    -0.0705339
x = 6.11766  y =     -0.058554
x = 6.61368  y =    -0.0298593
x = 7.10971  y =   0.000714345
x = 7.60574  y =     0.0219604
x = 8.10176  y =     0.0283645
x = 8.59779  y =     0.0203744
x = 9.09382  y =    0.00330991
x = 9.58984  y =    -0.0146569
x = 10.0859  y =    -0.0250193
x = 10.5819  y =    -0.0215192
x = 11.0779  y =   -0.00236588
x = 11.5739  y =      0.028343
x =   12.07  y =     0.0604278
x =  12.566  y =      0.079094
Blank

Vince Cole (August 2005)

GPL Licence — free for non commercial use. See Licence details.

Members of Stiefel

CLASS METHOD

Stiefel

Initializes the necessary data for following evaluations of the polynomial.

Parameters

degree
The order of the regression
y
The y-coordinates of the points
x
The x-coordinates of the points
n
The number of points

CLASS METHOD

Stiefel

Class destructor

CLASS METHOD

getValue

Returns the approximated ordinate at the given abscissa.

Parameters

x
The x-coordinate of the position of interest

CLASS METHOD

Stiefel_once

This function implements the Forsythe class for one off calculations, thereby avoid the need to instantiate the Forsythe class yourself.

Example 1

The following graph performs a regression on the following values, using a 3rd order polynomial:

x = 1  y = 0.22
x = 2  y = 0.04
x = 3  y = -0.13
x = 4  y = -0.17
x = 5  y = -0.04
x = 6  y = 0.09
x = 7  y = 0.11

\graph n=7 x="1 2 3 4 5 6 7" y="0.22 0.04 -0.13 -0.17 -0.04 0.09 0.11" degree=3 a=1:7 .input

Parameters

n
The number of initial points in the arrays x and y
x
The x-coordinates for the initial points
y
The y-coordinates for the initial points
degree
The number of polynomials to use in the approximation
a
The x-coordinate for the output point

Returns

the interpolated y-coordinate that corresponds to a.