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


    埃尔米特插值 原理

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

    #pragma once
    #include 
    #include 
    /*
    
    埃尔米特插值
    
    */
    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 operator
        InterpolationPoint& 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 
    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 <
        //f.close();
        return y; // 如果找不到对应的插值段,返回默认值
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140

    为了可视化效果可以把结果写到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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    ”邻接表建图+图的遍历“教程(C++)
    Jmeter分布式部署执行和常见报错
    物联网中的常见传感器
    算法基础 1.4 高精度 (加减乘除)
    2022年6月第十三届蓝桥杯大赛软件赛全国决赛C++A组题解
    MySQL进阶(再论事务)——什么是事务 & 事务的隔离级别 & 结合MySQL案例详细分析
    靠云业务独撑收入增长大梁,微软仍然被高估?
    解决阿里云ESC启动kube-proxy服务时出现错误 亲测有效
    go语言的使用方法
    抖音支付十万级 TPS 流量发券实践
  • 原文地址:https://blog.csdn.net/fuyouzhiyi/article/details/134550374