Unable to debug the problem in the code.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
Instead of testing for exact equality, test for equality within some tolerance. See this question/answer for help with that.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!