function fig8_10

% includes table 8.4

%  this requires the subaxis.m and parseArgs.m files
%  note plots change with each run due to the randomly generated data

global xd yd

clf
% get(gcf)
set(gcf,'Position', [12 450 748 895])
co = [0 0 1;
    0 0.5 0;
    1 0 0;
    0 0.75 0.75;
    0.75 0 0.75;
    0.75 0.75 0;
    0.25 0.25 0.25];
set(groot,'defaultAxesColorOrder',co)

%  generate data for regression y=a+b*x
n=20;
xd=rand(1,n);
qd=0.7*(rand(1,n)-0.5);
a=1; b=3;
yd=a+b*xd+2*(rand(1,n)-0.5);

max(yd)
min(yd)

%  have MATLAB find coeffs for linear fit
% g=aa+bb*x
ab=polyfit(xd,yd,1);
aa=ab(2);  bb=ab(1);
s2=[aa bb];

% true l^2 fit
sD = fminsearch(@ED, [aa bb], optimset('TolFun',eps,'TolX',eps));

% vertical l^1 fit
sT = fminsearch(@ET, [aa bb], optimset('TolFun',eps,'TolX',eps));

% vertical l^inf fit
%[sM , fval,exitflag,output] = fminsearch(@EM, a, optimset('TolFun',eps,'TolX',eps))
sM = fminsearch(@EM, [aa bb], optimset('TolFun',eps,'TolX',eps));

% format for book
fprintf('\n E_2 & %3.2f  & %3.2f',s2(1),s2(2))
fprintf('\n E_D & %3.2f  & %3.2f',sD(1),sD(2))
fprintf('\n E_1 & %3.2f  & %3.2f',sT(1),sT(2))
fprintf('\n E_inf & %3.2f  & %3.2f',sM(1),sM(2))
fprintf('\n\n')

%  evaluate interpolation fn's for use in plotting
%  nn = number of points plotted
nn=30;
X = linspace(0,1,nn);
Y = polyval(ab,X);
for ix=1:nn
    yD(ix)=sD(2)*X(ix)+sD(1);
    yT(ix)=sT(2)*X(ix)+sT(1);
    %	yll(ix)=solll(1)*X(ix)+solll(2);
    yM(ix)=sM(2)*X(ix)+sM(1);
end

clf
% get(gcf)
set(gcf,'Position', [25 1115 658 230])
subaxis(1,1,1,1,'MT',0.01,'MB',0.16,'MR',-0.03,'ML',0.023,'P',0.04)
co = [0 0 1;
    0 0.5 0;
    1 0 0;
    0 0.75 0.75;
    0.75 0 0.75;
    0.75 0.75 0;
    0.25 0.25 0.25];
set(groot,'defaultAxesColorOrder',co)

plot(X,yD,'r','LineWidth',1.6)
hold on
plot(X,Y,'--k','LineWidth',1.6)
%plot([x1 x4],[y1 y4],'--k','LineWidth',1.4)
plot(xd,yd,'.b','MarkerSize',25)
grid on
xlabel('x-axis','FontSize',16,'FontWeight','bold')
ylabel('y-axis','FontSize',16,'FontWeight','bold')
set(gca,'FontSize',16,'FontWeight','bold')
axis([0 1 0 5])
legend({' E_D',' E_2'},'Location','SouthEast')
hold off


function s=ED(x)
global xd yd
n=length(xd);
s=0;
for i=1:n
    s = s + f(x(1),x(2),xd(i),yd(i))^2;
end
s=s/(1+x(2)^2);

function s=EM(x)
global xd yd
n=length(xd);
for i=1:n
    ee(i) = abs(f(x(1),x(2),xd(i),yd(i)));
end
s=norm(ee,Inf);

function s=ET(x)
global xd yd
n=length(xd);
s=0;
for i=1:n
    s = s + abs(f(x(1),x(2),xd(i),yd(i)));
end

function d=f(a,b,x,y)
d=a+b*x-y;























