Hello, if you have a moment, I could use your help in resolving this error.
Show older comments
Accepted Answer
More Answers (1)
inputVoltage=0.3;
[q1, q2, q3, q4]=buckBoostControl(inputVoltage)
function [q1, q2, q3, q4] = buckBoostControl(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;
% Check input voltage to determine the mode (Boost or Buck)
if inputVoltage == 0.3 || inputVoltage == 0.4 || inputVoltage == 1.2
% Boost Mode
dutyCycle = dutyCycleBoost(inputVoltage == [0.3, 0.4, 1.2]);
delay = delayBoost(inputVoltage == [0.3, 0.4, 1.2]);
q1 = 1;
q2 = dutyCycle / 100;
q3 = 0;
q4 = 1 - q2;
elseif inputVoltage == 2
% Buck Mode
dutyCycle = dutyCycleBuck;
delay = delayBuck;
q1 = dutyCycle / 100;
q2 = 0;
q3 = 1 - q1;
q4 = 1;
else
error('Input voltage is out of the specified range.');
end
end
The function is ok in MATLAB although
if inputVoltage == 0.3 || inputVoltage == 0.4 || inputVoltage == 1.2
is very problematical in doing exact comparisons with floating point values; internal rounding of even the last significant digit away from the value converted from the text representation to internal representation will be sufficient for the comparison to fail when running and the inputVoltage value is not input manually as in the above.
More robust would be
BOOST_V=[0.3, 0.4, 1.2]; % put constants in variables so can change instead of in code
BUCK_V=2.0;
if any(ismembertol(inputVoltage,BOOST_V)); % check if any match
...
See ismembertol for details and how to set the "nearness" to a match for comparison; the above default will be at about 1E-12 or roughly three digits of precision of a double precision floating point value. How close to the actual values the computation will return will depend upon just how the inputVoltage value is calculated.
As for the syntax error, it appears that Simulink (I presume?) looks at code with a jaundiced eye and if it can't determine the shape of anything internally, it throws up its hands.
Try the following rearrangement of the code which is also just a little more efficient as it doesn't do the lookup indexing twice...
BOOST_V=[0.3, 0.4, 1.2]; % put constants in variables so can change instead of in code
BUCK_V=2.0;
ix=ismembertol(inputVoltage,BOOST_V)); % Check input voltage to determine the mode (Boost or Buck)
if any(ix) % found one matched...Boost Mode
ix=find(ix); % convert logical array to index for addressing
dutyCycle = dutyCycleBoost(ix);
delay = delayBoost(ix);
...
and see if that will satisfy...
2 Comments
Danikha Shyken
on 21 Oct 2023
Walter Roberson
on 21 Oct 2023
The concerns about == with a floating point number are important.
And what should happen if the input voltage is (for example) 0.5 ?
Perhaps you should consider using interp1() .
Categories
Find more on Standards, Guidelines, and Block Usage 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!