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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by