Unable to debug the problem in the code.

1 回表示 (過去 30 日間)
Tutan Debnath
Tutan Debnath 2019 年 9 月 6 日
コメント済み: Tutan Debnath 2019 年 9 月 6 日
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

採用された回答

the cyclist
the cyclist 2019 年 9 月 6 日
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 件のコメント
Tutan Debnath
Tutan Debnath 2019 年 9 月 6 日
Thanks a lot Sir. It worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by