热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Newton插值算法的C++实现

2019独角兽企业重金招聘Python工程师标准头文件:**Copyright(c)2008-2011ZhangMing(M.Zhang),zmjerry163

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

头文件:

/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 2 or any later version.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.** This program is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for* more details. A copy of the GNU General Public License is available at:* http://www.fsf.org/licensing/licenses*//****************************************************************************** interpolation.h** Base class for interpolation.** Zhang Ming, 2010-04, Xi'an Jiaotong University.*****************************************************************************/#ifndef INTERPOLATION_H
#define INTERPOLATION_H#include namespace splab
{template class Interpolation{public:Interpolation( const Vector &xn, const Vector &yn ): xi(xn), yi(yn){}virtual ~Interpolation(){}virtual void calcCoefs() = 0;virtual Type evaluate( Type x ) = 0;protected:Vector xi; // abscissasVector yi; // ordinates};// class Interpolation}
// namespace splab#endif
// INTERPOLATION_H

/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 2 or any later version.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.** This program is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for* more details. A copy of the GNU General Public License is available at:* http://www.fsf.org/licensing/licenses*//****************************************************************************** newtoninterp.h** Newton interpolation method.** For a given points set "Pn" {xn,yn}, this class can find a polynomial which* cross all points of "Pn".** Zhang Ming, 2010-04, Xi'an Jiaotong University.*****************************************************************************/#ifndef NEWTONINTERP_H
#define NEWTONINTERP_H#include namespace splab
{template class NewtonInterp : public Interpolation{public:using Interpolation::xi;using Interpolation::yi;NewtonInterp( const Vector &xn, const Vector &yn );~NewtonInterp();void calcCoefs();Type evaluate( Type x );Vector getCoefs() const;private:Vector coefs;};// class NewtonInterp#include }
// namespace splab#endif
// NEWTONINTERP_H

实现文件:

/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 2 or any later version.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.** This program is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for* more details. A copy of the GNU General Public License is available at:* http://www.fsf.org/licensing/licenses*//****************************************************************************** newtoninterp-impl.h** Implementation for NewtonInterp class.** Zhang Ming, 2010-04, Xi'an Jiaotong University.*****************************************************************************//*** constructors and destructor*/
template
NewtonInterp::NewtonInterp( const Vector &xn,const Vector &yn ): Interpolation( xn, yn ), coefs(yn)
{
}template
NewtonInterp::~NewtonInterp()
{
}/*** Compute polynomial' coefsficients.*/
template
void NewtonInterp::calcCoefs()
{int N = xi.size();for( int j=1; j=j; --i )coefs[i] = (coefs[i]-coefs[i-1]) / (xi[i]-xi[i-j]);
}/*** Compute the value of polynomial at given "x".*/
template
Type NewtonInterp::evaluate( Type x )
{int N = xi.size();Type y = 0,tmp = 0;for( int j=0; j}/*** Get polynomial' coefsficients.*/
template
inline Vector NewtonInterp::getCoefs() const
{return coefs;
}

测试代码:

/****************************************************************************** newtoninterp_test.cpp** Newton interpolation testing.** Zhang Ming, 2010-04, Xi'an Jiaotong University.*****************************************************************************/#define BOUNDS_CHECK#include
#include
#include using namespace std;
using namespace splab;typedef double Type;int main()
{Vector x(5),y(5);x[0] &#61; 0; x[1] &#61; 30; x[2] &#61; 45; x[3] &#61; 60; x[4] &#61; 90;y[0] &#61; 0; y[1] &#61; 0.5; y[2] &#61; sqrt(2.0)/2; y[3] &#61; sqrt(3.0)/2; y[4] &#61; 1;NewtonInterp poly(x,y);poly.calcCoefs();cout <}

运行结果&#xff1a;

Coefficients of Newton interpolated polynomial:size: 5 by 1
0.0000
0.0167
-0.0001
-0.0000
0.0000The true and interpolated values:
0.1736 0.1734
0.3420 0.3419
0.7660 0.7660Process returned 0 (0x0) execution time : 0.078 s
Press any key to continue.


转:https://my.oschina.net/zmjerry/blog/10882



推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
author-avatar
齐老大2502895835
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有