📄 lagrange.c
字号:
/*********************************************/
/* */
/* langrange插值 */
/* */
/*********************************************/
#include<math.h>
#include<stdio.h>
#include<malloc.h>
#define type "%lf" /*-定义数据类型-*/
typedef double Dtype;
Dtype f(Dtype x)
{
return sqrt(x);
}
void main()
{
int i,j,n;
Dtype *x,*y,Ln,l,xv;
printf("Please input n (n>0)\n");
scanf("%d",&n);
x=(Dtype*)malloc(sizeof(Dtype)*(n+1));
y=(Dtype*)malloc(sizeof(Dtype)*(n+1));
printf("Please input array x[0..%d]:\n",n);
for(i=0;i<n+1;i++)
{
scanf(type,&x[i]);
y[i]=f(x[i]);/*-由输入的xi值计算f(xi)的值-*/
}
/*-由lagrange插值多项式计算Ln(x)-*/
printf("Please input x:");
scanf(type,&xv);
Ln=0;
for(i=0;i<n+1;i++)
{
l=1;
for(j=0;j<n+1;j++)
if(j!=i) l*=(xv-x[j])/(x[i]-x[j]);
Ln+=l*y[i];
}
printf("Ln("type")="type,xv,Ln);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -