If statement should continue for some values even if "else" is true
1 次查看(过去 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
0 个评论
回答(1 个)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!