1 function x_hat = perturb(x, epsilon)

2 %

3 % Generate a perturbed copy of ``x``, that is, a vector (or matrix)

4 % of the form ``x + dx`` where ``dx`` has same dimensions as x and

5 % negligible norm.

6 %

7 % INPUT:

8 %

9 % ``x`` -- The vector or matrix to perturb.

10 %

11 % ``epsilon`` -- An upper bound for the entries of ``dx``.

12 %

13 % OUTPUT:

14 %

15 % ``x_hat`` -- A perturbed copy of ``x``; ``x_hat == x + dx``.

16 %

18 % If no epsilon was given, make something up.

19 if (nargin < 2)

20 epsilon = 1e-10;

21 end

23 % This creates a vector or matrix whose entries lie in [-1,1].

24 dx = 2*rand(size(x)) - 1;

26 % First normalize delta, using the infinity norm for speed (since

27 % it doesn't matter).

28 dx = dx / norm(dx, 'inf');

30 % Now scale it so that its largest entry is

31 dx = dx * norm(x,'inf') * epsilon;

33 x_hat = x + dx;

34 end