sysid blog

Modelling Battery Charging

May 08, 2022 ☕️ 3 min read


Modelling non-linear battery behavior with piecwise linear expressions

Modelling time dependent charging behavior of batteries is not new. The rise of global EV fleets makes it an interesting topic.

In the context of EV charging the most important charging objective is to have the car be charged to a target SOC (state of charge) at a pre-determined point in time, a.k.a. departure time.

This forms an important time constraint for the system.

The mathematical model of the charging and discharging behavior of a battery is straight forward.



H:  Time Horizon t(1..50)H: \ \text{ Time Horizon }\\ t \in (1..50)\\


pmin=25kWpmax=25kWemin=0kWhemax=100kWhestart=10kWhetgt=100kWhplugin=5 (number of 15min interval) plugout=48 (number of 15min interval) ndischarge=1 (number of discharge events allowed)p_{min} = -25kW\\ p_{max} = 25kW\\ e_{min} = 0kWh\\ e_{max} = 100kWh\\ e_{start} = 10kWh\\ e_{tgt} = 100kWh\\ plugin = 5 \text{ (number of 15min interval) }\\ plugout = 48 \text{ (number of 15min interval) }\\ n_{discharge} = 1 \text{ (number of discharge events allowed)}\\


etR+pt=pt+ptzt={1,  p<0 0,  p>0 dt={1,  discharging starts at begin of interval 0,  else e_t \in R^+\\ p_t = p_t^+ - p_t^- \\ z_{t} = \begin{cases} 1, \ \text{ $p \lt 0$ }\\ 0, \ \text{ $p \gt 0$ }\\ \end{cases}\\ d_t = \begin{cases} 1, \ \text{ discharging starts at begin of interval }\\ 0, \ \text{ else }\\ \end{cases}



pt+,pt0ptpminpt+pmaxpminptpmaxp_t^+, p_t^- \ge 0 \\ p_t^- \le -p_{min}\\ p_t^+ \le p_{max} \\ p_{min} \le p_t \le p_{max}\\


eminetemaxeplugin=Epluginet=et1+14pt1e_{min} \le e_t \le e_{max}\\ e_{plugin} = E_{plugin}\\ e_t = e_{t-1} + \frac{1}{4} p_{t-1}\\

All optimization strategies need to ensure, that the car is ready when needed.

et>=etgttplugoute_t >= e_{tgt} \qquad \forall t \geq plugout\\

Optimization Objectives

It is interesting to look at different optimization objectives:

  1. Time Optimization: (earliest possible, latest possible charging)
  2. Feasibility: Car can be charged within available time
  3. Cost: Car will be charge at minimum cost

Battery Profile

Batteries have non-linear charging behavior. Especially high-power charging can show interesting profiles. Here is a simplified charging and discharging profile depending on the energy in the battery (SOC). The closer the SOC gets to 100% the lower the charging power will be.

This phenomenon is also called degradation.

Step function as non-linearity


It is a well known modelling technique to approximate non-linear functions with piecewise linear functions.

Pyomo has out-of-the box support for this.

The above’s charging profile (upper boundary) can for example be modelled with this expression:

    # linearize non-linear P(E) battery behavior
    model.pw_constraint_charging = Piecewise(
        block.A, block.p_plus, block.e,
        pw_constr_type='UB',  # upper bound for power
        pw_repn='CC',  # algorithm selection

This allows for almost arbitrary non-linear battery profiles and is limited only by the number for generated binary variables and associated solver capability.


I solve here a very simple battery model over a charging period of 50x15min time intervals. The lower bound of the blue area delineates the plugin SOC and the upper bound is the target SOC.

The red area on the P(E) diagram defines the allowed power values, depending on battery energy.

Departure time is assumed to be t=46t=46 and we want to leave with full battery etgt=100e_{tgt}=100, of course.

Fast Charging


Late Charging


Cost Optimized

This scenario allows discharging the battery and selling the energy to the grid operator while still having the car ready at departure time.


Under the totally unrealistic assumption that we can sell energy for twice as much as we buy it, we can see that the battery discharges twice to the allowed minimum of 30% SOC and charges up again.

However, this scenario becomes more realistic when there are time periods where charging and discharging cost do have a real spread.


Using piecewise linear functions as approximation of complex battery charging profiles can be a valid solution for modelling non-trivial charging behavior. Pyomo supports here with built-in capabilities and makes our lives as modellers bearable.

always be building, by sysid.

© 2022