Skip to main content

Curve Fitting

Perform nonlinear parameter estimation and uncertainty analysis on experimental or engineering data. Worksheet observations feed SciPy curve fitting; interactive notebook controls select candidate models; diagnostics, residual plots, and confidence bands stay beside the code; and predictions publish back to Excel.

How to interact with this live demo
  1. Modify experimental observations: Edit any (x,y)(x, y) value in the blue worksheet cells (Data sheet).
  2. Switch candidate models: In the Python notebook, change the Model dropdown (Linear, Exponential, Power law, Polynomial) or drag the Confidence level slider.
  3. Inspect fitted results: See the parameter estimates, covariance standard errors, R2R^2, and confidence band plots update immediately, with predictions returned to the green worksheet range.

Need more space? Click Open in separate tab on the workbook header to test in a dedicated browser window.

Live interactive workbook

Curve Fitting interactive Python workbook
Loading Python workbook…

How the scientific workflow works

Scientific and engineering workflows often involve iterative model selection, parameter uncertainty quantification, and residual analysis that are difficult to express in spreadsheet formulas alone:

1. Feeding experimental data to SciPy

The notebook reads the observation table directly into NumPy arrays:

inputs = bf.inputs(observations=bf.ref("Data!A4:B24", headers=True))
x_data = inputs["observations"]["x"].to_numpy()
y_data = inputs["observations"]["y"].to_numpy()

2. Nonlinear parameter estimation

Using scipy.optimize.curve_fit, the notebook optimizes the chosen mathematical model and extracts parameter covariance for confidence bounds:

popt, pcov = curve_fit(selected_model_func, x_data, y_data)
perr = np.sqrt(np.diag(pcov)) # Parameter standard errors

3. Integrated visual diagnostics

Matplotlib renders both the fitted curve with confidence bands and the residual distribution alongside the parameter estimation code, keeping diagnostics co-located with the implementation.

4. Publishing predictions to the worksheet

Excel consumes the model parameters via =BF.OUTPUT("fit_summary") and evaluates arbitrary new points with a published custom function:

def predict(x_val):
return float(selected_model_func(x_val, *popt))

bf.publish(
outputs={"fit_summary": fit_summary, "predictions": predictions_table},
functions={"predict": predict},
)

In Excel, any cell can evaluate new values using =BF.FUNCTION("predict", A25).


What to try next