| for k = 1:N-1 |
| % 假设当前需求功率(可以根据实际情况调整) |
| P_req = rand * 100; % 随机需求功率,例如0到100kW |
| |
| % 计算电池和内燃机的功率分配 |
| % 目标是使等效燃油斲丧最小化 |
| % 等效燃油斲丧公式:Fuel_eq = P_engine/eta_engine + s_factor * (SOC(k) - SOC_ref) * P_battery/eta_battery_charge_or_discharge |
| % 此中,P_battery/eta_battery_charge_or_discharge 根据P_battery的正负选择充电或放电服从 |
| |
| % 初始推测 |
| P_battery_guess = (SOC_ref - SOC(k)) * 1000; % 假设一个简单的线性关系 |
| |
| % 迭代优化(这里使用简单的梯度降落法作为示例) |
| for iter = 1:100 |
| if P_battery_guess >= 0 |
| eta = eta_battery_discharge; |
| else |
| eta = eta_battery_charge; |
| end |
| |
| Fuel_eq = (P_req - P_battery_guess) / eta_engine + s_factor * (SOC(k) - SOC_ref) * P_battery_guess / eta; |
| |
| % 计算梯度(这里使用数值梯度) |
| P_battery_test = P_battery_guess + 0.01; |
| if P_battery_test >= 0 |
| eta_test = eta_battery_discharge; |
| else |
| eta_test = eta_battery_charge; |
| end |
| Fuel_eq_test = (P_req - P_battery_test) / eta_engine + s_factor * (SOC(k) - SOC_ref) * P_battery_test / eta_test; |
| |
| gradient = (Fuel_eq_test - Fuel_eq) / 0.01; |
| |
| % 更新推测值 |
| P_battery_guess = P_battery_guess - 0.01 * gradient; |
| |
| % 限定P_battery的范围 |
| P_battery_guess = max(min(P_battery_guess, P_req), -100); % 假设电池最大充放电功率为100kW |
| end |
| |
| % 更新电池功率和内燃机功率 |
| P_battery(k) = P_battery_guess; |
| P_engine(k) = P_req - P_battery(k); |
| |
| % 更新SOC |
| SOC(k+1) = SOC(k) + P_battery(k) * dt / (battery_capacity * eta_battery_charge_or_discharge); |
| % 留意:这里必要定义battery_capacity,而且根据P_battery的正负选择充电或放电服从 |
| if P_battery(k) >= 0 |
| eta_battery_charge_or_discharge = eta_battery_discharge; |
| else |
| eta_battery_charge_or_discharge = eta_battery_charge; |
| end |
| |
| % 计算等效燃油斲丧 |
| equivalent_fuel_consumption(k) = (P_engine(k) / eta_engine + s_factor * (SOC(k) - SOC_ref) * P_battery(k) / eta_battery_charge_or_discharge) * dt; |
| end |