> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ionworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Array Data Fits

> Fit the same model separately at each value of an independent variable (e.g. temperature, pulse SOC) and return the fitted parameter as a 2xN array.

An `ArrayDataFit` runs the same fit independently at each value of an
**independent variable**, and returns the fitted parameter as a 2xN
array — one row holding the independent-variable values and the other
holding the corresponding fitted values.

This is the right tool when the parameter you are fitting is expected to
*depend* on a state variable, and you want a sampled function rather
than a single scalar. Common examples:

* **Diffusivity vs. stoichiometry** from a GITT or pulse experiment —
  one fit per pulse, keyed by the midpoint stoichiometry of that pulse.
* **Exchange-current density or diffusivity vs. temperature** —
  one fit per chamber temperature.
* **OCP-derived parameters vs. SOC** — one fit per SOC setpoint.

## How it differs from a regular `DataFit`

A regular `DataFit` runs one optimization against all of its objectives
*together*, producing a single best-fit value per parameter. An
`ArrayDataFit` runs one optimization *per key* of `objectives`,
producing a fitted value per key. The keys themselves are the
independent-variable values.

|                            | `DataFit`                    | `ArrayDataFit`                                             |
| -------------------------- | ---------------------------- | ---------------------------------------------------------- |
| Number of optimizations    | One                          | One per `objectives` key                                   |
| Result shape per parameter | Scalar                       | 2xN array (`[independent_var_values, fitted_values]`)      |
| Use when                   | Parameter is a single number | Parameter is a sampled function of an independent variable |

## Python usage

```python theme={null}
import ionworkspipeline as iwp

# objectives keyed by the independent-variable value (here: midpoint
# stoichiometry of each pulse)
objectives = {
    0.25: iwp.objectives.Pulse(data_pulse_25, options={"model": model}),
    0.50: iwp.objectives.Pulse(data_pulse_50, options={"model": model}),
    0.75: iwp.objectives.Pulse(data_pulse_75, options={"model": model}),
}

array_fit = iwp.ArrayDataFit(
    objectives,
    parameters={
        "Positive particle diffusivity [m2.s-1]": iwp.Parameter(
            "D_p", initial_value=1e-14, bounds=(1e-15, 1e-13)
        ),
    },
)

result = array_fit.run(parameter_values)

# result.parameter_values["Positive particle diffusivity [m2.s-1]"]
# is a 2x3 array: row 0 = [0.25, 0.50, 0.75], row 1 = fitted D_p values
```

## API usage

The pipeline API accepts an `array_data_fit` element with the same
top-level fields as `data_fit`. The keys of `objectives` are the
independent-variable values:

```python theme={null}
pipeline_config = {
    "elements": {
        "known values": {"element_type": "entry", "values": parameter_values},
        "fit diffusivity vs sto": {
            "element_type": "array_data_fit",
            "objectives": {
                0.25: {"objective": "Pulse", "data": "db:meas-pulse-25", ...},
                0.50: {"objective": "Pulse", "data": "db:meas-pulse-50", ...},
                0.75: {"objective": "Pulse", "data": "db:meas-pulse-75", ...},
            },
            "parameters": {
                "Positive particle diffusivity [m2.s-1]": {
                    "bounds": [1e-15, 1e-13],
                    "initial_value": 1e-14,
                },
            },
        },
    },
}

pipeline = client.pipeline.create(pipeline_config)
```

See the [SimplePipeline guide](/simulate/api#running-simple-pipelines)
and the [API reference](https://pipeline.docs.ionworks.com/source/api/index.html)
for the full set of options shared with `DataFit` (cost, optimizer,
multistarts, priors, etc.).

## Related

<CardGroup cols={2}>
  <Card title="Introduction to Data Fitting" icon="chart-line" href="/guide/data-fitting/overview">
    Background on objectives, parameters, and optimizers
  </Card>

  <Card title="Objective Functions" icon="bullseye" href="/guide/data-fitting/objective-functions">
    Choose the right objective for your data
  </Card>
</CardGroup>
