%
% Application Programming in MatLab-environment
% Autumn 2000
% 
% Exercise 9 & 10
%
% Clear the variables and close all windows
clear
close all

% ----- Problem 1 -----

% Generate the test data: 50 x-values at the interval [0,2*pi]
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-');

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

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

% For training we need function fminunc from Optimization Toolbox and a
% function which computes values of the cost functional J and the
% corresponding gradient. This is given by mlp_cgd.m. The value of the
% functional MUST be the first output and gradient the second output. 

% 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',...
                   'GradObj','on','MaxIter',100);
v = fminunc('mlp_cgd',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:
% 80 x-values in the interval [0,2*pi]
N = 80; 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);

plot(x_new,o,'b-'); hold off
