#include<string.h>
#define N 100
typedef struct tag{
double x
double y
}POINT
void main()
{
int i,j,n
double x,temp,Ln=0
POINT pt[N]
printf("请输入你要输入点的个数,,1<=n<=%d:\n",N)
printf("n=")
scanf("%d",&n)
printf("\n")
printf("\n请输入对应的点数\n")
for(i=0i<ni++)
scanf("%lf,%lf",&pt[i].x,&pt[i].y)
printf("\n")
printf("输入插值点x的值:\n")
scanf("%lf",&x)
printf("\n")
for(i=0i<ni++)
{
for(j=0,temp=1j<nj++)
{
if(j!=i)
temp=temp*(x-pt[j].x)/(pt[i].x-pt[j].x)
}
Ln=Ln+temp*pt[i].y
}
printf("输出:\nLn(%lf)=%lf\n",x,Ln)
}
我的程序是牛顿插值和拉格朗日插值合起来,你自己看下,用的是C++#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std
#define N 100
void lagrange()
{
int n,k,m,q=1
float x[N],y[N],xx,yyy1,yyy2,yy1,yy2,yy3
cout<<"请输入X的个数:"
cin>>n
for(k=0k<=n-1k++)
{
cout<<"请输入X"<<k<<"的值:"
cin>>x[k]
cout<<"请输入Y"<<k<<"的值:"
cin>>y[k]
}
system("cls")
cout<<"则Xi与Yi表格如下:"<<endl
cout<<"Xi"<<" "for(k=0k<=n-1k++)cout<<setiosflags(ios::left)<<setw(10)<<x[k]
cout<<endl
cout<<"Yi"<<" "for(k=0k<=n-1k++)cout<<setiosflags(ios::left)<<setw(10)<<y[k]
cout<<endl
while(q)
{
cout<<"请输入所求x的值:"
cin>>xx
while(xx>x[k-1]||xx<x[0])
{
cout<<"输入错误,请重新输入:"
cin>>xx
}
for(k=0k<=n-1k++)
{
if(xx<x[k])
{
m=k-1
k=n-1
}
}
yyy1=y[m]*((xx-x[m+1])/(x[m]-x[m+1]))+y[m+1]*((xx-x[m])/(x[m+1]-x[m]))
cout<<"则拉格朗日分段线性插值为:"<<yyy1<<endl
for(k=0k<=n-1k++)
{
if(xx<x[k])
{
m=k-1
k=n-1
}
}
if((xx-x[m])>(x[m+1]-xx))m=m+1
else m=m
yy1=y[m-1]*((xx-x[m])*(xx-x[m+1]))/((x[m-1]-x[m])*(x[m-1]-x[m+1]))
yy2=y[m]*((xx-x[m-1])*(xx-x[m+1]))/((x[m]-x[m-1])*(x[m]-x[m+1]))
yy3=y[m+1]*((xx-x[m-1])*(xx-x[m]))/((x[m+1]-x[m-1])*(x[m+1]-x[m]))
yyy2=yy1+yy2+yy3
cout<<"则拉格朗日分段二次插值为:"<<yyy2<<endl
cout<<"是否输入其余要求x的值[是(1),否(0)]:"
cin>>q
}
system("cls")
}
void main()
{
lagrange()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)