#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
const short N = 6;
double Lagrang(double X[N], double Fx[N], double _x)
{
double Wx = 1.0;
for (short i = 0; i < N; ++i)
Wx *= (_x - X[i]);
double result = 0.0;
for (short i = 0; i < N; ++i)
{
double dWx = 1.0;
for(short j = 0; j < N; ++j)
if (i!= j) dWx *= X[i] - X[j];
result += (Wx * Fx[i])/((_x - X[i]) * dWx);
}
return result;
}
void test()
{
double X[N] = {0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
double Fx[N];
for (short i = 0; i < N; ++i)
Fx[i] = cos(X[i]);
std::cout << std::endl << "Cos_Lagr\t" << Lagrang(X, Fx, 0.55) << "\t" << cos(0.55);
for (short i = 0; i < N; ++i)
Fx[i] = sin(X[i]);
std::cout << std::endl << "Sin_Lagr\t" << Lagrang(X, Fx, 0.55) << "\t" << sin(0.55);
for (short i = 0; i < N; ++i)
Fx[i] = tan(X[i]);
std::cout << std::endl << "Tan_Lagr\t" << Lagrang(X, Fx, 0.55) << "\t" << tan(0.55);
}
int main(int argc, char** argv)
{
double X[6] = {0.41, 0.46, 0.52, 0.60, 0.65, 0.72};
double Fx[6] = {2.57418, 2.32513, 2.09336, 1.86203, 1.74926, 1.62098};
std::cout << std::fixed << std::setw(20) << std::setprecision(15) << std::endl
<< "Lagrang\t" << Lagrang(X, Fx, 0.537);
test();
std::system("pause");
return 0;
}
Вывод программы
Lagrang 2.037859651847294
Cos_Lagr 0.852524517903288 0.852524522059506
Sin_Lagr 0.522687226382460 0.522687228930659
Tan_Lagr 0.613108512374107 0.613105213288136
Тесты для основных тригонометрических функций подтверждают правильность работы алгоритма.