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

조회 수: 2(최근 30일)
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

범주

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by