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

📄 lagelangri.c

📁 实现 了 数值分析 课程 中得 几个 算法
💻 C
字号:
/* 这份源代码文件已被未注册的SourceFormatX格式化过 */
/* 如果您想不再添加此类信息,请您注册这个共享软件  */
/* 更多相关信息请访问网站: http://cn.textrush.com  */


/////////////////////////////////////////////////////////////
//         Purpose:(x_i,y_i)的拉格朗日插值多项式               //
/////////////////////////////////////////////////////////////

/**
 *The result :
 * Input n value: 2
 *Now input the (x_i,y_i),i=0,...,2:
 *11 0.190809 12 0.207912 13 0.224951
 *Now input the x value: 11.5
 *newton(11.500000)=0.199369
 *请按任意键继续. . .
 */

#include <stdio.h>
#define MAX_N 20                    //定义(x_i,y_i)的最大维数

typedef struct tagPOINT  //点的结构
{
  double x;
  double y;
} POINT;

int main()
{
  int n;
  int i, j;
  POINT points[MAX_N + 1];
  double diff[MAX_N + 1];
  double x, temp, fx = 0;
  printf("\n Input n value: "); //输入被插值点的数目
  scanf("%d", &n);
  if (n > MAX_N)
  {
    printf("The input n is larger than MAX_N, please redefine the MAX_N.\n");
    return 1;
  }
  if (n <= 0)
  {
    printf("Please input a number between 1 and %d.\n", MAX_N);
    return 1;
  }
  //输入被插值点(x_i,y_i)
  printf("Now input the (x_i,y_i),i=0,...,%d:\n", n);
  for (i = 0; i <= n; i++)
    scanf("%lf%lf", &points[i].x, &points[i].y);
  printf("Now input the x value: "); //输入计算牛顿插值多项式的x值
  scanf("%lf", &x);
  //for(i=0; i<=n; i++)    diff[i]=points[i].y;
  for (i = 0; i <= n; i++)
  {
    temp = 1;
    for (j = 0; j <= n; j++)
    {
      if (i == j)
        continue;
      else

      {
        temp = temp *(x - points[j].x) / (points[i].x - points[j].x);
      }
    }
    fx = fx + temp * points[i].y;

  }



  printf("newton(%f)=%f\n", x, fx); //输出

  system("pause");
}

⌨️ 快捷键说明

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