Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

stationary(x, y, type)
function index = stationary(x, y, type)
% STATIONARY returns stationary points of xy data
% Stationary gives the index points for stationary points in the function
% described by the vectors x and y. Specifying the string "type" as 'max',
% 'min' or 'both' means that "ind" gives the maximum stationary points, the
% minimum stationary points or both respectively.  The default option is
% 'both'.

% I think this could be vastly improved by vectorising, however, it is
% still pretty quick.

% type wasn't supplied, assume both
if nargin < 3
    % defines the type
    type = 'both';
end

% calculates the gradient (1st derivative)
ygrad = gradient(y, x);

% calculates the gradient of that (2nd derivative)
ygrad2 = gradient(ygrad, x);

% initialises the index
index = [];

% depending on the type selected...
switch type
    % find minimum stationary points only
    case 'min'
        for j = 1:length(x) - 1
            % ...
            if sign(ygrad(j + 1)) ~= sign(ygrad(j)) & ygrad(j + 1) & ygrad2(j) > 0
                % ...
                [v, g] = min(y(j:j + 1));

                % ... (note that the index is growing consider
                % pre-allocating this?)
                index = [index, j + (g - 1)];

            % ...
            elseif ~ygrad(j) & ygrad2(j) > 0
                % ...(see above)
                index = [index, j];
            end
        end
        
    % find maximum stationary points only
    case 'max'
        % ...
        for j = 1:length(x) - 1
            % ...
            if sign(ygrad(j + 1)) ~= sign(ygrad(j)) & ygrad(j + 1) & ygrad2(j) < 0
                % ...
                [v, g] = min(y(j:j + 1));

                % ...
                index = [index, j + (g - 1)];

            % ...
            elseif ~ygrad(j) & ygrad2(j) < 0
                % ...
                index = [index, j];
            end
        end
        
    % find minimum and maximum stationary points
    case 'both'
        % ...
        for j = 1:length(x) - 1
            % ...
            if sign(ygrad(j + 1)) ~= sign(ygrad(j)) & ygrad(j + 1)
                % ...
                [v, g] = min(y(j:j + 1));

                % ...
                index = [index, j + (g - 1)];

            % ...
            elseif ~ygrad(j)
                % ...
                index = [index, j];
            end
        end
end

Contact us