Unable to debug the problem in the code.

1 view (last 30 days)
The following code is written in Simlink matlab fuction. I am unable to debug the problem with this code at time 1.5e-4 and so on, Here my simulation step time is 1e-5.
function v_tri1 = fcn(time,delta_t)
persistent v_tri flag supply_freq v_dc carrier_freq carrier_time_period
if isempty(v_tri)
v_tri = 0;
flag = 0;
v_dc = 200;
supply_freq = 50;
carrier_freq = 10000;
carrier_time_period = 1/carrier_freq;
end
% delta_t = 1e-5;
%Carrier freq
if (time-flag*carrier_time_period) <= (carrier_time_period)/2
v_tri = v_tri + 2*carrier_freq*delta_t;
else
v_tri =v_tri - 2*carrier_freq*delta_t;
end
if (time-flag*carrier_time_period)== carrier_time_period
flag =flag+1;
end
v_tri1 = v_tri;
end

Accepted Answer

the cyclist
the cyclist on 6 Sep 2019
I'm guessing it's a problem with floating-point precision when you do this comparison:
(time-flag*carrier_time_period)== carrier_time_period
See, for example, this answer, for lots of detail.
Instead of testing for exact equality, test for equality within some tolerance. See this question/answer for help with that.

More Answers (0)

Categories

Find more on Simulink 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!