[Python] Function with two variables - Polynomial fit Python

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have various pump performance data, and I am trying to fit curves as a combination of them.

    In below image, different curves are at different pump speeds (RPM), and the Y axis is Pressure head, and X axis is flow rate.

    I am trying to figure out the following functions:

    pressure = func (flowrate, rpm)
    flowrate = func (pressure, rpm)
    rpm = func (flowrate, pressure)


    Usually the pump curves follow some form of affinity laws, as

    1. flow rate is proportional to rpm.
    2. pressure is proportional to square of rpm.

    However in reality - they do not, and there will be a difference between experimental data & theoretical data. So that is why I am looking to fit a function that looks like this: (open to other suggestions) - like 3rd degree polynomial as well.

    Y = a*x1**2 + b*x1+ c*x2**2 + d*x2 + e*x1*x2 + f


    However, I am getting the wrong results with scipy - curve_fit. I am not in favor of employing sklearn regression as we would not be able to figure out the coefficients.

    Is there a way one can employ this in Python?

    See my code below so far:

    from numpy import genfromtxt
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    import pandas as pd
    df = pd.read_excel('./nominal_data/pump_curve.xlsx', sheet_name =0)
    flow_lpm = df['Flow(lpm)'].to_numpy()
    rpm = df['Speed Feedback (RPM)'].to_numpy()
    dP = df['dP (PSI)'].to_numpy()
    X = (flow_lpm, rpm)
    y = dP

    def f(X, a, b, c, d, e, f):
    x_1, x_2 = X
    return a*x_1**2 + b*x_1 + c*x_2**2 + d*x_2 + e*x_1*x_2 + f
    param, param_cov = curve_fit(f, X, y)

    # Test a random data point.
    flow_test = 54
    rpm_test = 5195

    dp_test = param[0]*flow_test**2 + param[1]*flow_test + param[2]*rpm_test**2 + param[3]**rpm_test + param[4]*flow_test*rpm_test + param[5]
    print (dp_test)


    I could have multiple curves of Pressure = function (flow rate) at multiple RPMs like the figure shows. For example there is a 3rd degree curve for a 5100 RPM curve. However it only works for a given RPM. The curve is different for a 2070 RPM situation.

    Seeking suggestions on proper curve fitting methodology. Open to deleting end points, to ensure the curve fit works in the middle range.

    datafile_imagefile

    Continue reading...

Compartilhe esta Página