Clear Filters
Clear Filters

How to code a discrete rotating switch from simulink into matlab

6 views (last 30 days)
Hi, I'm new to matlab and I'm making a basic fuel system. I want to be able to control the refuel states with a discrete rotating selector switch with 5 states: refuel, refuel2desired, off, defuel2desired. Whenever I run the model I get an algabraic loop error and I don't know why. Either it's this section of code or I've done something wrong in the simulink model.
Is this code correct or is there a better way of writing it?
Thank you!
CurrentFuel = (Calculated in other section of code)
DesiredFuelQuantity = (user input)
MaxQuantity = 5180;
MinQuantity = 50;
while (RefuelSwitch == 1) && (LThrottle == 0) && (RThrottle == 0) && (EngineOnSwitch == 0)
if (RefuelState == 0)
BOpen = 0;
else
%Refuel
if (RefuelState == 1)
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
elseif (RefuelState == 1) && (CurrentFuel == MaxQuantity)
BOpen = 0;
end
%Refuel to Desired Quantity
if (RefuelState == 2)
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
elseif (RefuelState == 2) && (CurrentFuel == DesiredFuelQuantity)
BOpen = 0;
end
%Defuel to Desired Quantity
if (RefuelState == 3)
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
elseif (RefuelState == 2) && (CurrentFuel == DesiredFuelQuantity)
BOpen = 0;
end
%Defuel
if (RefuelState == 3)
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
elseif (RefuelState == 2) && (CurrentFuel <= MinQuantity)
BOpen = 0;
end
end
end

Accepted Answer

Amish
Amish on 15 Dec 2023
Hi,
As I can understand from the description, you are facing an Algebraic Loop error. You are not sure whether you have an issue with the logic of your implementation in MATLAB or the Simulink model that you have along with it.
I can spot the issues with the logical implementation that you have provided. The conditions for checking “RefuelState” and “CurrentFuel” are not appropriately structured and some of the conditions are being checked twice, which is not necessarily required.
Here is how I would suggest you the implementation of your logic:
  • Use switch-case statements
  • Remove the repeated conditions
  • Restructure the code to handle each state separately.
CurrentFuel = % (Calculated in other section of code)
DesiredFuelQuantity = % (user input)
MaxQuantity = 5180;
MinQuantity = 50;
while (RefuelSwitch == 1) && (LThrottle == 0) && (RThrottle == 0) && (EngineOnSwitch == 0)
switch RefuelState
case 1 % Refuel
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
if CurrentFuel >= MaxQuantity
BOpen = 0;
end
case 2 % Refuel to Desired Quantity
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
if CurrentFuel >= DesiredFuelQuantity
BOpen = 0;
end
case 3 % Defuel to Desired Quantity
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
if CurrentFuel <= DesiredFuelQuantity
BOpen = 0;
end
case 4 % Defuel
BOpen = 1;
LIOpen = 1;
RIOpen = 1;
if CurrentFuel <= MinQuantity
BOpen = 0;
end
otherwise
BOpen = 0;
LIOpen = 0;
RIOpen = 0;
end
end
The cause of the algebraic loop error might not be due to this code specifically, but rather due to its interaction with the Simulink model. One of the recommendations would be trying to add a unit delay block at the exact location of the error in the Model.
You can have a look at Jiang’s answer that suggests ways for solving generic error cases of algebraic loops: https://www.mathworks.com/matlabcentral/answers/14383-error-trouble-solving-algebraic-loop-cant-solve-it#answer_19545
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!