当另一个值变号时重置值

0 投票
1 回答
43 浏览
提问于 2025-04-14 18:28

我有一个电池在恒流充电和放电时的数据集。数据大概是这样的:

time = np.arange(0,100,1)
current_discharge = [-2 for i in np.arange(0,50,1)]
current_charge = [2 for i in np.arange(50,100,1)]
current = current_discharge + current_charge

我想计算绝对充电量(绝对值(时间*电流))与时间的关系。为此,当电流从负值变为正值时(也就是在50的时候),我需要把充电量重置为零。

在我下面的代码中,它在50之后从累计充电量中减去一些值。其实我想要的是在50的时候累计充电量变为零,然后之后再增加。

cumsum = 0
dQ_list = []
totalcharge = []

for i, t in zip(current,time):
    dQ = -(t+1-t)*i
    dQ_list.append(dQ)
    cumsum += dQ
    totalcharge.append(cumsum)

fig = plt.figure()
ax1 = plt.axes((0,0,1,0.5))
ax1.plot(time,current, label = "current vs time")
ax1.plot(time,totalcharge, label = "charge vs time")
ax1.set_xlabel("Time")

ax1.legend()

1 个回答

0

我不太确定我是否完全理解了,但我觉得这个是你需要的:

charge_started = False  # Flag to track if charge accumulation started

for i, t in zip(current, time):
    dQ = -(t + 1 - t) * i
    dQ_list.append(dQ)
    if i < 0:  # Check if current is negative, while the current retreives
               # from current_discharge, it behaves as it was in your code
        cumsum += dQ
        charge_started = True
    elif charge_started:
        cumsum = 0  # Reset cumsum to 0 when current becomes positive, it goes
                    # here only once
        charge_started = False
    else:
        cumsum += -dQ # all the other current, i put a - because you told you
                      # wanna see the current increase, you can adjust it as 
                      # you wish
    totalcharge.append(cumsum)

撰写回答