C++ 简化多项式
我想打印出拉格朗日插值形式C++ 简化多项式,c++,interpolation,polynomials,C++,Interpolation,Polynomials,我想打印出拉格朗日插值形式 double x0=x[0]; 双x1=x[1]; 双x2=x[2]; 双x3=x[3]; 双z0=z[0]; 双z1=z[1]; 双z2=z[2]; 双z3=z[3]; 如果(大小==2) { 试试这个,看看是否对你有好处: #include <iostream> #include <vector> double calc_coeff(double yi, std::vector<double> x, size_t i, si
double x0=x[0];
双x1=x[1];
双x2=x[2];
双x3=x[3];
双z0=z[0];
双z1=z[1];
双z2=z[2];
双z3=z[3];
如果(大小==2)
{
试试这个,看看是否对你有好处:
#include <iostream>
#include <vector>
double calc_coeff(double yi, std::vector<double> x, size_t i, size_t n)
{
double den_prod = 1;
for(size_t loop = 0; loop < n; loop++)
{
if( loop != i)
{
den_prod *= (x[i] - x[loop]);
}
}
double final_prod = yi/den_prod;
return final_prod;
}
double lagrange_interpolation_sum(std::vector<double> x, std::vector<double> y)
{
std::vector<double> den;
size_t n = x.size();
for(size_t i = 0; i < n; i++)
{
double coeff = calc_coeff(y[i], x, i, n);
std::cout<<coeff<<" * ";
for(size_t j = 0; j < n; j++)
{
if(j != i)
{
if(x[j] >= 0)
{
std::cout<<"(x - "<<x[j] << ") ";
}
else
{
std::cout<<"(x + "<<-x[j] << ") ";
}
}
}
std::cout<<std::endl;
}
return 0.0;
}
int main()
{
std::vector<double> x = {4.5, 6.7, 7.8, 8.7, 6.5};
std::vector<double> y = {1.1, 2.2, 3.3, 4.4, 5.5};
lagrange_interpolation_sum(x,y);
return 0;
}
#包括
#包括
双计算系数(双yi,标准::向量x,大小i,大小n)
{
双齿针=1;
用于(大小\u t循环=0;循环 std::coutIt如果你真的展示了这个方程的话会很有帮助。@cigen你需要什么方程?我不明白?你是指拉格朗日多项式形式的公式吗?@JohnHan紧凑形式是一个乘积的总和,它会转化为两个嵌套的循环。你可以添加一个方程的图像,如果你想要的话,就像我在中编辑的那样公式,它们的模式非常明显。从一个方便的数组中提取多元系数到离散变量是一种倒退。它们应该保持原来的数组,算法应该以简单的方式直接实现。拉格兰奇插值形式的公式可以直接转换为C++代码,也就是说。
#include <iostream>
#include <vector>
double calc_coeff(double yi, std::vector<double> x, size_t i, size_t n)
{
double den_prod = 1;
for(size_t loop = 0; loop < n; loop++)
{
if( loop != i)
{
den_prod *= (x[i] - x[loop]);
}
}
double final_prod = yi/den_prod;
return final_prod;
}
double lagrange_interpolation_sum(std::vector<double> x, std::vector<double> y)
{
std::vector<double> den;
size_t n = x.size();
for(size_t i = 0; i < n; i++)
{
double coeff = calc_coeff(y[i], x, i, n);
std::cout<<coeff<<" * ";
for(size_t j = 0; j < n; j++)
{
if(j != i)
{
if(x[j] >= 0)
{
std::cout<<"(x - "<<x[j] << ") ";
}
else
{
std::cout<<"(x + "<<-x[j] << ") ";
}
}
}
std::cout<<std::endl;
}
return 0.0;
}
int main()
{
std::vector<double> x = {4.5, 6.7, 7.8, 8.7, 6.5};
std::vector<double> y = {1.1, 2.2, 3.3, 4.4, 5.5};
lagrange_interpolation_sum(x,y);
return 0;
}