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.8780.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);// Endreturn 0;
}

Newton Interpolation相关推荐

  1. 多项式拟合缺点_曲线拟合方法的选择

    曲线拟合方法的选择 目    录 摘要 1 前言 2 1 问题提出 3 2 插值介绍 4 2.1拉格朗日公式求解 4 2.1.1 算法分析 5 2.1.2 程序设计 5 2.1.3 计算结果 8 2. ...

  2. matlab全域基函数,多项式函数插值:全域多项式插值(一)单项式基插值、拉格朗日插值、牛顿插值 [MATLAB]...

    全域多项式插值指的是在整个插值区域内形成一个多项式函数作为插值函数.关于多项式插值的基本知识,见"计算基本理论". 在单项式基插值和牛顿插值形成的表达式中,求该表达式在某一点处的值 ...

  3. 【Matlab】数据插值

    数据插值 插值与拟合的区别: 实现方法:插值要求曲线穿过样本点,而拟合不需要穿过样本点,只要求总体误差最小. 结果形式:插值是分段逼近样本点,没有同一的逼近函数:函数拟合则用一个函数去逼近,有完整的表 ...

  4. python插值程序_计算方法(2)——插值法(附Python程序)

    给定一些数据,生成函数的方式有两种:插值,回归. 插值而得到的函数通过数据点,回归得到的函数不一定通过数据点. 下面给出拉格朗日插值,牛顿插值和Hermite插值的程序, 具体原理可参考课本,不再赘述 ...

  5. 函数插值的python实现——拉格朗日、牛顿插值

    函数插值的python实现--拉格朗日.牛顿插值 1. 拉格朗日(Larange)插值 实现代码 2. 牛顿(Larange)插值 2.1 牛顿插值多项式的基本形式 2.2 牛顿均差插值多项式 (1) ...

  6. Spline interpolation and Savitzki-Golay smoothing

    转自:http://octave.1599824.n4.nabble.com/Spline-interpolation-and-Savitzki-Golay-smoothing-td1675136.h ...

  7. 双线性插值(Bilinear Interpolation)

    应用场景 图像放缩中的处理:从低分辨率图像生成高分辨率图像的过程,用以恢复图像中所丢失的信息:   假设源图像大小为 mxn ,目标图像为 axb.那么两幅图像的边长比分别为:m/a 和 n/b.这个 ...

  8. 什么是牛顿法(Newton methods)?什么是拟牛顿法(Quasi Newton methods)?牛顿法和梯度下降法的区别是什么?

    什么是牛顿法(Newton methods)?什么是拟牛顿法(Quasi Newton methods)?牛顿法和梯度下降法的区别是什么? 牛顿法的最初提出是用来求解方程的根的.对于最优化问题,其极值 ...

  9. 数字图像处理笔记二 - 图片缩放(最近邻插值(Nearest Neighbor interpolation))

    图片缩放的两种常见算法: 最近邻域内插法(Nearest Neighbor interpolation) 双向性内插法(bilinear interpolation) 本文主要讲述最近邻插值(Near ...

  10. 视频插值--Video Frame Interpolation via Adaptive Separable Convolution

    Video Frame Interpolation via Adaptive Separable Convolution ICCV2017 https://github.com/sniklaus/py ...

最新文章

  1. python中rfind函数_Python rfind()方法
  2. bat 执行 java jar包
  3. 002 python准备做题的一些准备
  4. 华南理工网络教育计算机概论,2020年《计算机概论》平时作业华南理工网络教育学院.pdf...
  5. jQuery progression 表单进度
  6. 英文.数字和中文混合的彩色验证码【JSP】
  7. hive排序:distribute by 、sort by 、cluster by 、order by 区别
  8. 实战:基于Node的控制台记事本开发
  9. 968. 监控二叉树(每日一难phase2--day17)
  10. python分段函数图像画法_特殊分段函数的图像画法
  11. cad安装日志文件发生错误_CAD安装问题,CAD安装时为什么显示内部发生错误急?...
  12. THREE.js开荒小记(二):无效宽度linewidth的Line 和 无效阔threshold的Line2
  13. 倒立摆c语言程序设计,清华大学倒立摆控制系统实验指导书.pdf
  14. 介绍中国传统节日的网页html,介绍中国传统节日的作文4篇
  15. cad快速选择命令快捷键_学好CAD必须掌握的20个常用快捷键命令
  16. 什么是自然语言处理,自然语言处理目前的应用有哪些?
  17. C++播放音频mav文件和mp3文件
  18. 先进PID控制Matlab仿真第4版-pdf课本+仿真程序
  19. 三种滤波算法针对不同噪声处理
  20. 工作流管理系统的简单介绍

热门文章

  1. PJzhang:微软出口管制条例
  2. c语言库用不用下载杀毒软件,开源杀毒软件ClamAV需要你的支持和帮助
  3. 形容等待时间长的句子_形容“等待时间长”的成语有哪些?
  4. 无界鼠标MOUSE WITHOUT BORDERS连接失败的一种情况
  5. 7月1日起交强险费率只与交通事故挂钩浮动
  6. linux 查看syn网络日志,Linux下分析SYN flood攻击案例
  7. Asp 解析 XML并分页显示
  8. 这可能是学日语最好的几个App
  9. 关于Intel芯片架构的发展史
  10. java游戏 麦克斯 狗,我的主角麦克斯----记南极大冒险中的狗狗们