拉格朗日插值用c语言怎么编程?各位高手帮帮忙啦
3个回答
展开全部
#include<stdio.h>
#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=0;i<n;i++)
scanf("%lf,%lf",&pt[i].x,&pt[i].y);
printf("\n");
printf("输入插值点x的值:\n");
scanf("%lf",&x);
printf("\n");
for(i=0;i<n;i++)
{
for(j=0,temp=1;j<n;j++)
{
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);
}
#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=0;i<n;i++)
scanf("%lf,%lf",&pt[i].x,&pt[i].y);
printf("\n");
printf("输入插值点x的值:\n");
scanf("%lf",&x);
printf("\n");
for(i=0;i<n;i++)
{
for(j=0,temp=1;j<n;j++)
{
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);
}
2011-04-15
展开全部
下,用的是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=0;k<=n-1;k++)
{
cout<<"请输入X"<<k<<"的值:";
cin>>x[k];
cout<<"请输入Y"<<k<<"的值:";
cin>>y[k];
}
system("cls");
cout<<"则Xi与Yi表格如下:"<<endl;
cout<<"Xi"<<" ";for(k=0;k<=n-1;k++)cout<<setiosflags(ios::left)<<setw(10)<<x[k];
cout<<endl;
cout<<"Yi"<<" ";for(k=0;k<=n-1;k++)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=0;k<=n-1;k++)
{
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=0;k<=n-1;k++)
{
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();
}
另外,站长团上有产品团购,便宜有保证
#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=0;k<=n-1;k++)
{
cout<<"请输入X"<<k<<"的值:";
cin>>x[k];
cout<<"请输入Y"<<k<<"的值:";
cin>>y[k];
}
system("cls");
cout<<"则Xi与Yi表格如下:"<<endl;
cout<<"Xi"<<" ";for(k=0;k<=n-1;k++)cout<<setiosflags(ios::left)<<setw(10)<<x[k];
cout<<endl;
cout<<"Yi"<<" ";for(k=0;k<=n-1;k++)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=0;k<=n-1;k++)
{
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=0;k<=n-1;k++)
{
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();
}
另外,站长团上有产品团购,便宜有保证
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
int main()
{
double x;
double xy[test][2]={};
double Lag(double x,double xy[test][test]);
printf("需要插入的项x:");
scanf("%lf",&x);
printf("结果近似为:%lf",Lag(x,xy) );
return 0;
}
double Lag(double x,double xy[test][test])
{
double lage[test];
double temp1=1.0,temp2=1.0;
double retnum=0;
int i,j;
for(i=0;i<test;i++)
{ for(j=0;j<test;j++)
{ if(j!=i)
temp1*=x-xy[j][0];
temp2*=x[i][0]-x[j][0];
}
lage[i]=temp1/temp2;
}
for(i=0;i<test;i++)
retnum+=xy[i][1]*lag[i];
return retnum;
}
int main()
{
double x;
double xy[test][2]={};
double Lag(double x,double xy[test][test]);
printf("需要插入的项x:");
scanf("%lf",&x);
printf("结果近似为:%lf",Lag(x,xy) );
return 0;
}
double Lag(double x,double xy[test][test])
{
double lage[test];
double temp1=1.0,temp2=1.0;
double retnum=0;
int i,j;
for(i=0;i<test;i++)
{ for(j=0;j<test;j++)
{ if(j!=i)
temp1*=x-xy[j][0];
temp2*=x[i][0]-x[j][0];
}
lage[i]=temp1/temp2;
}
for(i=0;i<test;i++)
retnum+=xy[i][1]*lag[i];
return retnum;
}
追问
在win-TC和VC6.0都运行不了啊?
追答
是啊 double xy[test][2]={};还没赋值呢 把初值赋值上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询