埃尔米特插值(hermite 插值) C++

2023-11-23 16:15
文章标签 c++ 插值 hermite 埃尔米

本文主要是介绍埃尔米特插值(hermite 插值) C++,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

埃尔米特插值 原理

在这里插入图片描述
在这里插入图片描述

#pragma once
#include <vector>
#include <functional>
/*埃尔米特插值*/
struct InterpolationPoint {double x; // 插值点的横坐标double y; // 插值点的纵坐标double derivative; // 插值点的导数值// 默认构造函数InterpolationPoint() : x(0.0), y(0.0), derivative(0.0) {}// 带参数的构造函数InterpolationPoint(double x_val, double y_val, double derivative_val) : x(x_val), y(y_val), derivative(derivative_val) {}// 拷贝构造函数InterpolationPoint(const InterpolationPoint& other) : x(other.x), y(other.y), derivative(other.derivative) {}// 移动构造函数InterpolationPoint(InterpolationPoint&& other) noexcept : x(other.x), y(other.y), derivative(other.derivative) {other.x = 0.0;other.y = 0.0;other.derivative = 0.0;}// Copy assignment operatorInterpolationPoint& operator=(const InterpolationPoint& other) {if (this != &other) {x = other.x;y = other.y;derivative = other.derivative;}return *this;}// 设置插值点的值void set(double x_val, double y_val, double derivative_val) {x = x_val;y = y_val;derivative = derivative_val;}// 获取插值点的横坐标double get_x() const {return x;}// 获取插值点的纵坐标double get_y() const {return y;}// 获取插值点的导数值double get_derivative() const {return derivative;}
};class HermiteInterpolator {
public:HermiteInterpolator(const std::vector<InterpolationPoint>& points);HermiteInterpolator(int width, std::vector<int> &adjPoints);void setPoints(const std::vector<InterpolationPoint>& points);double interpolate(double x) ;private:// 返回连接两点的线段函数std::function<double(double)> getLineFunction( InterpolationPoint& p1,  InterpolationPoint& p2);private:std::vector<InterpolationPoint> points_;
};
#include "pch.h"
#include "HermiteInterpolator.h"
#include <fstream>
HermiteInterpolator::HermiteInterpolator(const std::vector<InterpolationPoint>& points) : points_(points)
{
}
HermiteInterpolator::HermiteInterpolator(int width, std::vector<int>& adjPoints)
{float step = width / adjPoints.size();for (int i = 0; i < adjPoints.size(); i++){InterpolationPoint point(step*i, adjPoints[i] , 0);points_.push_back(point);}
}
void HermiteInterpolator::setPoints(const std::vector<InterpolationPoint>& points)
{points_ = points;
}// 返回连接两点的线段函数
std::function<double(double)> HermiteInterpolator::getLineFunction( InterpolationPoint& p1,  InterpolationPoint& p2) {// 计算线段的斜率和截距double slope = (p2.y - p1.y) / (p2.x - p1.x);double intercept = p1.y - slope * p1.x;// 返回线段的lambda表达式return [slope, intercept](double x) {return slope * x + intercept;};
}
// 计算三次分段Hermite插值函数的值
double HermiteInterpolator::interpolate(double x)  {int y = 0;int n = points_.size();if (n < 3){// 获取线段函数std::function<double(double)> lineFunction = getLineFunction(points_[0], points_[1]);y= lineFunction(x);}else{for (int i = 0; i < n - 1; i++) {if (x >= points_[i].x && x <= points_[i + 1].x) {double h = points_[i + 1].x - points_[i].x;double t = (x - points_[i].x) / h;// (x-x_k)/(x_{k+1} - x_k)double tk = (x - points_[i + 1].x) / (-h); // (x - x_{ k + 1 }) / (x_k - x_{ k + 1 }) double y0 = (1 + 2 * t) * tk * tk;double y1 = (1 + 2 * tk) * t * t;double y2 = (x - points_[i].x) * tk * tk;double y3 = (x - points_[i + 1].x) * t * t;y= points_[i].y * y0 + points_[i + 1].y * y1 + points_[i].derivative * y2 + points_[i + 1].derivative * y3;}}}//ofstream  f;//f.open("D:\\work\\documentation\\HermiteInterpolator.txt", ios::app);//f <<x<<"," << y << endl;//f.close();return y; // 如果找不到对应的插值段,返回默认值
}

为了可视化效果可以把结果写到HermiteInterpolator.txt
画图python代码:

import matplotlib.pyplot as plt# 打开文本文件进行读取
with open('D:\\work\\documentation\\HermiteInterpolator.txt') as f:data = f.readlines()# 定义两个列表分别存储横坐标和纵坐标的数据    
x = []
y = [] # 遍历每一行
for i, line in enumerate(data):# 去除换行符if line:user_pwd_list = line.strip().split(',')# 横坐标是行号x.append(float(user_pwd_list[0]))# 纵坐标是数值数据y.append(float(user_pwd_list[1]))# 创建散点图    
plt.scatter(x, y)# 添加标题和轴标签
plt.title('Scatter Plot')  
plt.xlabel('Line')
plt.ylabel('Value')# 显示并保存图像
#plt.savefig('plot.png')
plt.show()

这篇关于埃尔米特插值(hermite 插值) C++的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/419157

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(