Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Created December 6, 2022 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourceperl/226f050871d7f1bc40962e8756431c08 to your computer and use it in GitHub Desktop.
Save sourceperl/226f050871d7f1bc40962e8756431c08 to your computer and use it in GitHub Desktop.
Fit a polynomial to reflect the Cv curve of a control valve (Cv/position).
#!/usr/bin/env python3
"""Fit a polynomial to reflect the Cv curve of a control valve (Cv/position)."""
import numpy as np
import matplotlib.pyplot as plt
# a list of reference points extracted from the data sheet of the valve
ref_pts_l = [(0, 0), (4, 5), (10, 15), (16, 21), (20, 25), (30, 40), (40, 75),
(50, 145), (53, 166), (60, 235), (70, 345), (80, 460), (90, 580), (100, 680)]
# fitting a seventh-order polynomial with this point
x_ref, y_ref = zip(*ref_pts_l)
poly_coef = np.polyfit(x_ref, y_ref, deg=7)
pos2cv_pol = np.poly1d(poly_coef)
# show result as plot
x = np.linspace(0, 100, 1000)
plt.scatter(x_ref, y_ref)
plt.plot(x, pos2cv_pol(x))
plt.xlabel('Valve opening (%)')
plt.xlim(0, 100)
plt.ylabel('Flow Coefficient (Cv)')
plt.ylim(0, 800)
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment