如果安装了scipy version>=0.18.0,则可以使用scipy.interpolate中的cubic spline函数进行三次样条曲线插值。

您可以通过在python中运行以下命令来检查scipy版本:#!/usr/bin/env python3

import scipy

scipy.version.version

如果scipy版本为>=0.18.0,则可以运行以下三次样条曲线插值的示例代码:#!/usr/bin/env python3

import numpy as np

from scipy.interpolate import CubicSpline

# calculate 5 natural cubic spline polynomials for 6 points

# (x,y) = (0,12) (1,14) (2,22) (3,39) (4,58) (5,77)

x = np.array([0, 1, 2, 3, 4, 5])

y = np.array([12,14,22,39,58,77])

# calculate natural cubic spline polynomials

cs = CubicSpline(x,y,bc_type='natural')

# show values of interpolation function at x=1.25

print('S(1.25) = ', cs(1.25))

## Aditional - find polynomial coefficients for different x regions

# if you want to print polynomial coefficients in form

# S0(0<=x<=1) = a0 + b0(x-x0) + c0(x-x0)^2 + d0(x-x0)^3

# S1(1< x<=2) = a1 + b1(x-x1) + c1(x-x1)^2 + d1(x-x1)^3

# ...

# S4(4< x<=5) = a4 + b4(x-x4) + c5(x-x4)^2 + d5(x-x4)^3

# x0 = 0; x1 = 1; x4 = 4; (start of x region interval)

# show values of a0, b0, c0, d0, a1, b1, c1, d1 ...

cs.c

# Polynomial coefficients for 0 <= x <= 1

a0 = cs.c.item(3,0)

b0 = cs.c.item(2,0)

c0 = cs.c.item(1,0)

d0 = cs.c.item(0,0)

# Polynomial coefficients for 1 < x <= 2

a1 = cs.c.item(3,1)

b1 = cs.c.item(2,1)

c1 = cs.c.item(1,1)

d1 = cs.c.item(0,1)

# ...

# Polynomial coefficients for 4 < x <= 5

a4 = cs.c.item(3,4)

b4 = cs.c.item(2,4)

c4 = cs.c.item(1,4)

d4 = cs.c.item(0,4)

# Print polynomial equations for different x regions

print('S0(0<=x<=1) = ', a0, ' + ', b0, '(x-0) + ', c0, '(x-0)^2 + ', d0, '(x-0)^3')

print('S1(1< x<=2) = ', a1, ' + ', b1, '(x-1) + ', c1, '(x-1)^2 + ', d1, '(x-1)^3')

print('...')

print('S5(4< x<=5) = ', a4, ' + ', b4, '(x-4) + ', c4, '(x-4)^2 + ', d4, '(x-4)^3')

# So we can calculate S(1.25) by using equation S1(1< x<=2)

print('S(1.25) = ', a1 + b1*0.25 + c1*(0.25**2) + d1*(0.25**3))

# Cubic spline interpolation calculus example

# https://www.youtube.com/watch?v=gT7F3TWihvk

三次样条python_如何在python中进行三次样条插值?相关推荐

  1. closecmd python_如何在python中禁止控制台/ cmd错误消息

    如何在 python中为chromedriver和pyinstaller exe压缩错误消息? 我注意到当我在pyinstaller中使用chromedriver并运行它时,我得到一个错误消息列表.我 ...

  2. float在python_如何在python中读取.float文件? - python

    Improve this question 我正在处理大脑MRI数据,它是.float数据. 您知道如何在python中使用它吗? 与 with open('[43x25520].float') as ...

  3. opencv检测图片失焦 python_如何在Python中使用OpenCV执行模糊检测

    如何在Python中使用OpenCV执行模糊检测 目标检测 最后更新 2020-10-12 14:23 阅读 154 最后更新 2020-10-12 14:23 阅读 154 目标检测 ##FlyAI ...

  4. 怎么证明会python_如何在python中验证SSL证书?

    我需要验证证书是否由我的自定义CA签名.使用OpenSSL命令行实用程序很容易做到:# Custom CA file: ca-cert.pem # Cert signed by above CA: b ...

  5. 模拟退火算法 python_如何在python中实现模拟退火算法

    模拟退火算法 python The Simulated Annealing algorithm is commonly used when we're stuck trying to optimize ...

  6. 价钱转换python_如何在python中转换货币?

    我正在做一个虚拟助手项目.我想让它告诉我其他货币的美元汇率. 我用beauthoulsoup编写了以下代码,它从给定的网站获取数据,对其进行解析并在命令行中打印结果供我阅读.但这只是美元对巴基斯坦卢比 ...

  7. 返回1到n的所有组合python_如何在Python中生成0-1矩阵的所有可能组合?

    如何生成大小为K的0-1矩阵的所有可能组合? 例如,如果我取K = 2且N = 2,我得到以下组合. combination 1 [0, 0; 0, 0]; combination 2 [1, 0; ...

  8. if函数python_关于函数:如何在python中为一个if语句提供多个条件

    本问题已经有最佳答案,请猛点这里访问. 所以我在用python 3.1.5编写一些代码,这些代码需要有多个条件才能发生某些事情.例子: 1 2 3 4 5def example(arg1, arg2, ...

  9. 如何在python中找到两个日期时间对象之间的时差?

    本文翻译自:How do I find the time difference between two datetime objects in python? 如何分辨两个datetime对象之间的时 ...

最新文章

  1. 深入浅析mysql引擎
  2. Go协程池设计思路(Task-Job-Worker)
  3. Spring Boot文档阅读笔记-Creating Asynchronous Methods解析
  4. Linux学习笔记——Centos6.4
  5. oracle数据库常用操作语句
  6. linker command failed with exit code 1 (错误总结)
  7. Log4j有哪几种日志级别呢?
  8. 计算机网络习题(参考)
  9. java程序中,如何设置周一为一周的开始?如何设置周一为一周的第一天? 或者说,如何理解java的setFirstDayOfWeek()方法?
  10. 深圳保诚后端Java社招面试
  11. 【读书笔记】《谈谈方法》(笛卡尔)
  12. Zeloof 自制芯片工艺
  13. Error:java: 读取xxx.jar时出错; zip file is empty
  14. 新开淘宝店铺如何推广运营
  15. OCR应用(证件识别、文档识别)
  16. 初试GD32F103C8T6
  17. 当白帽黑客遇到了网络诈骗,他是如何套路并反制骗子的?
  18. Linux ARM平台开发系列讲解(TTY) 2.5.2 串口TTY子系统驱动源码分析
  19. VBox虚拟机Linux安装与配置
  20. 智力大冲浪 【贪心】

热门文章

  1. 联合双边滤波-Joint Bilateral Filter
  2. css3旋转(绕Y轴)
  3. 基于Kaggle训练集预测的多层人工神经网络的能源消耗的时间序列预测(Matlab代码实现)
  4. Android编译可执行c程序
  5. 树莓派 禁止屏幕关闭
  6. 新手适合用什么Vue组件库?TinyVue组件库好用吗?
  7. 计算机进程控制块的作用是什么,计算机考研操作系统之进程控制块(PCB)【中公原创】...
  8. 2018-2-13-不使用数据结构反转栈
  9. rancher 一键安装集群脚本
  10. 数据挖掘算法比赛 - 简单经验总结