how to check weather the values are constant or fluctuating over a period of time and give a output in one or zeros in a simulink model

조회 수: 2 (최근 30일)
in the above image the values are changing between 1 and 0 this is because vales are not in an increasing order
in the above image the values are constant 1 ( for 50 seconds)
i want to develop a model that can read the values till the 50 seconds and return a value of 1 if the values are flautuating like image 1 and 0 if the values remain constant (1) for fifty seconds

답변 (1개)

Manish
Manish 2025년 1월 2일
편집: Manish 2025년 1월 2일
Hi abhijeet.
I understand that you want to check whether values are constant or fluctuating during the first 50 seconds and output a 1 or 0 accordingly.
To achive this follow the below steps:
  • Add the 'MATLAB function' block to the Simulink model.
  • Implement the code in the 'MATLAB Function' block to determine if values are constant or fluctuating over 50 seconds and output the result. (See the sample code below.)
  • Connect the input signal to the 'MATLAB Function' block.
  • Add a 'Display' block to the model and connect it to the output of the 'MATLAB Function' block to visualize the result.
Here is sample code for the step 2:
function y = checkFluctuation(u)
persistent valuesArray index
% Initialize persistent variables
if isempty(valuesArray)
valuesArray = -1 * ones(1, 50); % Initialize with -1
index = 1;
end
if index <= 50
valuesArray(index) = u;
index = index + 1;
end
% Check if the array is filled
if index > 50
% Check if all values are 1
if all(valuesArray == 1)
y = 0;
% Check for alternating pattern 101010...
elseif isequal(valuesArray, repmat([1 0], 1, 25))
y = 1;
else
y = -1;
end
else
y = -1;
end
end
Refer the below links for better understanding:
Hope it helps!

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by