📄 lagrange.cpp
字号:
//Lagrange's interpolation formula
#include<stdio.h>
#include<conio.h>
main()
{
float x[20],y[20],p1,p2,s,xv;
int n,i,j;
printf("How many sets of values do you want to give:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter x%d,y%d:",i+1,i+1);
scanf("%f%f",&x[i],&y[i]);
}
label:
printf("\nEnter the value of x:");
scanf("%f",&xv);
s=0;
for(j=0;j<n;j++)
{
p1=y[j];
p2=1;
for(i=0;i<n;i++)
{
if(i!=j)
{
p1=p1*(xv-x[i]);
p2=p2*(x[j]-x[i]);
}
}
s=s+(p1/p2);
}
printf("The value of y is:%f\n",s);
printf("\nDo you want to continue(y/n):");
if('y'==getch())
goto label;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -