close all
clear --all
clear --functions
clc

pkg load optim;

function [val] = target (x, A, b, L)
	val = norm (x, 1) + L' * (A*x - b);
	return;
endfunction;

function [g] = gtarget (x, A, b, L)
	[m, n] = size(A);
	g = zeros ((m+n),1);
	temp = sign(x) + A'*L;
	g(1:n,1) = temp;
	g((n+1):(n+m),1) = A*x-b;
	return;
endfunction;

m = 3;
n = 5;

A = 1:(m*n);
A = reshape (A, m, n);
b = (9:(m+8))';

f = @(y) target (y(1:n,1), A, b, y((n+1):(n+m),1));
df = @(y) gtarget (y(1:n,1), A, b, y((n+1):(n+m),1));

B = [A, eye(m)];
y0 = pinv(B) * b;
g0 = df(y0);

o = optimset('MaxIter', 5000, 'TolFun', 1E-2, 'MaxFunEvals', 500, 'SearchDirections', g0);

[y, val, conv, iters, nevs] = powell (f, y0, o);

x = y(1:n,1);
L = y((n+1):(n+m),1);
target (x, A, b, L)










