我更新了代码。我想要做的是将每个拉格朗日的系数值保存在指针 d 中。(例如,对于 L1(x) d[0] 将是 "x-x2/x1-x2" ,d 1将是 (x-x2/ x1-x2)*(x-x3/x1-x3) 等。
我的问题是
1)如何初始化 d(我做了 d[0]=(zx[i])/(x[k]-x[i]) 但我认为“d[0]”不正确
2)如何初始化L_coeff。(我正在使用 L_coeff=new double[0] 但不确定它是否正确。
练习是:使用 5 个点(x = -1、-0.5、0、0.5 和 1)求 y(x)=cos(π x), x ∈−1,1 的拉格朗日多项式逼近。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const double pi=3.14159265358979323846264338327950288;
// my function
double f(double x){
return (cos(pi*x));
}
//function to compute lagrange polynomial
double lagrange_polynomial(int N,double *x){
//N = degree of polynomial
double z,y;
double *L_coeff=new double [0];//L_coefficients of every Lagrange L_coefficient
double *d;//hold the polynomials values for every Lagrange coefficient
int k,i;
//computations for finding lagrange polynomial
//double sum=0;
for (k=0;k<N+1;k++){
for ( i=0;i<N+1;i++){
if (i==0) continue;
d[0]=(z-x[i])/(x[k]-x[i]);//initialization
if (i==k) L_coeff[k]=1.0;
else if (i!=k){
L_coeff[k]*=d[i];
}
}
cout <<"\nL("<<k<<") = "<<d[i]<<"\t\t\tf(x)= "<<f(x[k])<<endl;
}
}
int main()
{
double deg,result;
double *x;
cout <<"Give the degree of the polynomial :"<<endl;
cin >>deg;
for (int i=0;i<deg+1;i++){
cout <<"\nGive the points of interpolation : "<<endl;
cin >> x[i];
}
cout <<"\nThe Lagrange L_coefficients are: "<<endl;
result=lagrange_polynomial(deg,x);
return 0;
}