utility_fn = @(x, y, opt) argdeal(- (x-opt(1))^2 - sum((y-opt(2:end)).^2), ... -2*(x-opt(1)), -2*(y-opt(2:end))); opt = [3;5;7]; cost_fn = utility_to_cost(@(arg1, arg2) utility_fn(arg1, arg2, opt)); init1 = 6; init2 = [2;4]; regularized_cost_fn = penalize_square(cost_fn, 0); [opt_arg1, opt_arg2, opt_value] = minimize_args(regularized_cost_fn, ... -100, init1, init2); testclose([opt_arg1;opt_arg2], opt); regularized_cost_fn = penalize_square(cost_fn, [0,0]); [opt_arg1, opt_arg2, opt_value] = minimize_args(regularized_cost_fn, ... -100, init1, init2); testclose([opt_arg1;opt_arg2], opt); regularized_cost_fn = penalize_square(cost_fn, {0, [0;0]}); [opt_arg1, opt_arg2, opt_value] = minimize_args(regularized_cost_fn, ... -100, init1, init2); testclose([opt_arg1;opt_arg2], opt); regularized_cost_fn = penalize_square(cost_fn, {0, 0}); [opt_arg1, opt_arg2, opt_value] = minimize_args(regularized_cost_fn, ... -100, init1, init2); testclose([opt_arg1;opt_arg2], opt); regularized_cost_fn = penalize_square(cost_fn, {1, [10;100]}); epsilon = 1e-6; [max_errors, g1, g2] = var_checkgrad(regularized_cost_fn, epsilon, init1, init2); testsmall(max(max_errors), epsilon*10); testclose(g1, 6 + 2*6*1, epsilon*10); testclose(g2, 2*[-3;-3] + 2*[2*10;4*100], epsilon*10); test_exit