If statement should continue for some values even if "else" is true

1 回表示 (過去 30 日間)
Harald Hermansson
Harald Hermansson 2022 年 11 月 26 日
編集済み: Torsten 2022 年 11 月 26 日
I'm working with a Simulink model for a series hybrid vehicle. The statement I want to make is "if the battery percentage (SoC) drops below 35%, then turn on the engine and run it until it reaches 50%. But I don't know the way to make it continue to 50% because it's making the "else" statement true.
This is what I came up with so far, but this is of course not enough, should I implenent something like a "continue" statement?
function [w,T] = EnginePower(B_SoC)
if B_SoC < 0.4
T=12;
w=2500;
else
T=0;
w=0;
end

回答 (1 件)

Torsten
Torsten 2022 年 11 月 26 日
編集済み: Torsten 2022 年 11 月 26 日
You must have a flag which indicates whether the engine is on or off.
function [w,T] = EnginePower(B_SoC)
persistent on
if isempty(on)
on = 0;
end
T = 0;
w = 0;
if on == 0
if B_SoC < 0.35
T = 12;
w = 2500;
on = 1;
end
end
if on == 1
if B_SoC <= 0.5
T = 12;
w = 2500;
else
on = 0
end
end
end

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by