% eda11_09
%
% Use a neural net to approximate a step function

% Step function definitions

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

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

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

% Neural Net definitions
% Nmax: number of layers;
Lmax = 3;

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

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

N = [1, 1, 1]';

w(1,1,2) = 4*slope;
w(1,1,3) = h;
b(1,2) = -4*slope*Xc;

figure(1);
clf;
eda_net_view(N,w,b);
% Nx: the number of points we want to plot
Nx= 100;
% xmax: the maximum value of the points we want to plot
xmax = 4;
% xmin: the minimum value of the points we want to plot
xmin = 0;
% x: the points we are plotting
x = xmin + (xmax-xmin)* [0:Nx-1]'/(Nx-1);

m= zeros(Nx,1);

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; slope = 999');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');


%----------------- do it again for slope = 4 ------------
slope = 4/4;

w(1,1,2) = 4*slope;
w(1,1,3) = h;
b(1,2) = -4*slope*Xc;

m2= zeros(Nx,1);

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

    a = eda_net_eval( N,w,b,a);
    
    m2(kx) = a(1:N(3),3);
end

subplot(2,1,2);
set(gca,'LineWidth',2);
hold on;
axis xy;
axis( [xmin xmax -1 2] );
 
plot(x,m2,'k-','LineWidth',2); 
title('step function; slope = 4');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');



