Unable to debug the problem in the code.

1 vue (au cours des 30 derniers jours)
Tutan Debnath
Tutan Debnath le 6 Sep 2019
Commenté : Tutan Debnath le 6 Sep 2019
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

Réponse acceptée

the cyclist
the cyclist le 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.
  1 commentaire
Tutan Debnath
Tutan Debnath le 6 Sep 2019
Thanks a lot Sir. It worked.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by