% eda11_11
%
% Use a neural net to approximate an parabolic function y(x)
% by superimposing 1D towers

% ---Polynomial function definitions---

% Nc: the number of centers in the approximation
Nc= 6;
% xcmax: the maximum value of the centers in the approximation
xcmax = 1;
% xcmin: the minimum value of the centers in the approximation
xcmin = 0;
% xc: the centers in the approximation
xc = xcmin + (xcmax-xcmin)* [0:Nc-1]'/(Nc-1);

% yc: the true value of the polynomial function
yc = 0.2 + 1.75*xc.^2;

% ---Step function definitions---

% Xc: the center of the step function
Xc = xc;

% Dx: the width of the step function
Dx = ones(Nc,1)*(xcmax-xcmin)/(Nc-1);

% h: the height of the step function
h = yc;

% slope: slope of the step functiona
slope = 999/4;

% ---Neural Net definitions---

% Nmax: number of layers;
Lmax = 3;

% Lmax: maximum number of neurons in any layer
Nmax = Nc * 2;

[N,w,b,a] = eda_net_init(Lmax,Nmax);

[N,w,b] = eda_net_1dtower( Nc, slope, Xc, Dx, h, N, w, b );

figure(1);
clf;
eda_net_view(N,w,b);

% ---Picking the points to plot---

% Nx: the number of points we want to plot
Nx= 100;
% xmax: the maximum value of the points we want to plot
xmax = xcmax + .1;
% xmin: the minimum value of the points we want to plot
xmin = xcmin - .1;
% x: the points we are plotting
x = xmin + (xmax-xmin)* [0:Nx-1]'/(Nx-1);

m= zeros(Nx,1);

% ---Calculating the Activities---

for kx = [1:Nx]
    a(1:N(1),1) = [x(kx)];

    a = eda_net_eval( N,w,b,a);

    m(kx) = a(1:N(3),3);
end

figure(2);
clf;
subplot(2,1,1);
set(gca,'LineWidth',2);
hold on;
axis xy;
axis( [xmin xmax -1 2] );

plot(x,m,'k-','LineWidth',2); 

title('step function points');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');

% ---Plot true value---

m= zeros(Nx,1);
m = 0.2 + 1.75*x.^2;

subplot(2,1,2);
set(gca,'LineWidth',2);
hold on;
axis xy;
axis( [xmin xmax -1 2] );
 
plot(x,m,'k-','LineWidth',2); 

title('step function points');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');


