You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双独立变量拟合a*(t^α)*(p_p^β)方程及NaN错误问询

Hey there! Let's tackle your problem from two angles first: fixing that ValueError about NaN values, and then getting your final fitted model expression right.

First: Fixing the NaN Input Error

The error you're seeing means somewhere in your input data (S_L1, t, or p_p) there are NaN values, which break the fitting process. Let's trace back to your calculation code to find the root causes:

Your unburned temperature calculation:

# Calculating unburned mass temperature
T_u = T_i*(p_filter/p_i)**((k_u-1)/k_u)
# Linear unburned temperature
t = T_u/T_i

Check these critical points to avoid NaNs:

  • p_i can't be 0: Dividing by zero will immediately create NaNs or infinities.
  • p_filter/p_i must be positive: Fractional exponents (like (k_u-1)/k_u) don't work with non-positive numbers in real-number arithmetic—this will produce NaNs if the ratio is ≤0.
  • k_u can't equal 1: If k_u=1, the exponent becomes 0/0 (undefined), which outputs NaN.
  • Final data check: Even if the above are fixed, run a quick check on your final arrays before fitting:
    import numpy as np
    print("NaN in S_L1:", np.isnan(S_L1).any())
    print("NaN in t:", np.isnan(t).any())
    print("NaN in p_p:", np.isnan(p_p).any())
    

If you find NaNs, clean your data by filtering out invalid entries:

# Get indices of all valid (non-NaN, non-infinite) data points
valid_mask = ~np.isnan(S_L1) & ~np.isinf(S_L1) & ~np.isnan(t) & ~np.isinf(t) & ~np.isnan(p_p) & ~np.isinf(p_p)

# Use only valid data for fitting
clean_S_L1 = S_L1[valid_mask]
clean_t = t[valid_mask]
clean_p_p = p_p[valid_mask]

Second: Writing the Fitted Model Expression

Assuming you're using lmfit (since you mentioned model.fit), here's how to structure your model and extract the final fitted expression:

Step 1: Define the Model Function

First, formalize your equation into a Python function that accepts your independent variables (t, p_p) and fitting parameters (a, alpha, beta):

def combustion_model(t, p_p, a, alpha, beta):
    return a * (t ** alpha) * (p_p ** beta)

Step 2: Initialize the Model and Parameters

Set up the lmfit model and initial guesses for your parameters (adjust initial values based on your domain knowledge):

from lmfit import Model

model = Model(combustion_model, independent_vars=['t', 'p_p'])
params = model.make_params(a=1.0, alpha=0.5, beta=0.5)  # Initial guesses

Step 3: Run the Fitting (with Clean Data)

Use the cleaned data we prepared earlier:

result = model.fit(clean_S_L1, params, t=clean_t, p_p=clean_p_p)

Step 4: Extract Fitted Parameters and Write the Expression

Once fitting succeeds, pull out the optimized parameter values:

a_fit = result.params['a'].value
alpha_fit = result.params['alpha'].value
beta_fit = result.params['beta'].value

Your final fitted model expression can be written in two ways:

  • Code form (for further calculations):

    S_L1_fit = a_fit * (t ** alpha_fit) * (p_p ** beta_fit)
    
  • Mathematical form (for reports/papers):

    $\hat{S}{L1} = {a{\text{fit}}} \cdot t^{{\alpha_{\text{fit}}}} \cdot p_p^{{\beta_{\text{fit}}}}$

    Or if LaTeX isn't supported:

    $S_{L1,\text{fit}} = a_{\text{fit}} \times t^{\alpha_{\text{fit}}} \times p_p^{\beta_{\text{fit}}}$

Quick Notes

  • Always print the fitting result summary to check parameter uncertainties and goodness-of-fit:
    print(result.fit_report())
    
  • If you're using scipy.optimize.curve_fit instead of lmfit, the model function structure is similar, but you'll need to flatten your independent variables into a single array.

内容的提问来源于stack exchange,提问作者user9259974

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:08:42