+++
title = "Multiple datasets"
weight = 80
+++

## Fitting several datasets at once

Several datasets can be fitted simultaneously,
either using the BornAgain fit facilities, or using external minimizers.

### With the BornAgain fit facilities

Add different simulation builders and data to an instance of `FitObjective`:

```python
    fit_objective = ba.FitObjective()
    fit_objective.addFitPair(sim_builder1, data1[, weight1])
    fit_objective.addFitPair(sim_builder2, data2[, weight2])
```

Each `sim_builder` is a callable that takes a parameter dict and returns
a `Simulation` object. The optional weights default to 1.

For a full example, see [Examples > Fitting > Simultaneous fit](/ref/fit/ex/multiple-datasets).

### With external minimizer

See the [honeycomb fit example](/ref/fit/ex/reflectometry-honeycomb),
where differential evolution is called with an objective function that
has contributions from several reflectometry datasets and models:

```python
    def objective_function(*args):
        ...
        return <sum of squared relative differences>

    result = scipy.optimize.differential_evolution(
        objective_function,
        ...)
```
