% eda04_09
% grid search
% but first shift x values 

T = load('linedata01.txt');

% now independently name the two columns of data
x = T(:,1);
dobs = T(:,2);
N = length(dobs);
M = 2;

% shift x values
x=x+2;

% define grid
L1=100; L2=100;
m1min=-10; m1max=10;
m2min=-10; m2max=10;
m1=m1min+(m1max-m1min)*[0:L1-1]'/(L1-1);
m2=m2min+(m2max-m2min)*[0:L2-1]'/(L2-1);

% data kerner
G=zeros(N,M);
G(:,1)=1;
G(:,2)=x;

% evaluate error at each grid point
E=zeros(L1,L2);
for i = [1:L1]
for j = [1:L2]
    mest = [ m1(i), m2(j) ]';
    dpre = G*mest;
    e = dobs-dpre;
    E(i,j) = e'*e;
end
end

eda_draw(' ',log(E),'caption E');

% search grid for minimum E
[Etemp, k] = min(E);
[Emin, j] = min(Etemp);
i=k(j);
m1est = m1(i);
m2est = m2(j);

Emin
m1est
m2est







