%% Apply Quadratic Function and Plot Sparsity Pattern
% Create a random 50-by-50 sparse matrix with density 0.02, where the matrix has 50 nonzero elements. Plot the sparsity pattern of the matrix S.

rng default;
S = sprand(50,50,0.02);
spy(S)

% Evaluate the quadratic function x^2 + x + 1 to the nonzero elements of S. The evaluated function using spfun has the same sparsity pattern as the matrix S.

fun = @(x) x.^2 + x + 1;
F = spfun(fun,S);
spy(F)

%
% Copyright 2020 The MathWorks, Inc.