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

如何改善双对数坐标系下模型拟合曲线的视觉匹配效果

Fixing Fitted Curve Deviation in Log10 Coordinate Plots

Hey there! The visual mismatch between your fitted curve and data points in the log10 plot stems from two key issues:

  • Your current model is optimized for the original linear variable space, but your plot uses log-transformed coordinates. Errors in low-value regions (low shear rate/viscosity) get amplified in the log plot, making the fit look off.
  • There’s a mismatch between your initial linearization step (which follows the standard Cross model for shear-thinning fluids) and the simplified power-law-plus-constant model you’re actually fitting with nlsLM.

Let’s fix this with targeted adjustments that align your model with your data and plotting goals:

Step 1: Switch to the Cross Model (Better for Shear-Thinning Data)

Your initial code calculates start_viscosity using the formula for the Cross model, which is the standard choice for describing fluids that thin under shear (like your data shows). The Cross model is:
$$\eta = \eta_\infty + \frac{\eta_0 - \eta_\infty}{1 + (\lambda \dot{\gamma})^n}$$
Where:

  • $\eta_0$ = Zero-shear viscosity (viscosity at extremely low shear rates)
  • $\eta_\infty$ = Infinite-shear viscosity (viscosity at extremely high shear rates)
  • $\lambda$ = Time constant (relates to the shear rate where thinning starts)
  • $n$ = Power-law index (measures how quickly the fluid thins)

Your original model ($\eta = C \dot{\gamma}^{m-1} + \eta_\infty$) has a flaw: at very low shear rates, it predicts infinite viscosity, which doesn’t match your actual data. The Cross model fixes this by setting a finite zero-shear viscosity.

Step 2: Fit in Log Space for Visual Alignment

Since we’re plotting in log10 coordinates, we should optimize the fit to minimize errors in the log space directly. This ensures that the curve aligns visually with the data, even in the stretched low-value regions of the log plot.

Revised Fitting Code

library(data.table)
library(ggplot2)
library(minpack.lm)

# Fit Cross model optimized for log10 plot
fitted_models <- lapply(split(selected_data, by = "sample_name"), function(d) {
  d <- d[order(shear_rate)]
  
  # Estimate initial parameter values from data
  eta0_init <- mean(d[seq(nrow(d) - 5, nrow(d)), viscosity])  # Avg of highest viscosity (low shear)
  etainf_init <- mean(d[seq(5), viscosity])                   # Avg of lowest viscosity (high shear)
  
  # Linearize Cross model to get initial lambda and n
  d[, cross_transform := (eta0_init - viscosity) / (viscosity - etainf_init)]
  linear_fit <- lm(log(cross_transform) ~ log(shear_rate), data = d)
  n_init <- coef(linear_fit)[[2]]
  lambda_init <- exp(coef(linear_fit)[[1]] / n_init)
  
  # Fit Cross model using log10(viscosity) to optimize for log plot
  nlsLM(
    log10(viscosity) ~ log10(etainf + (eta0 - etainf)/(1 + (lambda * shear_rate)^n)),
    data = d,
    start = list(
      eta0 = eta0_init,
      etainf = etainf_init,
      lambda = lambda_init,
      n = n_init
    ),
    control = nls.lm.control(maxiter = 1000)  # Increase iterations if needed
  )
})

Updated Prediction & Plotting Code

# Generate predictions (convert back from log10 to original scale)
selected_data[, prediction := 10^predict(fitted_models[[ .BY[["sample_name"]] ]], .SD), by = "sample_name"]

# Create the log10 plot with aligned fit
p <- ggplot(selected_data, aes(x = shear_rate, y = viscosity, color = sample_name)) +
  geom_point(size = 1.5, alpha = 0.7) +  # Make points slightly transparent for clarity
  geom_line(aes(y = prediction), linewidth = 1) +
  coord_trans(x = "log10", y = "log10") +
  scale_color_discrete("Sample") +
  labs(x = "Shear Rate", y = "Viscosity") +
  theme_bw()
p

Why This Fixes the Deviation

  1. Correct Model: The Cross model matches both your initial linearization step and the physical behavior of your shear-thinning fluid, eliminating the unphysical infinite viscosity prediction of your original model.
  2. Log-Space Optimization: By fitting log10(viscosity) instead of raw viscosity, we’re directly minimizing the errors that are most visible in your log-transformed plot. This ensures the curve lines up with data points across the entire range of shear rates, including the low-value regions that were previously misaligned.

If you absolutely need to stick with your original model (instead of switching to Cross), you can modify the nlsLM call to fit log10(viscosity) with the log-transformed version of your model, but the Cross model is far more appropriate for this type of rheology data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:52:01