C#,数值计算——插值和外推,三次样条插值(Spline_interp)的计算方法与源程序

本文主要是介绍C#,数值计算——插值和外推,三次样条插值(Spline_interp)的计算方法与源程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// 三次样条插值
    /// Cubic Spline Interpolation
    /// Cubic spline interpolation object. Construct with x and y vectors, and
    /// (optionally) values of the first derivative at the endpoints, then call
    /// interp for interpolated values
    /// </summary>
    public class Spline_interp : Base_interp
    {
        private double[] y2 { get; set; }

        public Spline_interp(double[] xv, double[] yv, double yp1 = 1.0e99, double ypn = 1.0e99) : base(xv, yv[0], 2)
        {
            this.y2 = new double[xv.Length];
            sety2(xv, yv, yp1, ypn);
        }

        public Spline_interp(double[] xv, double yv, double yp1 = 1.0e99, double ypn = 1.0e99) : base(xv, yv, 2)
        {
            this.y2 = new double[xv.Length];
            sety2(xv, y2, yp1, ypn);
        }

        /// <summary>
        /// This routine stores an array y2[0..n - 1] with second derivatives of the
        /// interpolating function at the tabulated points pointed to by xv, using
        /// function values pointed to by yv.If yp1 and/or ypn are equal to 1.0E99 or
        /// larger, the routine is signaled to set the corresponding boundary condition
        /// for a natural spline, with zero second derivative on that boundary;
        /// otherwise, they are the values of the first derivatives at the endpoints.
        /// </summary>
        /// <param name="xv"></param>
        /// <param name="yv"></param>
        /// <param name="yp1"></param>
        /// <param name="ypn"></param>
        public void sety2(double[] xv, double[] yv, double yp1, double ypn)
        {
            double[] u = new double[n - 1];
            if (yp1 > 0.99e99)
            {
                y2[0] = u[0] = 0.0;
            }
            else
            {
                y2[0] = -0.5;
                u[0] = (3.0 / (xv[1] - xv[0])) * ((yv[1] - yv[0]) / (xv[1] - xv[0]) - yp1);
            }
            for (int i = 1; i < n - 1; i++)
            {
                double sig = (xv[i] - xv[i - 1]) / (xv[i + 1] - xv[i - 1]);
                double p = sig * y2[i - 1] + 2.0;
                y2[i] = (sig - 1.0) / p;
                u[i] = (yv[i + 1] - yv[i]) / (xv[i + 1] - xv[i]) - (yv[i] - yv[i - 1]) / (xv[i] - xv[i - 1]);
                u[i] = (6.0 * u[i] / (xv[i + 1] - xv[i - 1]) - sig * u[i - 1]) / p;
            }
            double qn;
            double un;
            if (ypn > 0.99e99)
            {
                qn = un = 0.0;
            }
            else
            {
                qn = 0.5;
                un = (3.0 / (xv[n - 1] - xv[n - 2])) * (ypn - (yv[n - 1] - yv[n - 2]) / (xv[n - 1] - xv[n - 2]));
            }
            y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
            for (int k = n - 2; k >= 0; k--)
            {
                y2[k] = y2[k] * y2[k + 1] + u[k];
            }
        }

        /// <summary>
        /// Given a value x, and using pointers to data xx and yy, this routine returns
        /// an interpolated value y, and stores an error estimate dy. The returned
        /// value is obtained by mm-point polynomial interpolation on the subrange
        /// xx[jl..jl + mm - 1].
        /// </summary>
        /// <param name="jl"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public override double rawinterp(int jl, double x)
        {
            int klo = jl;
            int khi = jl + 1;
            double h = xx[khi] - xx[klo];
            //if (h == 0.0)
            if (Math.Abs(h) <= float.Epsilon)
            {
                throw new Exception("Bad input to routine splint");
            }
            double a = (xx[khi] - x) / h;
            double b = (x - xx[klo]) / h;
            double y = a * yy[klo] + b * yy[khi] + ((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;
            return y;
        }
    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// 三次样条插值/// Cubic Spline Interpolation/// Cubic spline interpolation object. Construct with x and y vectors, and/// (optionally) values of the first derivative at the endpoints, then call/// interp for interpolated values/// </summary>public class Spline_interp : Base_interp{private double[] y2 { get; set; }public Spline_interp(double[] xv, double[] yv, double yp1 = 1.0e99, double ypn = 1.0e99) : base(xv, yv[0], 2){this.y2 = new double[xv.Length];sety2(xv, yv, yp1, ypn);}public Spline_interp(double[] xv, double yv, double yp1 = 1.0e99, double ypn = 1.0e99) : base(xv, yv, 2){this.y2 = new double[xv.Length];sety2(xv, y2, yp1, ypn);}/// <summary>/// This routine stores an array y2[0..n - 1] with second derivatives of the/// interpolating function at the tabulated points pointed to by xv, using/// function values pointed to by yv.If yp1 and/or ypn are equal to 1.0E99 or/// larger, the routine is signaled to set the corresponding boundary condition/// for a natural spline, with zero second derivative on that boundary;/// otherwise, they are the values of the first derivatives at the endpoints./// </summary>/// <param name="xv"></param>/// <param name="yv"></param>/// <param name="yp1"></param>/// <param name="ypn"></param>public void sety2(double[] xv, double[] yv, double yp1, double ypn){double[] u = new double[n - 1];if (yp1 > 0.99e99){y2[0] = u[0] = 0.0;}else{y2[0] = -0.5;u[0] = (3.0 / (xv[1] - xv[0])) * ((yv[1] - yv[0]) / (xv[1] - xv[0]) - yp1);}for (int i = 1; i < n - 1; i++){double sig = (xv[i] - xv[i - 1]) / (xv[i + 1] - xv[i - 1]);double p = sig * y2[i - 1] + 2.0;y2[i] = (sig - 1.0) / p;u[i] = (yv[i + 1] - yv[i]) / (xv[i + 1] - xv[i]) - (yv[i] - yv[i - 1]) / (xv[i] - xv[i - 1]);u[i] = (6.0 * u[i] / (xv[i + 1] - xv[i - 1]) - sig * u[i - 1]) / p;}double qn;double un;if (ypn > 0.99e99){qn = un = 0.0;}else{qn = 0.5;un = (3.0 / (xv[n - 1] - xv[n - 2])) * (ypn - (yv[n - 1] - yv[n - 2]) / (xv[n - 1] - xv[n - 2]));}y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);for (int k = n - 2; k >= 0; k--){y2[k] = y2[k] * y2[k + 1] + u[k];}}/// <summary>/// Given a value x, and using pointers to data xx and yy, this routine returns/// an interpolated value y, and stores an error estimate dy. The returned/// value is obtained by mm-point polynomial interpolation on the subrange/// xx[jl..jl + mm - 1]./// </summary>/// <param name="jl"></param>/// <param name="x"></param>/// <returns></returns>/// <exception cref="Exception"></exception>public override double rawinterp(int jl, double x){int klo = jl;int khi = jl + 1;double h = xx[khi] - xx[klo];//if (h == 0.0)if (Math.Abs(h) <= float.Epsilon){throw new Exception("Bad input to routine splint");}double a = (xx[khi] - x) / h;double b = (x - xx[klo]) / h;double y = a * yy[klo] + b * yy[khi] + ((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;return y;}}
}

这篇关于C#,数值计算——插值和外推,三次样条插值(Spline_interp)的计算方法与源程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

轨迹规划-B样条

B样条究竟是干啥的?白话就是给出一堆点,用样条的方式,给这些点连接起来,并保证丝滑的。 同时B样条分为准均匀和非均匀,以下为准均匀为例。 参考链接1:https://zhuanlan.zhihu.com/p/50626506https://zhuanlan.zhihu.com/p/50626506 参考链接2: https://zhuanlan.zhihu.com/p/536470972h

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t