Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A square matrix is symmetric if array(i,j) = array(j,i) for all i, j. We say tha

ID: 3687530 • Letter: A

Question

A square matrix is symmetric if array(i,j) = array(j,i) for all i, j. We say that a square matrix is skew symmetric if array(i,j) = - array(j,i) for all i, j. Notice that this means that all of the values on the diagonal must be 0. Write a function that will receive a square matrix as an input argument, and will return logical 1 for true if the matrix is skew symmetric or logical 0 for false if not.

For example, the following matrix is skewed:

0 1 1 1

-1 0 1 1

-1 -1 0 1

-1 -1 -1 0

The following matrix is not:

1 1 1 1

1 2 1 1

1 0 1 1

1 1 0 1

Explanation / Answer

function bin= isSkewSymmetric(M)

%% Check dimensions of M
if size(M,1) ~= size(M,2) || ~ismatrix(M)
    error('"M" must be a square matrix.');
end

%% Check for custom functions
%TODO - check for zeroFPError.m

%% Check for skew-symmetric
bin = true; % assume skew-symmetric matrix

chk = zeroFPError( sum(sum( abs(M + transpose(M)) )) );
try
    % Check if term contains any symbolic variables
    logical(chk);
catch
    % Try simplifying complicated term one more time
    chk = zeroFPError(chk);
end

try
    if abs(chk) > 0 % converts "chk" to logical, it will throw an error if
                     % for symbolic arguments are still in the expression.
        bin = false;
        return
    end
catch
    bin = false;
    return
end

zeroFPError.m

function H = zeroFPError(H,ZERO)

%% Set Defaults
if nargin < 2
    if strcmpi(class(H),'double') || strcmpi(class(H),'single')
        ZERO = 10*eps(class(H));
    else
        ZERO = 1e-10; % assume magnitudes smaller than this are zero
    end
end

%% Zero floating point error
if strcmpi( class(H), 'sym') % apply for symbolic inputs
    % Simplify initial input (prior to VPA call)
    H = simplify(H);

    % Numerically evaluate terms
    dgts = ceil( -log10(ZERO) ); % specify digits based on ZERO
    H = vpa(H,dgts+1);           % numerically evaluate input
  
    % Zero
    for i = 1:numel(H)
        h = H(i);
        [n,d] = numden(h); % account for expressions that are fractions
      
        % Numerator
        [c,s] = coeffs(n);
        if ~isempty(c) && ~isempty(s)
            for k = 1:numel(c)
                if abs( c(k) ) < ZERO
                    c(k) = 0;
                else % round based on zero
                    c(k) = round(c(k)*(1*10^dgts))*(1*10^-dgts);
                end
            end
            n = s*c';
        else
            n = 0;
        end
      
        % Denominator
        if d ~= 1
            [c,s] = coeffs(d);
            for k = 1:numel(c)
                if abs( c(k) ) < ZERO
                    c(k) = 0;
                else % round based on zero
                    c(k) = round(c(k)*(1*10^dgts))*(1*10^-dgts);
                end
            end
            d = s*c';
          
            % Combine
            H(i) = simplify(n/d);
        else % for denominator of 1
            H(i) = n;
        end

    end
    H = vpa(H);
else % apply for double and single precision
    for i = 1:numel(H)
        h = H(i);
        % Zero
        if abs(h) < ZERO
            H(i) = 0;
        end
    end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote