niudun.c

来自「计算方法c牛顿插值函数」· C语言 代码 · 共 57 行

C
57
字号
#include <stdio.h>#include <stdlib.h>#include <malloc.h>double NEWT(int n,double *x,double *y,double t){    int i,j;    double *s,p;    s=(double *)calloc(n+1,sizeof(double));    if(s==NULL)        exit(1);    for(i=1;i<=n;i++)        s[i]=y[i];    for(j=1;j<=n-1;j++)        for(i=n;i>=j+1;i--)            s[i]=(s[i]-s[i-1])/(x[i]-x[i-1]);    p=y[n];    for(i=n-1;i>=1;i--)        p=p*(t-x[i])+s[i];    free(s);    return (p);}double NEWT(int,double *,double *,double);main(){   int n;  double t,*x,*y;  n=6;  t=75.5;  //printf("这是牛顿插值算法,请输入要计算的x值:");  //scanf("%e",&t);   x=(double*)calloc(n+1,sizeof(double));  if(x==NULL)      exit(1);  y=(double*)calloc(n+1,sizeof(double));  if(y==NULL)      exit(1);  x[0]=75;  x[1]=76;  x[2]=77;  x[3]=78;  x[4]=79;  x[5]=80;  y[0]=2.768;  y[1]=2.833;  y[2]=2.903;  y[3]=2.979;  y[4]=3.062;  y[5]=3.153;  printf("使用牛顿插值算法:\n");  printf("当x=75.5时,计算结果为: %f \n",NEWT(n,x,y,t));  t=78.3;  printf("当x=78.3时,计算结果为: %f \n",NEWT(n,x,y,t));  free(x);  free(y);  return 0;}

⌨️ 快捷键说明

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