VEC polyRoots(double x,VEC &A,int maxiter, double eps){ int n = A.leng()-1; double err,f,df,B_i,C_i; VEC Z(n); int k; VEC B(n+1); VEC C(n+1); while(n>=1){ err = 1+eps; k = 0; while((err>=eps)&&(k<maxiter)){ B[n-1] = A[n]; C[n-1] = B[n-1]; for(int j=n-2;j>=0;j--) B[j] = A[j+1]+x*B[j+1]; for(int j=n-3;j>=0;j--) C[j] = B[j+1]+x*C[j+1]; B_i = A[0]+x*B[0]; C_i = B[0]+x*C[0]; f = B_i; df = C_i; x = x - f/df; err = fabs(f); k++; } Z[n-1] = x; for(int j=0;j<n;j++) A[j] = B[j]; x = Z[n-1]; n--; } return Z; }
double norm2(VEC x){ //compute 2-norm int n=x.leng(); double norm=0; for(int i=0;i<n;i++){ norm += pow(x[i], 2); } norm = pow(norm, 0.5); return norm; }
double Lagrange(double x,VEC &XDATA,VEC &YDATA){ //use non-recursive Neville’s algorithm to calculate Lagrange Interpolation int n=XDATA.leng(); //get the length of the vector XDATA VEC NS(n); for(int i=0;i<n;i++){ NS[i] = YDATA[i]; } for(int k=1;k<n;k++){ for(int j=0;j<n-k;j++){ NS[j] = ((x-XDATA[j])*NS[j+1]-(x-XDATA[k+j])*NS[j])/(XDATA[j+k]-XDATA[j]); } } return NS[0]; }