# Battery Parameter Extraction from Data

This example shows optimization of the Battery block's parameters to fit data defined over different temperatures. It uses the MATLAB® optimization function `fminsearch`. Other products available for performing this type of parameter fitting with Simscape™ Electrical™ models are the Optimization Toolbox™ and Simulink® Design Optimization™. These products provide predefined functions to manipulate and analyze blocks using GUIs or a command line approach.

### Strategy

Fit output voltage curves for a Battery to data using a 4 step procedure:

1. Optimize parameters in the Battery Main dialog tab.

2. Optimize parameters in the Battery Dynamics dialog tab.

3. Optimize nominal voltage and internal resistance in the Battery Temperature Dependence dialog tab.

4. Optimize temperature dependent charge dynamics parameters in the Battery Temperature Dependence dialog tab.

### Data and Block Setup

The MATLAB data file, BatteryParameterizationData.mat, stores Battery data as an array of structures. Each structure contains 5 fields: v (voltage), i (current), t (time), SOC0 (initial state of charge) and T (temperature). Scope save the output voltage as structure data, out.Vo.signals.values.

### Initial Parameter Specification

Starting values for `fminsearch` can be estimated using a combination of Battery block defaults and data sheet values

List of parameters and initial values prior to optimization

``` Vnom = 3.6 R1 = 0.045 AH = 2.7 V1 = 3.4 AH1 = 1.4 Rp1 = 0.006 tau1 = 200 Vnom_T2 = 3.8 R1_T2 = 0.055 V1_T2 = 3.6 Rp1_T2 = 0.006 tau1_T2 = 200 ```

Since `fminsearch` is an unconstrained nonlinear optimizer that locates a local minimum of a function, varying the initial estimate will result in a different solution set.

### Plot Data Versus Battery Output Using Initial Parameters

Load single cell Battery model and set parameters

### Sum of Squares of Error Calculation

`BatteryParameterizationCostFunction` is the function to be minimized by `fminsearch`. This function returns a sum of squares of error for the difference between the Battery output voltage and the data. If an invalid parameter value is supplied by `fminsearch`, the `catch` statement returns a large value for the error.

### Optimize Main Tab Dialog Parameters Without Charge Dynamics (Step 1)

```Optimized parameters for the battery main dialog tab are: Vnom = 3.6999 R1 = 0.050299 AH = 2.6033 V1 = 3.5265 AH1 = 1.4 ```

### Optimize Charge Dynamics Parameters (Step 2)

```Optimized parameters for the Battery, including charge dynamics, are: Vnom = 3.699931 R1 = 0.05019736 AH = 2.603326 V1 = 3.526493 AH1 = 1.4 Rp1 = 0.005029392 tau1 = 109.691 ```

### Optimize Temperature Dependent Parameters (Step 3)

```Optimized temperature dependent parameters for the Battery are: Vnom_T2 = 3.9003 R1_T2 = 0.081404 V1_T2 = 3.8133 ```

### Optimize Charge Dynamics Parameters for Second Temperature (Step 4)

```Optimized temperature dependent parameters for the Battery, including charge dynamics, are: Vnom_T2 = 3.900266 R1_T2 = 0.07979468 V1_T2 = 3.813256 Rp1_T2 = 0.007920818 tau1_T2 = 160.2999 ```