Python使用曲线连接任意三个给定点 [英] Python Join any three given points using a curve

查看:723
本文介绍了Python使用曲线连接任意三个给定点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与已报告的问题有所不同,因为我参与这些活动的信息有限.我正在尝试使用曲线连接三个点.我没有其他信息可以加入曲线.我想要并且必须在任何给定的点上实现这一目标.下面给出了三个要点的示例:

This problem is different than what already reported because I have limited information to join the points. I am trying to join three points using a curve. I don't have any other information to join the curves. I want and have to achieve this for any given points. An example of three points is given below:

Point1 = [19.616, 8.171] 
Point2 = [28.7, 5.727]
Point3 = [34.506, 0.012125]

我的代码如下:

def curve_line(point1, point2):
    a = (point2[1] - point1[1])/(np.cosh(point2[0]) - np.cosh(point1[0]))
    b = point1[1] - a*np.sinh(point1[0])
    print(a,b,point1,point2)
    x = np.linspace(point1[0], point2[0],100)
    c = [b,  -a,  0.65636074, -0.05219088]
    y = a*np.cosh(x) + b 
    return x,y

x1,y1 = curve_line(point1, point2)
x2,y2 = curve_line(point2, point3)
plt.plot(x1,y1)
plt.plot(x2,y2) 

我的实际输出和预期输出如下:

My actual output and expected output are given below:

推荐答案

您使用与弯曲完全相同的方式进行操作.如果您有一个具有3个参数的函数并将其拟合到3个点,则将获得一条精确的解决方案,曲线将遍历所有3个点(归结为求解具有3个未知数的3个方程式):

You do it exactly the same way as with a curve. If you have a function with 3 parameters and fit it to three points, you'll get an exact solution with the curve going through all 3 points (it boils down to solving 3 equations with 3 unknowns):

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

x = np.array([ 1.92, 30, 34.21])
y = np.array([8.30, 5, 0.06])

def fun(x, a, b, c):
    return a * np.cosh(b * x )+ c

coef,_ = curve_fit(fun, x, y)

plt.plot(x, y, 'o', label='Original points')
plt.plot(np.linspace(x[0],x[-1]), fun(np.linspace(x[0],x[-1]), *coef), label=f'Model: %5.3f cosh(%4.2f x) + %4.2f' % tuple(coef) )
plt.legend()
plt.show()

在某些情况下,您必须提供一些合理的起始值.这些值可以从曲线的粗略草图中获得,在线函数绘图工具可能有助于理解各个参数的影响.

In some cases you'll have to give some sensible starting values. These values can be obtained from a rough sketch of the curve, an online function graphing tool may be helpful in understanding the influence of the individual parameters.

示例:

x = np.array([ 19.616, 28.7, 34.506])
y = np.array([8.171, 5.727, 0.012125])
p0 = [-0.1, 0.5, 8]
coef,_ = curve_fit(fun, x, y, p0)

这篇关于Python使用曲线连接任意三个给定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆