# Battery Capacity Estimator (Least Squares)

Battery capacity estimator using least-squares algorithms

Since R2024a

Libraries:
Simscape / Battery / BMS / Estimators

## Description

The Battery Capacity Estimator (Least Squares) block calculates the cell capacity of a battery by using least-squares algorithms.

For discrete-time simulation, set the Sample time parameter to a positive value. To inherit the sample time, set Sample time to `-1`. For continuous-time simulation, set Sample time to `0`.

### Equations

The least-squares estimation finds the line of best fit for a set of data points by minimizing the sum of the squared residuals. The squared residuals are the difference between the observed responses and the predicted responses of the model. This line of best fit predicts the dependent variable.

This equation defines the battery state of charge (SOC) at time tn2,

`$SOC\left({t}_{\text{n2}}\right)=SOC\left({t}_{\text{n1}}\right)+\frac{1}{Q}{\int }_{{t}_{\text{n1}}}^{{t}_{\text{n2}}}i\left(\tau \right)d\tau ,$`

where:

• SOC(tn1) is the SOC at time tn1, with n={1,…,N}.

• Q is the capacity in ampere-hours.

• i is the battery cell current in amperes.

• ${\int }_{{t}_{\text{n1}}}^{{t}_{\text{n2}}}i\left(\tau \right)d\tau$ denotes the integrated battery cell current over the time interval tn2 - tn1. Time tn1 and tn2 must be in hours for the units to be consistent.

Define ${x}_{\text{n}}=SOC\left({t}_{\text{n2}}\right)-SOC\left({t}_{\text{n1}}\right)$ and ${y}_{\text{n}}={\int }_{{t}_{\text{n1}}}^{{t}_{\text{n2}}}i\left(\tau \right)d\tau$, and rewrite the SOC equation:

`${y}_{\text{n}}=Q{x}_{\text{n}}.$`

The block uses this linear equation to determine an estimate of the cell capacity using the least squares estimation,

`$\left({y}_{\text{n}}-\Delta {y}_{\text{n}}\right)=\stackrel{^}{Q}\left({x}_{\text{n}}-\Delta {x}_{\text{n}}\right),$`

where:

• Δxn is the error associated with the SOC estimation.

• Δyn is the error associated with the current sensor.

• $\stackrel{^}{Q}$ is an estimate of the true cell capacity under noisy measurements.

Ordinary Least Squares

The weighted ordinary least squares (OLS) method estimates the cell capacity by minimizing the weighted squared errors, Δyn, of the merit function

`${x}_{\text{WLS}}^{2}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-\stackrel{^}{Q}{x}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}},$`

where σ2yn denotes the variance of the current measurement and Yn is a point on the line ${Y}_{\text{n}}=\stackrel{^}{Q}{x}_{\text{n}}$ that corresponds to the measurement data point {xn,yn}. The block assumes that yn has noise and xn has no noise.

To solve the weighted OLS problem, the block differentiates the merit function with respect to $\stackrel{^}{Q}$ and solves for $\stackrel{^}{Q}$ by setting the partial derivative to zero [1]:

`$\begin{array}{c}\frac{\partial {x}_{\text{WLS}}^{2}}{\partial \stackrel{^}{Q}}=-2\sum _{\text{n}=1}^{\text{N}}\frac{{x}_{\text{n}}\left({y}_{\text{n}}-\stackrel{^}{Q}{x}_{\text{n}}\right)}{{\sigma }_{\text{yn}}^{2}}=0\\ \stackrel{^}{Q}\sum _{\text{n}=1}^{\text{N}}\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}\\ \stackrel{^}{Q}=\frac{\sum _{\text{n}=1}^{\text{N}}\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}}{\sum _{\text{n}=1}^{\text{N}}\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}}\end{array}$`

This method can be computed recursively and is easily adapted to allow fading of past measurements.

If you define the forgetting factor as $0\ll \gamma \le 1$, then the block obtains the estimated capacity from the equation

`$\stackrel{^}{Q}=\frac{{k}_{2,\text{n}}}{{k}_{1,\text{n}}},$`

where:

`$\begin{array}{l}{k}_{1,\text{n}}=\gamma {k}_{1,\text{n}-1}+\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}\\ {k}_{2,\text{n}}=\gamma {k}_{2,\text{n}-1}+\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}\end{array}$`

Total Least Squares

The weighted total least squares (TLS) method estimates the cell capacity by minimizing the sum of both weighted square errors, Δxn and Δyn, of the merit function [1],[2],

`${\chi }_{\text{WLTS}}^{2}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}}+\frac{{\left({x}_{\text{n}}-{X}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{xn}}^{2}},$`

where σ2yn denotes the variance of the current measurement, σ2xn denotes the variance of the error on the SOC estimates, and Xn and Yn are points on the line ${Y}_{\text{n}}=\stackrel{^}{Q}{X}_{\text{n}}$ that corresponds to the noisy measured data point {xn,yn}.

To enforce ${Y}_{\text{n}}=\stackrel{^}{Q}{X}_{\text{n}}$, the block augments the merit function with Lagrange multipliers λi:

`${\chi }_{\text{WLTS,}a}^{\text{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}}+\frac{{\left({x}_{\text{n}}-{X}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{xn}}^{2}}-{\lambda }_{i}\left({Y}_{\text{n}}-\stackrel{^}{Q}{X}_{\text{n}}\right).$`

The block sets the partial derivatives of ${\chi }_{\text{WLTS,}a}^{\text{2}}$ with respect to λi, Yn, and Xn to zero:

`$\begin{array}{c}\frac{\partial {\chi }_{\text{WLTS,}a}^{\text{2}}}{\partial {\lambda }_{i}}=-\left({Y}_{\text{n}}-\stackrel{^}{Q}{X}_{\text{n}}\right)=0\to {Y}_{\text{n}}=\stackrel{^}{Q}{X}_{\text{n}}\\ \frac{\partial {\chi }_{\text{WLTS,}a}^{\text{2}}}{\partial {Y}_{n}}=\frac{-2\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}{{\sigma }_{\text{yn}}^{2}}-{\lambda }_{i}=0\to {\lambda }_{i}=\frac{-2\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}{{\sigma }_{\text{yn}}^{2}}\\ \frac{\partial {\chi }_{\text{WLTS,}a}^{\text{2}}}{\partial {X}_{n}}=\frac{-2\left({x}_{\text{n}}-{X}_{\text{n}}\right)}{{\sigma }_{\text{xn}}^{2}}+{\lambda }_{i}\stackrel{^}{Q}=0\to {X}_{n}=\frac{{x}_{n}{\sigma }_{yn}^{2}+\stackrel{^}{Q}{y}_{n}{\sigma }_{xn}^{2}}{{\sigma }_{yn}^{2}+{\stackrel{^}{Q}}^{2}{\sigma }_{xn}^{2}}\end{array}$`

Then the block rewrites the merit function as [1]:

`${\chi }_{\text{WLTS}}^{\text{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}}+\frac{{\left({x}_{\text{n}}-{X}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{xn}}^{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-\stackrel{^}{Q}{x}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}+{\stackrel{^}{Q}}^{2}{\sigma }_{\text{xn}}^{2}}.$`

Finally, the block sets the partial derivative of ${\chi }_{\text{WLTS}}^{\text{2}}$ with respect to $\stackrel{^}{Q}$ to zero:

`$\frac{\partial {\chi }_{\text{WLTS}}^{\text{2}}}{\partial \stackrel{^}{Q}}=\sum _{\text{n}=1}^{\text{N}}\frac{2\left(\stackrel{^}{Q}{x}_{\text{n}}-{y}_{\text{n}}\right)\left(\stackrel{^}{Q}{y}_{\text{n}}{\sigma }_{\text{xn}}^{2}+{x}_{\text{n}}{\sigma }_{\text{yn}}^{2}\right)}{{\left({\stackrel{^}{Q}}^{2}{\sigma }_{\text{xn}}^{2}+{\sigma }_{\text{yn}}^{2}\right)}^{2}}=0.$`

In this equation:

• There is no closed-form solution in the general case. To find a closed-form solution, perform a Newton-Raphson search for $\stackrel{^}{Q}$.

• There is no recursive update in the general case.

• The solution is impractical for implementation in an embedded system.

Recursive Approximation of Total Least Squares (RTLS)

The block substitutes ${\sigma }_{\text{xn}}=k{\sigma }_{\text{yn}}$ in the equation of ${\chi }_{\text{WLTS}}^{\text{2}}$:

`${\chi }_{\text{RTLS}}^{2}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-{Y}_{\text{n}}\right)}^{2}}{{\sigma }_{\text{yn}}^{2}}+\frac{{\left({x}_{\text{n}}-{X}_{\text{n}}\right)}^{2}}{k{\sigma }_{\text{yn}}^{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-\stackrel{^}{Q}{x}_{n}\right)}^{2}}{\left({\stackrel{^}{Q}}^{2}{k}^{2}+1\right){\sigma }_{\text{yn}}^{2}}.$`

The partial derivative of the WTLS merit function reduces to:

`$\begin{array}{c}\frac{\partial {\chi }_{\text{RTLS}}^{2}}{\partial \stackrel{^}{Q}}=2\sum _{\text{n}=1}^{\text{N}}\frac{\left(\stackrel{^}{Q}{x}_{\text{n}}-{y}_{\text{n}}\right)\left(\stackrel{^}{Q}{k}^{2}{y}_{\text{n}}+{x}_{\text{n}}\right)}{{\left({\stackrel{^}{Q}}^{2}{k}^{2}+1\right)}^{2}{\sigma }_{\text{yn}}^{2}}=0\\ {\stackrel{^}{Q}}^{2}\underset{a}{\underbrace{\sum _{\text{n}=1}^{\text{N}}{k}^{2}\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}}}+\underset{b}{\underbrace{\sum _{\text{n}=1}^{\text{N}}{k}^{2}\frac{{x}_{\text{n}}^{2}-{k}^{2}{y}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}}}+\underset{c}{\underbrace{\sum _{\text{n}=1}^{\text{N}}\frac{-{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}}}=0\\ \stackrel{^}{Q}=\frac{-b±\sqrt{{b}^{2}-4ac}}{2a}\end{array}$`

The block incorporates fading memory by using this equation

`$\stackrel{^}{Q}=\frac{-{k}_{1,\text{n}}+{k}^{2}{k}_{3,\text{n}}+\sqrt{{\left({k}_{1,\text{n}}-{k}^{2}{k}_{3,\text{n}}\right)}^{2}+4{k}^{2}{k}_{2,\text{n}}^{2}}}{2{k}^{2}{k}_{2,\text{n}}},$`

where:

`$\begin{array}{c}{k}_{1,\text{n}}=\gamma {k}_{1,\text{n}-1}+\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}\\ {k}_{2,\text{n}}=\gamma {k}_{2,\text{n}-1}+\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}\\ {k}_{3,\text{n}}=\gamma {k}_{3,\text{n}-1}+\frac{{y}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}\end{array}$`

Approximate Total Least Squares (ATLS)

The ATLS method is recursive and allows σ2yn and σ2xn to be non-proportional.

This figure shows the definitions the block uses to derive the approximate weighted total least squares:

The block defines Δxn and Δyn as the x-distance and y-distance between the data point n and the line, respectively. The slope of the line is $\stackrel{^}{Q}=\frac{\Delta {y}_{n}}{\Delta {x}_{n}}$ for all n. The angle of the line is $\theta ={\mathrm{tan}}^{-1}\stackrel{^}{Q}.$

This equation defines the shortest distance between the line and a given data point:

`${R}_{\text{n}}=\Delta {y}_{\text{n}}\mathrm{cos}\theta =\frac{\Delta {y}_{\text{n}}}{\sqrt{1+{\stackrel{^}{Q}}^{2}}}.$`

If $\delta {x}_{i}={R}_{i}\mathrm{sin}\theta$ and $\delta {y}_{i}={R}_{i}\mathrm{cos}\theta$, the block defines the x and y components of the perpendicular distance between the data point n and the fitting line. Then the block can define the AWTLS merit function using this equation:

`${\chi }_{\text{AWTLS}}^{2}=\sum _{\text{n}=1}^{\text{N}}\frac{\delta {x}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}+\frac{\delta {y}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}=\sum _{\text{n}=1}^{\text{N}}\frac{{\left({y}_{\text{n}}-\stackrel{^}{Q}{x}_{\text{n}}\right)}^{2}}{{\left({\stackrel{^}{Q}}^{2}+1\right)}^{2}}\left(\frac{{\stackrel{^}{Q}}^{2}}{{\sigma }_{\text{xn}}^{2}}+\frac{1}{{\sigma }_{\text{yn}}^{2}}\right).$`

This equation defines the Jacobian of the AWTLS merit function:

`$\frac{\partial {\chi }_{\text{AWTLS}}^{2}}{\partial \stackrel{^}{Q}}=\frac{2}{{\left({\stackrel{^}{Q}}^{2}+1\right)}^{3}}\sum _{\text{n}=1}^{\text{N}}{\stackrel{^}{Q}}^{4}\frac{{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{xn}}^{2}}+{\stackrel{^}{Q}}^{3}\left(\frac{2{x}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}-\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}-\frac{{y}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}\right)+{\stackrel{^}{Q}}^{2}\left(\frac{3{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}-\frac{3{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{xn}}^{2}}\right)+\stackrel{^}{Q}\left(\frac{{x}_{\text{n}}^{2}-2{y}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}+\frac{{y}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}\right)+\frac{-{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{yn}}^{2}}.$`

The roots of the quartic equation are the candidate solutions for $\stackrel{^}{Q}$. The block rewrites the quartic equations as

`${c}_{5,\text{n}}{\stackrel{^}{Q}}^{4}+\left(2{c}_{4,\text{n}}-{c}_{1,\text{n}}-{c}_{6,\text{n}}\right){\stackrel{^}{Q}}^{3}+\left(3{c}_{2,\text{n}}-3{c}_{5,\text{n}}\right){\stackrel{^}{Q}}^{2}+\left({c}_{1,\text{n}}-2{c}_{3,\text{n}}+{c}_{6,\text{n}}\right)\stackrel{^}{Q}-{c}_{2,\text{n}}=0,$`

where:

`$\begin{array}{c}{c}_{1,\text{n}}=\gamma {c}_{1,\text{n}-1}+\frac{{x}_{\text{n}}^{2}}{{k}^{2}{\sigma }_{\text{yn}}^{2}}\\ {c}_{2,\text{n}}=\gamma {c}_{2,\text{n}-1}+\frac{k{x}_{\text{n}}{y}_{\text{n}}}{{k}^{2}{\sigma }_{\text{yn}}^{2}}\\ {c}_{3,\text{n}}=\gamma {c}_{3,\text{n}-1}+\frac{{y}_{\text{n}}^{2}}{{\sigma }_{\text{yn}}^{2}}\\ {c}_{4,\text{n}}=\gamma {c}_{4,\text{n}-1}+\frac{{x}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}\\ {c}_{\text{5,n}}=\gamma {c}_{5,\text{n}-1}+\frac{k{x}_{\text{n}}{y}_{\text{n}}}{{\sigma }_{\text{xn}}^{2}}\\ {c}_{\text{6,n}}=\gamma {c}_{6,\text{n}-1}+\frac{{k}^{2}{y}_{\text{n}}^{2}}{{\sigma }_{\text{xn}}^{2}}\end{array}$`

The ATLS merit function does not equal the weighted TLS merit function when ${\sigma }_{\text{xn}}=k{\sigma }_{\text{yn}}$.

When ${\sigma }_{\text{xn}}=k{\sigma }_{\text{yn}}$, the block uses this equation to estimate the true cell capacity:

`${\stackrel{^}{Q}}_{\text{corrected}}=\frac{\stackrel{^}{Q}}{k}.$`

### Implementation

This figure shows how the block estimates the battery capacity using the different least-squares algorithms:

During the Data Acquisition phase, the block runs in a loop until the algorithm finds a valid data point. The block follows these steps:

1. The block receives an SOC estimate at time tn1 using an SOC estimator.

2. The block recevies a second SOC estimate at time tn2.

3. While $SOC\left({t}_{\text{n}1}\right)-SOC\left({t}_{\text{n}2}\right), the block:

• Keeps receiving SOC estimates.

• Integrates cell current, ${y}_{\text{n}}={\int }_{{t}_{\text{n1}}}^{{t}_{\text{n2}}}i\left(\tau \right)d\tau$.

• Overwrites time tn2 with a new timestamp.

• If the battery is charging, the block discards the measurement and goes back to step 1.

### Assumptions and Limitations

• The block assumes the random errors Δxn and Δyn follow a zero-mean normal distribution.

• The block assumes that the weights σ2xn and σ2yn are known.

• In the RTLS algorithm, the scaling factor k must proportionally relate the weights σ2xn and σ2yn such that ${\sigma }_{\text{xn}}=k{\sigma }_{\text{yn}}$

## Ports

### Input

expand all

Battery current, in amperes, specified as a scalar for a single cell or a vector for multiple cells. To specify this input as a vector of cell currents, select the Specify Current input as cell current(s) parameter. If you select this parameter, the size of this input port must be equal to the size of the other input ports.

State of charge of one or multiple cells, specified as a scalar or vector of entries in the range [0, 1]. The size of this input port must be equal to the size of the InitialCellAH input port.

Initial cell capacity of the battery, in ampere-hours, specified as a scalar for a single cell or a vector for multiple cells. The size of this input port must be equal to the size of the CellSOC input port.

### Output

expand all

Estimated cell capacity of the battery, in ampere-hours, returned as a scalar or a vector. The size of this output port is equal to the size of the InitialCellAH input port.

## Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select .

Option to specify the value of the Current input port as a vector of cell currents. If you select this parameter, the value at the Current input port can be a scalar or a vector of size equal to the size of the block inputs.

Least-square algorithm that the block uses to estimate the battery cell capacity.

Threshold to detect a change in the SOC. Typical values are 0.05 or 0.03, which represent a 5% or 3% change.

Forgetting factor.

Variance of the battery SOC.

#### Dependencies

To enable this parameter, set Least squares algorithm to `Approximate total least squares`.

Variance of the current measurement.

Scaling factor k.

#### Dependencies

To enable this parameter, set Least squares algorithm to `Recursive total least squares` or ```Approximate total least squares```.

Time between consecutive block executions. During execution, the block produces outputs and, if appropriate, updates its internal state. For more information, see What Is Sample Time? and Specify Sample Time.

For inherited discrete-time operation, specify this parameter as `-1`. For discrete-time operation, specify this parameter as a positive integer. For continuous-time operation, specify this parameter as `0`.

If this block is in a masked subsystem or a variant subsystem that supports switching between continuous and discrete operations, promote the sample time parameter. Promoting the sample time parameter ensures correct switching between the continuous and discrete implementations of the block. For more information, see Promote Block Parameters on a Mask.

## References

[1] Plett, G., Battery Management Systems: Volume II, Equivalent-Circuit Methods, Artech House, 2015.

[2] E. Bakas, B. Rosca, S. Wilkins, T. Donkers. Least-Squares-based Capacity Estimation for Lithium-ion Battery Cells, The European Battery, Hybrid and Fuel Cell Electric Vehicle Congress, Geneva, Switzerland, 2017.

## Version History

Introduced in R2024a