⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newton 插值程序.c

📁 本程序的要求是已知几个节点x及节点函数f(x)
💻 C
字号:
#include <stdio.h> 
#define N 10 
#define M 20 
float x[N],f[N],h[N],g[M],xx=0.23; 
int n=0; 
Print();                /*输出函数*/ 
Read() ;                /*读数函数*/ 
Control(int i);        /*控制计算行*/ 
Require();                        /*询问是否还要插值*/ 
Xcalculate(float xx);          /*计算函数精度*/ 
main() 
{      int i,j; 
clrscr(); 
Read(); 
for(i=0;i<n;i++) 
    Control(i); 
Print(); 
Xcalculate(xx); 
Require(); 

} 
Read() 
{ 
int i,m; 
printf("\n  input the num of the node:\n"); 
scanf("%d",&m); 
printf("    input x,f:\n"); 
for(i=0;i<m;i++) 
    { scanf("%f%f",&x[n],&f[n]); 
      n++; 
    } 
} 
Require() 
{ 
int i,m; 
printf("      if you want insert another node\n"); 
printf("    input the num of the node,otherwise input '0':\n"); 
scanf("%d",&m); 
while(m)                  /*由m控制输入的个数,m=0,跳出!*/ 
{ 
    printf(" input the node x,f\n"); 
    for(i=0;i<m;i++) 
      {scanf("%f%f",&x[n],&f[n]); 
      n++; 
      } 
    for(i=n-m;i<n;i++) 
      Control(i); 
    Print(); 
    Xcalculate(xx); 
    printf("    if you want insert another node \n"); 
    printf(" input the num of the node,otherwise input '0':\n"); 
    scanf("%d",&m); 
} 
} 
/*Xcalculate(float xx) 
{ float new=0.0,tem=1.0; 
float Rx=1.0; 
int i,j,k; 
for(i=1;i<n;i++) 
  { j=i; 
    for(k=1;k<=j;k++) 
    tem*=xx-x[i-j]; 
    new+=g[i*(i+1)/2+j]*tem; 
  } 
Rx=tem*(xx-x); 
for(j=1;j<=i+1;j++) 
    Rx/=j; 
      printf(" f(%f) =  %f \n" ,xx,new); 
      printf(" Rx(%f)= %ef \n" ,xx,Rx); 
}                      */ 
Control(int i) 
{ 
int j,l=0; 
float temp1,temp2; 
if(i==0) 
    g[l]=h=f; 
else if(i==1) 
  { temp1=g[0]; 
    g[0]=f; 
    g[1]=(g[0]-temp1)/(x[1]-x[0]); 
    h[1]=g[1]; 
  } 
else    { temp1=g[0]; 
    g[0]=f; 
    for(j=1;j<i;j++) 
        { temp2=g[j]; 
  g[j]=(g[j-1]-temp1)/(x-x[i-j]); 
  temp1=temp2; 
        } 
    g[j]=h=(g[j-1]-temp1)/(x-x[i-j]); 
  } 
} 
Xcalculate(float xx) 
{ int i,j; 
float new=0.0,tem=1.0; 
float Rx=1.0; 
for(i=1;i<n;i++) 
    { for(j=1;j<=i;j++) 
  tem*=(xx-x[i-j]); 
      new+=(g*tem); 
    } 
Rx= tem*(xx-x); 
for(i=1;i<=n+1;i++) 
  Rx/=i; 
printf("  f( %f)=%f\n",xx,new); 
printf("  Rx(%f)=%ef\n",xx,Rx); 
} 
Print() 
{ 
int i=0,j; 
printf("  %f\n",h); 
for(i=1;i<n-1;i++) 
  {printf("  %f",f); 
  for(j=0;j<i-1;j++) 
    printf("          "); 
  printf("  %f",h); 
  printf("\n"); 
  } 
for(i=0;i<n;i++) 
      printf("  %f",g); 
printf("\n"); 
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -