%
% Application Programming in MatLab-environment
% Autumn 2000
% 
% Exercise 7 & 8
%

% Clear the variables and close all windows
clear
close all

% ----- Problem 1 -----
% See file sigmoid.m for the definition of the MatLab function.
a = -3:0.02:3;
[f,d] = sigmoid(a,1);

plot(a,f,'-'); hold on
title('sigmoid and its derivative with different k');
xlabel('x-coordinate'); ylabel('y-coordinate');
plot(a,d,'--');

for k=2:5
  [f,d] = sigmoid(a,k);
  plot(a,f,'-');
  plot(a,d,'--');
end
hold off

% Plot the vectors b and c corresponding to the vector a
[b,c] = sigmoid(a,1:length(a));
figure; plot(a,b,'-');
figure; plot(a,c,'--');

disp('Press any key to continue.'); pause

% ----- Problem 2 -----
% Read the Iris data set from the ASCII file. The last column,
% which includes text, is not read.
clear
close all
m = dlmread('iris.data',',',0,0,[ 0 0 149 3 ])';
figure; matrix_graph(m,'r','-');

interval = [ -1 1 ];
[sm,ax,bx] = scaledata(m,interval);
figure; matrix_graph(sm,'r','-');

mm = invscaledata(sm,ax,bx);
figure; matrix_graph(mm,'r','-');
fprintf(1,'\nError caused by transformations: %6.2f\n',max(max(abs(m-mm))));

disp('Press any key to continue.'); pause

% ----- Problem 3 -----
% See mlp_out.m for computing the output of MLP-network.
clear
close all

% Generate the test data.
N = 50; hx = 2*pi/(N-1);
delta = 0.3; x = 0:hx:2*pi; n0= size(x,1);
ye = sin(x); y = ye + delta*randn(1,N); n2 = size(y,1);

plot(x,ye,'r--'); hold on
plot(x,y,'b-'); hold off

[sx,ax,bx] = scaledata(x,[ 0 1 ]);
[sy,ay,by] = scaledata(y,[ 0 1 ]);

disp('Press any key to continue.'); pause

% For training we need an optimization routine from 
% Optimization Toolbox and a function which computes 
% values of the cost functional J. This is given in mlp_cost.m

% Definition of the structure of the MLP-network:
% dimension of hidden layer and parameter for the sigmoid
% Attention: k must be a column vector, since the result of the
%            first linear transformation is a column vector. It is
%            of course possible to use a constant value for k.
n1 = 3;
k = 1:n1; k=k';

% random initial guess and optimization with fminunc
w1_dim = n1*(n0+1);
w2_dim = n2*(n1+1);
v0 = rand(w1_dim+w2_dim,1);
options = optimset('Diagnostics','on','Display','iter',...
                   'MaxIter',200*(w1_dim+w2_dim));
v = fminunc('mlp_cost',v0,options,k,n1,sx,sy);

% reshape the weight matrices from the vector v
w1 = reshape(v(1:w1_dim),n1,n0+1);
w2 = reshape(v(w1_dim+1:w1_dim+w2_dim),n2,n1+1);

% illustrate the obtained MLP-function on different grid
N = 81; hx = 2*pi/(N-1);
x_new = 0:hx:2*pi;

[sx_new,ax,bx] = scaledata(x_new,[ 0 1 ]);
os = [];
for i=1:N
   os(i) = mlp_out(sx_new(:,i),k,w1,w2);
end
o = invscaledata(os,ay,by);

figure; plot(x_new,o,'b-'); hold on
plot(x_new,sin(x_new),'r--'); hold off

