Newton Interpolation

The main function takes as input two sequences of numbers and one single number. The first sequence represents [x1​,x2​,…,xi​] of some points, and the second sequence represents [y1​,y2​,…,yi​]. You need to find the Newton interpolating polynomial to pass through the points. The single number is the xn​ of an additional point to be interpolated.

First, implement your Newton Interpolation algorithm based on [x1​,x2​,…,xi​] and [y1​,y2​,…,yi​]. Then, use it to compute the value yn​at xn​.

Example

Input: 0.0 0.1 0.2 0.3 0.4 0.5 
       1.0 0.995 0.98 0.955 0.921 0.878
       0.7
Output: 0.748

Note

1. The output retains three decimal places (rounded)!

2. [x1​,x2​,…,xi​] and [y1​,y2​,…,yi​] are distinct and of equal length.

3. The [x1​,x2​,…,xi​] of points are increased monotonically and have the equal interval, i.e. xk​=x0​+kh,h∈{1,2,…}.

Reference Code

#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cmath>

using namespace std;

// You can add additional standard libraries if necessary.
// Implement the Newton interpolation!
class Newton
{
public:
    Newton(vector<double> x, vector<double> y,int l): X(x),Y(y),len(l) {}

    double newton_interpolation(double xn)
    {
        double yn;

        double h=X[1]-X[0];

        double *dt=getDividedDifferenceTable();

        yn=Y[0];

        double mult;
        double k_;

        for(int k=1;k<len;k++)
        {
            mult=1;
            k_=1;
            for(int i=0;i<k;i++)
            {
                mult*=xn-X[i];
                k_*=i+1;
            }
            yn+=dt[k]/(k_*pow(h,k))*mult;
        }

        return yn;
    }

    double *getDividedDifferenceTable()
    {
        double* dt=new double[len];
        for(int i=0;i<len;i++)
        {
            dt[i]=Y[i];
        }
        double tmp1;
        double tmp2;
        for(int k=0;k<len-1;k++)
        {
            tmp1=dt[k];
            tmp2=dt[k+1];
            for(int j=k+1;j<len;j++)
            {
                dt[j]=tmp2-tmp1;
                tmp1=tmp2;
                tmp2=dt[j+1];
            }
        }
        return dt;
    }

private:
    vector<double> X,Y;
    int len;
};


// Test your implementation.
int main()
{
    //  Input preprocessing.
    string str;
    getline(cin, str);
    stringstream xstr(str);
    getline(cin, str);
    stringstream ystr(str);

    // X and Y are two vectors of equal length to be traversed.
    vector<double> X, Y;
    double a;
    while (xstr >> a)
    {
        X.push_back(a);
    }
    while (ystr >> a)
    {
        Y.push_back(a);
    }
    // interp_x is the point to be interpolated.
    double interp_x;
    cin >> interp_x;

    // Do Newton interpolation for interp_x using X and Y, and print your results
    // Note: The result retains three decimal places (rounded)!
    int len=X.size();

    Newton N(X,Y,len);

    cout<<setiosflags(ios::fixed)<<setprecision(3)<<N.newton_interpolation(interp_x);

    // End
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值