[Solved] Python Join any three given points using a curve


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()

enter image description here

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.

Example:

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)

12

solved Python Join any three given points using a curve