--- /dev/null
+function x_hat = perturb(x, epsilon)
+ %
+ % Generate a perturbed copy of ``x``, that is, a vector (or matrix)
+ % of the form ``x + dx`` where ``dx`` has same dimensions as x and
+ % negligible norm.
+ %
+ % INPUT:
+ %
+ % ``x`` -- The vector or matrix to perturb.
+ %
+ % ``epsilon`` -- An upper bound for the entries of ``dx``.
+ %
+ % OUTPUT:
+ %
+ % ``x_hat`` -- A perturbed copy of ``x``; ``x_hat == x + dx``.
+ %
+
+ % If no epsilon was given, make something up.
+ if (nargin < 2)
+ epsilon = 1e-10;
+ end
+
+ % This creates a vector or matrix whose entries lie in [-1,1].
+ dx = 2*rand(size(x)) - 1;
+
+ % First normalize delta, using the infinity norm for speed (since
+ % it doesn't matter).
+ dx = dx / norm(dx, 'inf');
+
+ % Now scale it so that its largest entry is
+ dx = dx * norm(x,'inf') * epsilon;
+
+ x_hat = x + dx;
+end
--- /dev/null
+A = rand(10);
+A_hat = perturb(A, 1e-12);
+dA = A_hat - A;
+
+unit_test_equals("dA has the correct infinity norm", ...
+ 1e-12, ...
+ norm(dA, 'inf'));