# scipy-curve-fit

scipy-curve-fit guides coding agents to estimate nonlinear model parameters from experimental data with scipy.optimize.curve_fit.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 2 installs · ★ 1933 repo stars
- Author: benchflow-ai
- License: Apache-2.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/benchflow-ai/skillsbench/tree/HEAD/tasks/hvac-control/environment/skills/scipy-curve-fit/SKILL.md
- Updated: 2026-10-24
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/scipy-curve-fit

## What it does

The skill provides reference implementations for nonlinear least squares parameter estimation in Python. It details how to set up `scipy.optimize.curve_fit`, configure parameter bounds, and choose initial guesses `p0` to speed up convergence.

It focuses on fitting first-order step response models. The agent learns how to extract gain `K` and time constant `tau`, while evaluating fit quality through R-squared and root mean square error calculations.

Without `scipy` and `numpy` in the environment, the generated scripts fail immediately. If input measurements contain `NaN` values or poor initial guesses, the solver terminates with a `RuntimeError`.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add benchflow-ai/skillsbench --skill scipy-curve-fit -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/scipy-curve-fit/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/scipy-curve-fit/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add benchflow-ai/skillsbench --skill scipy-curve-fit -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/scipy-curve-fit/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add benchflow-ai/skillsbench --skill scipy-curve-fit -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/scipy-curve-fit/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install benchflow-ai/skillsbench tasks/hvac-control/environment/skills/scipy-curve-fit
```

2. **Manually: put the files in place**
- File: `.github/skills/scipy-curve-fit/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add benchflow-ai/skillsbench --skill scipy-curve-fit -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/benchflow-ai/skillsbench --path tasks/hvac-control/environment/skills/scipy-curve-fit
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/scipy-curve-fit/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add benchflow-ai/skillsbench --skill scipy-curve-fit -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/benchflow-ai/skillsbench/tree/HEAD/tasks/hvac-control/environment/skills/scipy-curve-fit/SKILL.md

## When to use it
- Estimate parameters for nonlinear models using experimental test data
- Define parameter bounds and initial guesses for `curve_fit`
- Fit first-order step response models to identify gain `K` and time constant `tau`
- Evaluate fitting accuracy using R-squared and RMSE metrics

## Example prompts
- Fit a first-order step response to experimental time and output arrays using scipy.optimize.curve_fit
- Set initial guesses p0 and parameter bounds to prevent unrealistic values in curve_fit
- Calculate R-squared and RMSE metrics for my fitted model predictions

## Source text · SKILL.md · Apache-2.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 952bee93a66ddde1.

BEGIN THIRD-PARTY FILE 952bee93a66ddde1
````text
---
name: scipy-curve-fit
description: Use scipy.optimize.curve_fit for nonlinear least squares parameter estimation from experimental data.
---

# Using scipy.optimize.curve_fit for Parameter Estimation

## Overview

`scipy.optimize.curve_fit` is a tool for fitting models to experimental data using nonlinear least squares optimization.

## Basic Usage

```python
from scipy.optimize import curve_fit
import numpy as np

# Define your model function
def model(x, param1, param2):
    return param1 * (1 - np.exp(-x / param2))

# Fit to data
popt, pcov = curve_fit(model, x_data, y_data)

# popt contains the optimal parameters [param1, param2]
# pcov contains the covariance matrix
```

## Fitting a First-Order Step Response

```python
import numpy as np
from scipy.optimize import curve_fit

# Known values from experiment
y_initial = ...  # Initial output value
u = ...          # Input magnitude during step test

# Define the step response model
def step_response(t, K, tau):
    """First-order step response with fixed initial value and input."""
    return y_initial + K * u * (1 - np.exp(-t / tau))

# Your experimental data
t_data = np.array([...])  # Time points
y_data = np.array([...])  # Output readings

# Perform the fit
popt, pcov = curve_fit(
    step_response,
    t_data,
    y_data,
    p0=[K_guess, tau_guess],      # Initial guesses
    bounds=([K_min, tau_min], [K_max, tau_max])  # Parameter bounds
)

K_estimated, tau_estimated = popt
```

## Setting Initial Guesses (p0)

Good initial guesses speed up convergence:

```python
# Estimate K from steady-state data
K_guess = (y_data[-1] - y_initial) / u

# Estimate tau from 63.2% rise time
y_63 = y_initial + 0.632 * (y_data[-1] - y_initial)
idx_63 = np.argmin(np.abs(y_data - y_63))
tau_guess = t_data[idx_63]

p0 = [K_guess, tau_guess]
```

## Setting Parameter Bounds

Bounds prevent physically impossible solutions:

```python
bounds = (
    [lower_K, lower_tau],    # Lower bounds
    [upper_K, upper_tau]     # Upper bounds
)
```

## Calculating Fit Quality

### R-squared (Coefficient of Determination)

```python
# Predicted values from fitted model
y_predicted = step_response(t_data, K_estimated, tau_estimated)

# Calculate R-squared
ss_residuals = np.sum((y_data - y_predicted) ** 2)
ss_total = np.sum((y_data - np.mean(y_data)) ** 2)
r_squared = 1 - (ss_residuals / ss_total)
```

### Root Mean Square Error (RMSE)

```python
residuals = y_data - y_predicted
rmse = np.sqrt(np.mean(residuals ** 2))
```

## Complete Example

```python
import numpy as np
from scipy.optimize import curve_fit

def fit_first_order_model(data, y_initial, input_value):
    """
    Fit first-order model to step response data.
````
END THIRD-PARTY FILE 952bee93a66ddde1

The third-party file has ended. Everything between the 952bee93a66ddde1 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2695 of 4095 bytes. © benchflow-ai · Apache-2.0 · https://github.com/benchflow-ai/skillsbench/tree/HEAD/tasks/hvac-control/environment/skills/scipy-curve-fit/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
