Orthogonal
Approximates an arbitrary function using orthogonal polynomials.
You're viewing an older version of this page (#1007). View the current version.
Interface
#include <codecogs/maths/approximation/regression/orthogonal.h>
using namespace Maths::Approximation::Regression;
Overview
This class approximates an arbitrary discrete function by least squares fitting orthogonal polynomials. In mathematics, two polynomials f and g are orthogonal to each other with respect to a nonnegative <em> weight function </em> w precisely if
In other words, if polynomials are treated as vectors and the inner product of two polynomials and
is defined as
then the orthogonal polynomials are simply orthogonal vectors in this inner product space. The algorithm uses this class of polynomials to achieve the best curve fitting by minimization in terms of least squares.
Below you will find the regression graph for a set of points obtained by evaluating the function , displayed in light blue, at particular abscissas. The regression polynomial, displayed in red, has been calculated using this class. The root mean squared error is also displayed.

As a general rule, the degree parameter should always be strictly less than the the total number of sample points.
References
- Jean-Pierre Moreau's Home Page, http://perso.wanadoo.fr/jean-pierre.moreau/
- Tuan Dang Trong, "Numath Library" in Fortran 77
Example 1
The following example displays 10 approximated values (you may change this amount through the N_out variable) for the given function with abscissas equally spaced in the
interval. The absolute approximation errors are also displayed. The X and Y coordinate arrays are initialized by evaluating this function for N = 13 points equally spaced in the domain from
to
. The degree parameter (the number of orthogonal polynomials to use) is equal to 12 in this example.
#include <codecogs/maths/regression/orthogonal.h>
#include "orthogonal.h"
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define N 13
int main()
{
// Declare and initialize two arrays to hold the coordinates of the initial data points
double x[N], y[N];
double xx = PI, step = 4 * PI / (N - 1);
for (int i = 0; i < N; i++, xx += step) {
x[i] = xx;
y[i] = sin(xx) + xx;
}
// Initialize the regression approximation routine with known data points
Maths::Regression::Orthogonal A(N, x, y, N - 1);
// Interrogate the regression function to find approximated values
int N_out = 10;
xx = PI, step = (3 * PI) / (N_out - 1);
for (int i = 0; i < N_out; ++i, xx += step) {
cout << "x = " << setw(7) << xx << " y = ";
cout << setw(7) << A.getValue(xx);
cout << setw(10) << "error = " << fabs(sin(xx) + xx - A.getValue(xx)) << endl;
}
return 0;
}Output:
x = 3.14159 y = 3.14159 error = 1.33227e-15
x = 4.18879 y = 3.32276 error = 1.77636e-15
x = 5.23599 y = 4.36996 error = 1.86517e-14
x = 6.28319 y = 6.28319 error = 7.19425e-14
x = 7.33038 y = 8.19641 error = 1.1191e-13
x = 8.37758 y = 9.24361 error = 2.41585e-13
x = 9.42478 y = 9.42478 error = 2.91323e-13
x = 10.472 y = 9.60595 error = 2.62901e-13
x = 11.5192 y = 10.6531 error = 2.2915e-13
x = 12.5664 y = 12.5664 error = 1.04805e-13Members of Orthogonal
CLASS METHOD
Orthogonal
Initializes the necessary data for following evaluations of the polynomial.
Parameters
CLASS METHOD
Orthogonal
Class destructor
CLASS METHOD
getValue
Returns the approximated ordinate at the given abscissa.
Parameters
STATIC CLASS METHOD
prod
CLASS METHOD
Orthogonal_once
This function implements the Orthogonal class for one off calculations, thereby avoid the need to instantiate the Orthogonal class yourself.
Example 1
The following graphs are constructed from the interpolation of the following values, using a 3rd order polynomial and then 8th order polynomial, respectively:
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 \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=6 a=1:7 .input