%
% -------------------------------------------------------------------
%
%     Approximation im WS 2013/14                      H.J. Oberle
%
%     L_1 Bestapproximation auf Gitter t_1 < ... < t_m
%     mittels linearer Optimierung (Simplex Algorithmus)
%
% -------------------------------------------------------------------
%
%
function [y, fval, exitflag, output, lambda] = simplex1
% 
clear all
close all
format long e
%
%   Beispiel (1.3)
% 
m = 4;
T = [-1 0 1 2]';
Y = [-2 0 2.5 3]';
b = Y;
e = ones(4,1);
E = eye(4);
H = [e T];
A = [H -E E];
c = [0 0 e' e'];
%
lb = [-inf -inf 0 0 0 0 0 0 0 0];
%x0 = [0 1.75 0 0 0 0 0 0 0 0]';
x0 = 1+0*c;
options = optimset('TolX',1e-8,'TolFun',1e-8,'Largescale','off');
%
fprintf('\n Lineares Optimierungsproblem: \n');
%
[y, fval, exitflag, output, lambda] = linprog(c,[ ],[ ],A,b,lb,[ ],x0,options);
%
%   Loesung
%
x = y(1:2)
u = y(3:6)
v = y(7:10)
n = norm(u-v,1)
%
%   Plot
%
t = [-1.5:0.01:2.5];
p1 = x(2)*t+x(1);
plot(t,p1,'k','Linewidth',1.2)
set(gca,'FontSize',12)
ylabel('L_1 Ausgleichsgerade p(t)','FontSize',16)
hold
plot(T,Y,'b.','Markersize',20)
%
pause
p2 = 1.75*t;
plot(t,p2,'b--','Linewidth',1.2)
%
pause
p3 = 1/4 + (5/3)*t;
plot(t,p3,'r:','Linewidth',1.2)
%
print -depsc simplex02
%
%  ------------------------------------------------------------------
%
