Here is the code:
function [q1, q2, q3, q4] = buckBoostController(inputVoltage)
% Constants for duty cycle and delay values
dutyCycleBoost = [85.71, 81.82, 60.00];
delayBoost = [51.444, 65.448, 144.000];
dutyCycleBuck = 47.37;
delayBuck = 170.532;
% Define the battery voltage
batteryVoltage = 1.8;
% Check input voltage to determine the mode (Boost, Buck, or Battery Voltage)
ind = inputVoltage == [0.3, 0.4, 1.2]; % Avoid repeating "inputVoltage == [0.3, 0.4, 1.2]"
if any(ind)
% Boost Mode
dutyCycle = dutyCycleBoost(ind);
delay = delayBoost(ind);
q1 = 1;
q2 = dutyCycle(1) / 100; % Add (1) to clearly define size
q3 = 0;
q4 = 1 - q2;
elseif inputVoltage == 2
% Buck Mode
dutyCycle = dutyCycleBuck;
delay = delayBuck;
q1 = dutyCycle / 100;
q2 = 0;
q3 = 1 - q1;
q4 = 1;
elseif inputVoltage == batteryVoltage
% Battery Voltage Mode
q1 = 1;
q2 = 0;
q3 = 0;
q4 = 0;
% Optionally, you can set dutyCycle and delay for this mode if needed.
else
error('Input voltage is out of the specified range.');
end
end
Can someone please help me improve this code to make it work as intended?