zoukankan      html  css  js  c++  java
  • 牛顿插值多项式

     1 //题目要求:用牛顿差商公式进行插值  
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstring>
     5 using namespace std;
     6 #define numofx 20                //定义数据量的大小 
     7 struct data{
     8     double x;
     9     double fx;                   //定义进行运算的点(x,fx) 
    10 }pointdata[numofx];
    11 
    12 //求差商f(x0,x1,x2...xn)的公式
    13 long double chashang(int n){     //计算f[x0,x1,x2...xn]
    14     if(n<=0){
    15         return 0;
    16     }
    17     
    18     long double result=0;
    19     for(int i=0;i<=n;i++){
    20         //计算这0~n的计算值的和 
    21         long double resproduct=1;
    22         for(int j=0;j<=n;j++){
    23             if(j!=i){
    24                 resproduct*=(pointdata[i].x-pointdata[j].x);                
    25             }              
    26         } 
    27         long double temp=pointdata[i].fx/resproduct;
    28         result+=temp;
    29     }
    30     return result;
    31 } 
    32                        
    33 int main(){
    34     int numofpoint; 
    35     cout<<"将要输入的插值点的个数为:";
    36     cin>>numofpoint;
    37     cout<<"请依次输入x,fx的值:
    ";    
    38     for(int i=0;i<numofpoint;i++){
    39         cin>>pointdata[i].x>>pointdata[i].fx;
    40     }
    41     
    42     //打印输出牛顿差商公式 ,未简约 
    43     cout<<"由以上插值点计算出的牛顿插值多项式为:
    "; 
    44     cout<<"f(x)="<<pointdata[0].fx;
    45     for(int i=1;i<numofpoint;i++){
    46         long double temp4=chashang(i);
    47         if(temp4>=0){
    48             cout<<"+"<<temp4<<"*";
    49         }
    50         else{
    51             cout<<temp4<<"*";
    52         }
    53         
    54         for(int j=0;j<i;j++){
    55             cout<<"(x-"<<pointdata[j].x<<")";
    56         }
    57     } 
    58     
    59     //计算某个x的牛顿插值多项式近似值 
    60     double x;
    61     cout<<"
    
    请输入所求变量x的值:";
    62     cin>>x;
    63     cout<<"牛顿插值多项式近似结果为:";
    64      
    65     long double res=pointdata[0].fx;        //牛顿差商公式的f(x0)项                                            
    66     for(int i=0;i<numofpoint-1;i++){        //牛顿插值多项式项f[x0,x1...xn](x-x0)(x-x1)...(x-xn) n>=1 
    67 
    68         long double temp=1;
    69         for(int j=0;j<=i;j++){
    70             temp=temp*(x-pointdata[j].x); 
    71         }
    72         
    73         res=res+chashang(i+1)*temp;
    74     }
    75     cout<<res<<endl;
    76 } 

    拉格朗日插值的程序:(这个程序中运用了文件输入的方式读入数据,当然上面的牛顿插值程序也可以运用这个方法,减轻输入数据时的繁琐;同时也可以将生成的牛顿插值公式通过文件输出出来,便于复制粘贴)

     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 
     5 class lagrange{
     6 
     7     private:
     8         int i,j,n;
     9         double mult,sum,z;
    10         double *x,*y;
    11         
    12     public:
    13         void interpolation(){
    14             ifstream fin("a.txt");
    15             fin>>n;
    16             x=new double[n];
    17             y=new double[n];
    18             for(i=0;i<n;i++){
    19                 fin>>x[i]>>y[i];
    20             }
    21             fin.close();
    22             cout<<"
    输入需要插值的点:";
    23             cin>>z;
    24             sum=0.0;
    25             
    26             for(i=0;i<n;i++){
    27                 mult=1.0;
    28                 for(j=0;j<n;j++){
    29                     if(j!=i){
    30                         mult*=(z-x[j])/(x[i]-x[j]);
    31                     } 
    32                 }
    33                 sum+=mult*y[i];
    34             } 
    35             cout<<"
    插补的值="<<sum<<endl; 
    36                         
    37         }
    38         
    39         ~lagrange(){
    40             delete[] x,y;
    41         }
    42     
    43 };
    44 
    45 int main(){
    46     lagrange interp;
    47     interp.interpolation();
    48 } 
  • 相关阅读:
    读书笔记—CLR via C#线程25-26章节
    算法回顾--N皇后问题简单回顾
    编程拾趣--集合子集问题
    读书笔记—CLR via C#异常和状态管理
    读书笔记—CLR via C#字符串及文本
    设计模式---抽象工厂
    读书笔记—CLR via C#反射
    读书笔记—CLR via C#委托和attribute
    C#编程实践—EventBroker简单实现
    Linux平台屏幕录像工具RecordMyDesktop
  • 原文地址:https://www.cnblogs.com/liugl7/p/4888692.html
Copyright © 2011-2022 走看看