% eda11_10
%
% Use a neural net to approximate a 1D tower function

% Tower function definitions

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

% Dx: the width of the tower function
Dx = 1;

% h: the height of the tower function
h = 1.5;

% 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);

% implement a three-layer Neural net
N = [1, 2, 1]';

w12 = 4*slope;
w22 = 4*slope;
w(1:N(2),1,2) = [w12;w22];

w13 = h;
w23 = -h;
w(1,1:N(2),3) = [w13;w23];

b12 = -w12 * ( Xc - Dx/2);
b22 = -w22 * ( Xc + Dx/2);
b(1:N(2),2) = [b12;b22];

% graph the net
figure(1);
clf;
eda_net_view(N,w,b);

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

% evaluate neural net for al x's
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

% plot the result
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('tower function; slope = 999');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');

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

w12 = 4*slope;
w22 = 4*slope;
w(1:N(2),1,2) = [w12;w22];

b12 = -w12 * ( Xc - Dx/2);
b22 = -w22 * ( Xc + Dx/2);
b(1:N(2),2) = [b12;b22];

m2= zeros(Nx,1);

for kx = [1:Nx]
    a(1:N(1),1) = [x(kx)];
    
    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('tower function; slope = 4');
xlabel('a_1_1');
ylabel('a_3_1(a_1_1)');
