# SOC Estimator (Adaptive Kalman Filter)

State-of-charge and terminal resistance estimator with adaptive Kalman filter

Since R2022b

• Libraries:
Simscape / Battery / BMS / Estimators

## Description

This block implements an estimator that calculates the state-of-charge (SOC) and the terminal resistance of a battery by using the Kalman filter algorithms. The terminal resistance, R0, is a state of the estimator.

The SOC is defined as the ratio of the released capacity Creleasable to the rated capacity Crated. Manufacturers provide the value of the rated capacity of each battery, which represents the maximum amount of charge in the battery:

`$SOC=\frac{{C}_{releasable}}{{C}_{rated}}.$`

This block supports both single-precision and double-precision floating-point simulation.

Note

To enable single-precision floating-point simulation, the data type of all inputs and parameters, except for the Sample time (-1 for inherited) parameter, must be `single`.

For continuous-time simulation, set the Filter type parameter to `Extended Kalman-Bucy filter` or ```Unscented Kalman-Bucy filter```.

Note

Continuous-time implementation of this block works only in double-precision floating-point simulation. If you provide single-precision floating-point parameters and inputs, this block casts them to double-precision floating-point values to prevent errors.

For discrete-time simulation, set the Filter type parameter to `Extended Kalman filter` or ```Unscented Kalman filter``` and the Sample time (-1 for inherited) parameter to a positive value or `-1`.

### Equations

This figure shows the equivalent circuit for a battery with one time-constant dynamics: The equations for the equivalent circuit, with the terminal resistance R0 as an additional state, are:

`$\begin{array}{l}\frac{dSOC}{dt}=-\frac{i}{3600AH\left(T\right)}\\ \frac{d{V}_{1}}{dt}=\frac{i}{{C}_{1}\left(SOC,T\right)}-\frac{{V}_{1}}{{R}_{1}\left(SOC,T\right){C}_{1}\left(SOC,T\right)}\\ \frac{d{R}_{0}}{dt}=0\\ {V}_{t}={V}_{0}\left(SOC,T\right)-i{R}_{0}-{V}_{1}\end{array}$`

where

• SOC is the state-of-charge.

• i is the current.

• V0 is the no-load voltage.

• Vt is the terminal voltage.

• AH is the ampere-hour rating.

• R1 is the first polarization resistance.

• C1 is the parallel RC capacitance.

• T is the temperature.

A time constant, τ1, for the parallel section relates the first polarization resistance R1 and the parallel RC capacitance C1 using the relationship ${C}_{1}={\tau }_{1}/{R}_{1}.$

For the Kalman filter algorithms, the block uses this state and these process and observation nonlinear functions:

`$\begin{array}{l}x={\left[\begin{array}{ccc}SOC& {V}_{1}& {R}_{0}\end{array}\right]}^{T}\\ f\left(x,i\right)=\left[\begin{array}{c}-\frac{i}{3600AH\left(T\right)}\\ \frac{i}{{C}_{1}\left(SOC,T\right)}-\frac{{V}_{1}}{{R}_{1}\left(SOC,T\right){C}_{1}\left(SOC,T\right)}\\ 0\end{array}\right]\\ h\left(x,i\right)={V}_{0}\left(SOC,T\right)-i{R}_{0}-{V}_{1}\end{array}$`

Extended Kalman Filter

This diagram shows the structure of the extended Kalman filter (EKF): The EKF technique relies on a linearization at every time step to approximate the nonlinear system. To linearize at every time step, the algorithm computes these Jacobians online:

`$\begin{array}{l}F=\frac{\partial f}{\partial x}\\ H=\frac{\partial h}{\partial x}\end{array}$`

The EKF is a discrete-time algorithm. After the discretization, the Jacobians for the SOC estimation of the battery are:

`$\begin{array}{l}{F}_{d}=\left[\begin{array}{ccc}1& 0& 0\\ 0& {e}^{\frac{-{T}_{S}}{{R}_{1}{C}_{1}}}& 0\\ 0& 0& 1\end{array}\right]\\ {H}_{d}=\left[\begin{array}{ccc}\frac{\partial {V}_{OC}}{\partial SOC}& -1& -i\end{array}\right]\end{array}$`

where TS is the sample time.

The EKF algorithm comprises these phases:

• Initialization

• $\stackrel{^}{x}\left(0|0\right)$⁠— State estimate at time step 0 using measurements at time step 0.

• $\stackrel{^}{P}\left(0|0\right)$⁠— State estimation error covariance matrix at time step 0 using measurements at time step 0.

• Prediction

• Project the states ahead (a priori):

`$\stackrel{^}{x}\left(k+1|k\right)=f\left(\stackrel{^}{x}\left(k|k\right),i\right).$`
• Project the error covariance ahead:

`$\stackrel{^}{P}\left(k+1|k\right)={F}_{d}\left(k\right)\stackrel{^}{P}\left(k|k\right){F}_{d}^{T}\left(k\right)+Q,$`

where Q is the covariance of the process noise.

• Correction

• Compute the Kalman gain:

`$K\left(k+1\right)=\stackrel{^}{P}\left(k+1|k\right){H}_{d}^{T}\left(k\right){\left({H}_{d}\left(k\right)\stackrel{^}{P}\left(k+1|k\right){H}_{d}^{T}\left(k\right)+R\right)}^{-1},$`

where R is the covariance of the measurement noise.

• Update the estimate with the measurement y(k) (a posteriori):

`$\stackrel{^}{x}\left(k+1|k+1\right)=\stackrel{^}{x}\left(k+1|k\right)+K\left(k+1\right)\left({V}_{t}\left(k\right)-h\left(\stackrel{^}{x}\left(k|k\right),i\right)\right).$`

• Update the error covariance:

`$\stackrel{^}{P}\left(k+1|k+1\right)=\left(I-K\left(k+1\right){H}_{d}\right)\stackrel{^}{P}\left(k+1|k\right).$`

Extended Kalman-Bucy Filter

This diagram shows the structure of the extended Kalman-Bucy filter (EKBF): The EKBF is the continuous-time variant of the Kalman filter. In continuous-time, the prediction and correction steps are coupled. The EKBF algorithm comprises these phases:

• Initialization

• $\stackrel{^}{x}\left({t}_{0}\right)$⁠— State estimate at time t0.

• $\stackrel{^}{P}\left({t}_{0}\right)$⁠— State estimation error covariance matrix at time t0.

• Prediction-Correction EKBF algorithm

`$\begin{array}{l}K\left(t\right)=P\left(t\right){H}^{T}\left(t\right){R}^{-1}\left(t\right)\\ \frac{d\stackrel{^}{x}\left(t\right)}{dt}=f\left(\stackrel{^}{x}\left(t\right),i\left(t\right)\right)+K\left(t\right)\left({V}_{t}\left(t\right)-h\left(\stackrel{^}{x}\left(t\right),i\left(t\right)\right)\right)\\ \frac{dP\left(t\right)}{dt}=F\left(t\right)P\left(t\right)+P\left(t\right){F}^{T}\left(t\right)+Q\left(t\right)-K\left(t\right)H\left(t\right)P\left(t\right)\end{array}$`

where:

`$\begin{array}{l}F\left(t\right)=\frac{\partial f}{\partial x}\\ H\left(t\right)=\frac{\partial h}{\partial x}\end{array}$`

Unscented Kalman Filter

This diagram shows the structure of the unscented Kalman filter (UKF): The EKF locally approximates nonlinear functions with the linear equations obtained from the Taylor expansion by using only the first term of the expansion. In a highly nonlinear system, these solutions are not very accurate.

The UKF uses nonlinear transformations on a set of sigma points that the algorithm chooses deterministically. This technique is called unscented transformation. The mean and the covariance matrix of the transformed points are accurate to the second order of the Taylor series expansion.

The UKF algorithm follows these steps:

• Initialization

• $\stackrel{^}{x}\left(0|0\right)$⁠— State estimate at time step 0 using measurements at time step 0.

• $\stackrel{^}{P}\left(0|0\right)$⁠— State estimation error covariance matrix at time step 0 using measurements at time step 0.

• Generate sigma points and calculate the mean weight and covariance weight for each point.

• Choose the sigma points, x(i)(k|k)

`$\begin{array}{l}{x}^{\left(i\right)}\left(k|k\right)=\left\{\begin{array}{cc}\stackrel{^}{x}\left(k+1|k\right)& i=1\\ \stackrel{^}{x}\left(k+1|k\right)+{\left(\sqrt{\left(n+\lambda \right)P\left(k|k\right)}\right)}_{i}& i=2,\dots ,n+1\\ \stackrel{^}{x}\left(k+1|k\right)-{\left(\sqrt{\left(n+\lambda \right)P\left(k|k\right)}\right)}_{i}& i=n+2,\dots ,2n+1\end{array}\\ {W}_{m}^{\left(i\right)}=\left\{\begin{array}{cc}\frac{\lambda }{n+\lambda }& i=1\\ \frac{1}{2\left(n+\lambda \right)}& i\ne 1\end{array}\\ {W}_{c}^{\left(i\right)}=\left\{\begin{array}{cc}\frac{\lambda }{n+\lambda }+\left(1-{\alpha }^{2}+\beta \right)& i=1\\ \frac{1}{2\left(n+\lambda \right)}& i\ne 1\end{array}\end{array}$`

where

• n is the dimension of the state vector x.

• $\lambda ={\alpha }^{2}\left(n+\kappa \right)-n,\text{\hspace{0.17em}}\alpha \in \left[0,1\right]$ describes the distance between the sigma point and the mean point. In a normal distribution, β = 2 and κ = 0.

• ${\left(\sqrt{\left(n+\lambda \right)P}\right)}_{i}$ is the i-th row or column of $\sqrt{cP}$. The block calculates the matrix square root by using numerically efficient and stable methods such as the Cholesky decomposition.

• First estimation of the system state matrix:

`$\begin{array}{l}{\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right)=f\left({\stackrel{^}{x}}^{\left(i\right)}\left(k|k\right),i\left(k\right)\right)\\ \stackrel{^}{x}\left(k+1|k\right)=\sum _{i=1}^{2n+1}{W}_{m}^{\left(i\right)}{\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right)\end{array}$`

• First estimation of the covariance matrix of the state variables:

`$P\left(k+1|k\right)=\sum _{i=1}^{2n+1}{W}_{c}^{\left(i\right)}\left({\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right)-\stackrel{^}{x}\left(k+1|k\right)\right)\cdot {\left({\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right)-\stackrel{^}{x}\left(k+1|k\right)\right)}^{T}+Q$`

• Estimation of the measured variables:

`$\begin{array}{l}{V}_{t}^{\left(i\right)}\left(k+1|k\right)=h\left({\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right),i\left(k\right)\right)\\ {\stackrel{^}{V}}_{t}\left(k+1|k\right)=\sum _{i=1}^{2n+1}{W}_{m}^{\left(i\right)}{\stackrel{^}{V}}_{t}^{\left(i\right)}\left(k+1|k\right)\end{array}$`

• Estimation of the covariance of the measurement (Py) and covariance between the measurement and the state (Pxy):

`$\begin{array}{l}{P}_{y}=\sum _{i=1}^{2n+1}{W}_{c}^{\left(i\right)}\left({\stackrel{^}{V}}_{t}^{\left(i\right)}\left(k+1|k\right)-{\stackrel{^}{V}}_{t}\left(k+1|k\right)\right)\cdot {\left({\stackrel{^}{V}}_{t}^{\left(i\right)}\left(k+1|k\right)-{\stackrel{^}{V}}_{t}\left(k+1|k\right)\right)}^{T}+R\\ {P}_{xy}=\sum _{i=1}^{2n+1}\left({\stackrel{^}{x}}^{\left(i\right)}\left(k+1|k\right)-\stackrel{^}{x}\left(k+1|k\right)\right)\cdot {\left({\stackrel{^}{V}}_{t}^{\left(i\right)}\left(k+1|k\right)-{\stackrel{^}{V}}_{t}\left(k+1|k\right)\right)}^{T}\end{array}$`

• Kalman filter gain:

`$K\left(k+1\right)={P}_{xy}{P}_{y}^{-1}$`

• Second update of the state matrix and of the covariance of the state variables:

`$\begin{array}{l}\stackrel{^}{x}\left(k+1|k+1\right)=\stackrel{^}{x}\left(k+1|k\right)+K\left(k+1\right)\left({V}_{t}\left(k+1\right)-{\stackrel{^}{V}}_{t}\left(k+1|k\right)\right)\\ P\left(k+1|k+1\right)=P\left(k+1|k\right)-K\left(k+1\right){P}_{y}{K}^{T}\left(k+1\right)\end{array}$`

Unscented Kalman-Bucy Filter

This diagram shows the structure of the unscented Kalman-Bucy filter (UKBF): The derived continuous-time filtering equations of the UKBF are similar to the EKBF equations.

Because the UKF uses matrix square roots in its sigma points, the algorithm obtains the square-root version of the UKBF by formulating the filter as a differential equation for the sigma points. The equations for the square-root UKBF are:

`$\begin{array}{l}K\left(t\right)=X\left(t\right)W{h}^{T}\left(X\left(t\right),t\right){R}^{-1}\left(t\right)\\ M\left(t\right)={A}^{-1}\left(t\right)\left[X\left(t\right)W{f}^{T}\left(X\left(t\right),t\right)+f\left(X\left(t\right),t\right)W{X}^{T}\left(t\right)+Q\left(t\right)-K\left(t\right)R\left(t\right){K}^{T}\left(t\right)\right]{A}^{-T}\left(t\right)\\ \frac{d{X}_{i}\left(t\right)}{dt}=f\left(X\left(t\right),t\right){w}_{m}+K\left(t\right)\left({V}_{t}\left(t\right)-h\left(X\left(t\right),t\right){w}_{m}\right)+\sqrt{c}{\left[\begin{array}{ccc}0& A\left(t\right)\Phi \left(M\left(t\right)\right)& -A\left(t\right)\Phi \left(M\left(t\right)\right)\end{array}\right]}_{i}\end{array}$`

where

• $X\left(t\right)=\left[\begin{array}{ccc}m\left(t\right)& \dots & m\left(t\right)\end{array}\right]+\sqrt{c}\left[\begin{array}{ccc}0& A\left(t\right)& -A\left(t\right)\end{array}\right]$ is the sigma-point matrix.

• ${\Phi }_{ij}\left(M\left(t\right)\right)=\left\{\begin{array}{cc}{M}_{ij}\left(t\right),& i>j\\ 0.5{M}_{ij}\left(t\right),& i=j\\ 0,& i is a function that returns the lower diagonal part of the argument .

• ${w}_{m}={\left[\begin{array}{ccc}{W}_{m}^{\left(1\right)}& \cdots & {W}_{m}^{\left(2n+1\right)}\end{array}\right]}^{T}$

• $W=\left(1-\left[\begin{array}{ccc}{w}_{m}& \cdots & {w}_{m}\end{array}\right]\right)diag\left(\begin{array}{ccc}{W}_{c}^{\left(1\right)}& \cdots & {W}_{c}^{\left(2n+1\right)}\end{array}\right){\left(I-\left[\begin{array}{ccc}{w}_{m}& \cdots & {w}_{m}\end{array}\right]\right)}^{T}$

• $c={\alpha }^{2}\left(n+\kappa \right)$

• ${\left[\begin{array}{ccc}0& A\left(t\right)\Phi \left(M\left(t\right)\right)& -A\left(t\right)\Phi \left(M\left(t\right)\right)\end{array}\right]}_{i}$ is the i-th column of the argument matrix.

## Assumptions and Limitations

• Process and sensor noises are independent, zero-mean, Gaussian noises.

• Battery nominal capacity does not consider aging.

## Ports

### Input

expand all

Battery pack current, in ampere, specified as a scalar.

Cell voltage, in volt, 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 CellTemperature, InitialSOC, and InitialR0 input ports.

Cell temperature, 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 CellVoltage, InitialSOC, and InitialR0 input ports.

Initial state-of-charge, 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 CellVoltage, CellTemperature, and InitialR0 input ports.

Initial terminal resistance, specified as a scalar or a vector. The size of this input port must be equal to the size of the CellVoltage, CellTemperature, and InitialSOC input ports.

### Output

expand all

State-of-charge of the battery, returned as a scalar or a vector. The size of this output port is equal to the size of the CellVoltage, CellTemperature, InitialSOC, and InitialR0 input ports.

Terminal resistance, returned as a scalar or a vector. The size of this output port is equal to the size of the CellVoltage, CellTemperature, InitialSOC, and InitialR0 input ports.

## Parameters

expand all

### Main

Type of Kalman filter that this block uses to estimate the battery state-of-charge and the terminal resistance.

Coefficient that controls the spread of the sigma points. The block uses this parameter in its implementation of the equations for the unscented Kalman filter and the Unscented Kalman-Bucy filter.

#### Dependencies

To enable this parameter, set Filter type to `Unscented Kalman filter` or `Unscented Kalman-Bucy filter`.

Coefficient related to the distribution. The block uses this parameter in its implementation of the unscented Kalman filter and the Unscented Kalman-Bucy filter.

#### Dependencies

To enable this parameter, set Filter type to `Unscented Kalman filter` or `Unscented Kalman-Bucy filter`.

Coefficient that controls the spread of the sigma points. The block uses this parameter in its implementation of the equations for the unscented Kalman filter and the Unscented Kalman-Bucy filter.

#### Dependencies

To enable this parameter, set Filter type to `Unscented Kalman filter` or `Unscented Kalman-Bucy filter`.

Covariance matrix of the noise in the states.

Covariance matrix of the noise in the measurements.

Covariance matrix of the initial state error. This parameter defines the deviation in the initialization of the state.

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 allows you to switch between continuous operation and discrete operation, 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.

#### Dependencies

To enable this parameter, set Filter type to `Extended Kalman filter` or `Unscented Kalman filter`.

### System Model

Vector of the state-of-charge breakpoints defining the points at which you specify lookup data. The entries of this vector must be in strictly ascending order. The block calculates the state-of-charge value with respect to the nominal battery capacity specified in the Cell capacity, AH (A*Hr) parameter. The SOC is the ratio of the available battery charge qbattery and the nominal battery capacity qnom(T,n). You must make sure that, for each temperature, an SOC of 1 represents the respective battery charge capacity specified in the Cell capacity, AH (A*Hr) parameter when you model a fresh battery with a number of cycles N equal to 1 and δAH(n = 1, Tfade) = 0.

`$\begin{array}{c}SOC=\frac{{q}_{battery}}{{q}_{nom}\left(T,n\right)}\\ for\text{\hspace{0.17em}}N=1\text{\hspace{0.17em}}and\text{\hspace{0.17em}}{\delta }_{AH}\left(n,{T}_{fade}\right)=0,\text{\hspace{0.17em}}\text{\hspace{0.17em}}{q}_{nom}\left(T,n\right)=AH.\end{array}$`

Vector of temperature breakpoints defining the points at which you specify lookup data. This vector must be strictly ascending and greater than `0` `K`. The physical unit of this parameter must be the same as the physical unit of the CellTemperature input port.

Lookup data, in ohm, for the first parallel RC resistance at the specified SOC and temperature breakpoints. The number of rows of this matrix is equal to the size of the Vector of state-of-charge values, SOC (-) parameter. The number of columns of this matrix is equal to the size of the Vector of temperatures, T parameter.

Lookup data, in second, for the first parallel RC time constant at the specified SOC and temperature breakpoints. The number of rows of this matrix is equal to the size of the Vector of state-of-charge values, SOC (-) parameter. The number of columns of this matrix is equal to the size of the Vector of temperatures, T parameter.

Lookup data, in volt, for open-circuit voltages across the fundamental battery model at the specified SOC. The number of rows of this matrix is equal to the size of the Vector of state-of-charge values, SOC (-) parameter. The number of columns of this matrix is equal to the size of the Vector of temperatures, T parameter.

Cell capacity of the battery, in A*Hr . The block calculates the state of charge by dividing the accumulated charge by this value. The block calculates the accumulated charge by integrating the battery current.

 Grewal, Mohinder S., and Angus P. Andrews. Kalman Filtering: Theory and Practice Using MATLAB. 2nd ed. New York: Wiley, 2001.

 Van der Merwe, R., and E. A. Wan, The Square-Root Unscented Kalman Filter for State and Parameter-Estimation. 2001 IEEE International Conference on Acoustics, Speech, and Signal Processing. Proceedings (Cat. No.01CH37221), vol. 6, IEEE, 2001, pp. 3461–64.

 Sarkka, Simo On Unscented Kalman Filtering for State Estimation of Continuous-Time Nonlinear Systems. IEEE Transactions on Automatic Control, vol. 52, no. 9, pp. 1631-1641, Sept. 2007, doi: 10.1109/TAC.2007.904453.

 Huria, Tarun, Massimo Ceraolo, Javier Gazzarri, and Robyn Jackey. Simplified Extended Kalman Filter Observer for SOC Estimation of Commercial Power-Oriented LFP Lithium Battery Cells.2013, pp. 2013-01–1544.