注册地址 登录
数学建模社区-数学中国 返回首页

chalrieq的个人空间 http://www.madio.net/?195740 [收藏] [复制] [分享] [RSS]

日志

牛顿和拉格朗日插值算法

已有 250 次阅读2011-1-27 16:35 |个人分类:Files

 #include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
typedef strUCt data
{
 float x;
 float y;
}Data;//
变量x和函数值y的结构
Data d[20];//
最多二十组数据
float f(int s,int t)//
牛顿插值法,用以返回插商
{
 if(t==s+1)
  return (d[t].y-d[s].y)/(d[t].x-d[s].x);
 else
        return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);
}
float Newton(float x,int count)
{
 int n;
 while(1)
 {
     cout<<"
请输入n(n次插值):";//获得插值次数
  cin>>n;
  if(n<=count-1)//
插值次数不得大于count1
   break;
  else
   system("cls");
 }
//
初始化tyyt
 float t=1.0;
 float y=d[0].y;
 float yt=0.0;
//
计算y
 for(int j=1;j<=n;j++)
 {
  t=(x-d[j-1].x)*t;
  yt=f(0,j)*t;
  //cout<<f(0,j)<<endl;
  y=y+yt;
 }
 return y;
}
float lagrange(float x,int count)
{
 float y=0.0;
 for(int k=0;k<count;k++)//
这儿默认为count1次插值
 {
  float p=1.0;//
初始化p
  for(int j=0;j<count;j++)
  {//
计算p的值
   if(k==j)continue;//
判定是否为同一个数
   p=p*(x-d[j].x)/(d[k].x-d[j].x);
  }
  y=y+p*d[k].y;//
求和
 }
 return y;//
返回y的值
}
void main()
{
 float x,y;
 int count;
 while(1)
 {
  cout<<"
请输入x[i],y[i]的组数,不得超过20:";//要求用户输入数据组数
  cin>>count;
  if(count<=20)
   break;//
检查输入的是否合法
  system("cls");
 }
//
获得各组数据
 for(int i=0;i<count;i++)
 {
      cout<<"
请输入第"<<i+1<<"x的值:";
      cin>>d[i].x;
      cout<<"
请输入第"<<i+1<<"y的值:";
      cin>>d[i].y;
   system("cls");
 }
 cout<<"
请输入x的值:";//获得变量x的值
 cin>>x;
 while(1)
 {
  int choice=3;
  cout<<"
请您选择使用哪种插值法计算:"<<endl;
  cout<<"              (0):
退出"<<endl;

  cout<<"              (1):Lagrange"<<endl;
  cout<<"              (2):Newton"<<endl;
  cout<<"
输入你的选择:";
  cin>>choice;//
取得用户的选择项
  if(choice==2)
  {
   cout<<"
你选择了牛顿插值计算方法,其结果为:";
   y=Newton(x,count);break;//
调用相应的处理函数
  }
        if(choice==1)
  {
   cout<<"
你选择了拉格朗日插值计算方法,其结果为:";
   y=lagrange(x,count);break;//
调用相应的处理函数
  }
  if(choice==0)
   break;
  system("cls");
  cout<<"
输入错误!!!!"<<endl;
 }
 cout<<x<<"    ,        "<<y<<endl;//
输出最终结果

}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2025-12-8 08:09 , Processed in 2.159665 second(s), 28 queries .

回顶部