function check_grad_example_vector(feature_dim, sample_size) % an example of check_grad on the Lasso % min_{x} || A * x - y ||_F^2 % % by Jiayu Zhou. Dec 2, 2015. if nargin < 1, feature_dim = 500; end if nargin < 3, sample_size = 30; end A = randn(sample_size, feature_dim); y = randn(sample_size, 1); x0 = rand(feature_dim, 1); % closure on the constant variables. test_func = @(x) dic_obj(x, A, y); % perform testing. check_grad(test_func, x0) function [f, g] = dic_obj(x, A, y) % The function value and gradient of the following objective % min_{x} || A * x - y ||_F^2 % where % INPUT % [X(:); alpha] % OUTPUT % given the search point, variable_vect % f - function value % g - the vectorized gradient Axy = (A * x - y); g = A' * Axy; f = 0.5 * sum(Axy.^2);