Hello, if you have a moment, I could use your help in resolving this error.

조회 수: 3 (최근 30일)
  댓글 수: 1
Danikha Shyken
Danikha Shyken 2023년 10월 19일
편집: dpb 2023년 10월 20일
Below is the code:
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

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2023년 10월 20일
편집: Guillaume 2023년 10월 20일
Hello,
You should help the interpreter to determine the size of your variables. You can define the size in the data properties or in the code with unambiguous assignments. Your code could work like that :
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)
ind = inputVoltage == [0.3, 0.4, 1.2]; % avoid to repeat 3 times this "inputVoltage == [0.3, 0.4, 1.2]"
if any(ind) % easier
% 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;
else
error('Input voltage is out of the specified range.');
end
end

추가 답변 (1개)

dpb
dpb 2023년 10월 20일
inputVoltage=0.3;
[q1, q2, q3, q4]=buckBoostControl(inputVoltage)
q1 = 1
q2 = 0.8571
q3 = 0
q4 = 0.1429
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
Danikha Shyken
Danikha Shyken 2023년 10월 21일
All good now. I truly appreciate your help.
Walter Roberson
Walter Roberson 2023년 10월 21일
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() .

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Switches and Breakers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by