File size: 2,369 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | function [Constant,VarVal] = isconstant(x,VarTol)
% isconstant - Evaluate if all of the elements of an array x are constant
% to within a specified tolerance
%
% Syntax: [Constant,VarVal] = isconstant(x,VarTol)
%
% Inputs:
% x - Array to be analyzed (any dimension)
% VarTol - Tolerance to evaluate x element variations (default: 1e-4)
%
% Outputs:
% Constant - Logical indicating if all x array elements are constant to
% within the specified variation tolerance level VarTol:
% true => x values constant to within tolerance
% false => x values vary beyond tolerance
% VarVal - Variation value used to evaluate constance
%
% Example/Validation Cases:
%
% Other m-files required: None
% Subfunctions: None
% MAT-files required: None
%
% See also: none
%
% August 2019; Last revision: 2019-SEP-04
%
% ----------------- BEGIN CODE -----------------
% Set default variation tolerance
if (nargin < 2) || isempty(VarTol)
VarTol = 1e-4;
end
% Evaluate x array for variations
Nx = numel(x);
if Nx <= 1
% Arrays with one or zero elements can have no variations,
% so are evaluated as constant
Constant = true;
VarVal = 0;
else
% Min and max of linear-indexed x array
minx = min(x(:));
maxx = max(x(:));
% Evaluate variation in x array elements
if minx == maxx
% Equal min and max values indicate no variations
Constant = true;
VarVal = 0;
else
% Compare difference between unequal min and max x values to
% cutoff value defined by specified tolerance
difx = maxx-minx;
medx = 0.5*(minx+maxx);
valx = max([difx abs(medx) abs(minx) abs(maxx)]);
Constant = (difx <= valx*VarTol);
VarVal = difx/valx;
end
end
return
end
% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% D.Hall | 2019-SEP-04 | Initial Development
%
|