why does an "if" statement inside a MATlab function block in simulink not trigger?
이전 댓글 표시
I am trying to create a "switch" of sorts within my simulink model. I have a system that takes in 3 numbers, and if all 3 numbers are below a value, it will increase my counter (k) by 1. Once my counter reaches a value (lets say 5 for example), the block will change the value it is outputting.
The blocks in simulink are shown in the picture.
The code inside the MATLAB function is as follows:
function [R,k] = fcn(eR,eV,eA,k,rHold, rDock)
if k > 5
R = rDock;
else
R = rHold;
end
if (k <= 5)
if((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2))
k = k + 1;
end
end
end
댓글 수: 6
Walter Roberson
2018년 8월 2일
편집: Walter Roberson
2018년 8월 2일
I would recommend double-checking that k is a scalar.
assert(length(k) == 1)
and non-nan
assert(~isnan(k))
Johan Prent
2018년 8월 2일
Dennis
2018년 8월 2일
Can you provide a minimal not-working example? Are eR, eV and eA all small enough? Which if statement does not trigger?
I ran your funtion with some dummy inputs and it looked fine.
eR=0.01;
eV=0.01;
eA=0.01;
rDock=1;
rHold=2;
k=0;
for i=1:10
[R,k]=fcn(eR,eV,eA,k,rHold,rDock)
end
function [R,k] = fcn(eR,eV,eA,k,rHold, rDock)
if k > 5
R = rDock;
else
R = rHold;
end
if (k <= 5)
if((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2))
k = k + 1;
end
end
end
Johan Prent
2018년 8월 2일
Adam
2018년 8월 2일
What happens if you add
assert( ((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2)) )
at the top of the function?
Johan Prent
2018년 8월 3일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!