I am trying to create a simple battery model in Python based on the RC circuit from, http://firsttimeprogrammer.blogspot.com/2015/07/electric-circuits-101-rc-and-rl-circuits.html. I have attached figures of what I would ideally want the output to look like using my equations.
I know battery modeling is a large subject, but I want to make a simple lithium ion battery model based off a RC circuit where I can decide when the battery charges and discharges.
I am wondering if my equations are correct for what I am trying to do (the equations are in the code below). Right now all my equations are independent and only rely on time, but I am wondering if the current and voltage across the capacitor equations for charging/discharging should somehow be dependent. I am not taking into account factors like heat, cycling, etc. This is supposed to be a simple model with just charging and discharging.
Below are some examples of plots I found that I would like my code output to look like. When the battery is "used" it discharges and the voltage level in the battery decreases, when the battery charges, you see the corresponding current and the voltage reaching its full capacity.
You will see in the code I have 3 equations that correspond to the "battery", which are for charging (
#Charge Equations), and 3 equations for the "battery" that correspond to discharging (
#Discharge Equations). These are the equations I am curious about.
import numpy as np import matplotlib.pyplot as plt plt.style.use('ggplot') #capacitor (battery) c = 100 * 10**(-6) #farad #voltage source vs = 5 #volts #resistor r = 2000 #ohms #time t = np.linspace(0,1,1000) #Charge Equations q = c*vs*(1-np.exp((-1/(r*c))*t)) #charge i = (vs/r)*np.exp((-1/(r*c))*t) #battery current vc = vs*(1-np.exp((-1/(r*c))*t)) #battery voltage #Discharge Equations qd = c*vs*(np.exp((-1/(r*c))*t)) #discharge id = (vs/r)*np.exp((-1/(r*c))*t) #battery current vd = vs*(np.exp((-1/(r*c))*t)) #battery voltage plt.plot([0,t[-1]],[c*vs,c*vs],label='Charge peak') plt.plot(t,q,label='Charge of the capacitor (C)') plt.plot(t,i,label='Current (A)') plt.plot(t,vc,label='Voltage across capacitor') print('Tau',1/(r*c)) print('Peak current (A)',vs/r) plt.xlabel('Time (s)') plt.title('Battery') plt.legend() plt.show()
The code is not finished, however, when done the basic idea is that when I run the code, I can specify when I want the battery to charge and when I want it to discharge during a simulated amount of time.
To summarize, here are my main questions and concerns:
- Are my equations correct for what I am trying to do?
- Should the equations somehow be dependent on each other?
- Should I be using a different set of equations when trying to simulate my lithium ion battery?
Any advice, or suggestions, would be greatly appreciated. If anything seems unclear, or needs further explaining, please let me know.
Sources for pictures
First picture: https://www.dnkpower.com/why-18650-battery-explode/