echo on
clc
clear all
x = [0 1 2 3 4
-2 -1 0 1 2];
d=[-1 3.736 3.271 3.100 9.037
15.000 13.000 15.000 21.000 31.000];
% x = [0 1 2
% -2 1 2];
% d=[-1 5 11.27
% 0 1 0];
%weights connecting inputs to the hidden node
iw = zeros(3,2);
%weights connecting hidden nodes with the output nodes
lw= zeros(2,3);
%learning rate
mu = 0.25;
%test parttens
max_tests = 5;
echo off;pause
mse = inf;
a = 0;
tic
while(mse > 0.26)
sum = 0;
a = a+1;
for n=1:max_tests,
a_in=iw*x(:,n);
%outputs vector for the hidden nodes
a=[1/(1+exp(-a_in(1))) 1/(1+exp(-a_in(2))) 1/(1+exp(-a_in(3)))];
%input to the output nodes
y_in = lw*a';
%output vector
y = [1/(1+exp(-y_in(1))) 1/(1+exp(-y_in(2)))];
%error vector
e = d(:,n)'-y
%theta for the outputs
thetaout=[e(1)*y(1)*(1-y(2));
e(2)*y(2)*(1-y(2))];
%theta for the hidden nodes
thetaL1 = a(1)*(1-a(1))*lw(:,1)'*thetaout;
thetaL2 = a(2)*(1-a(2))*lw(:,2)'*thetaout;
thetaL3 = a(3)*(1-a(3))*lw(:,3)'*thetaout;
%updates the weights
lw(1,:)=lw(1,:)+mu*e(1)*a;
lw(2,:)=lw(2,:)+mu*e(2)*a;
iw(1,:)=iw(1,:)+mu*thetaL1*x(:,n)';
iw(2,:)=iw(2,:)+mu*thetaL2*x(:,n)';
iw(3,:)=iw(3,:)+mu*thetaL3*x(:,n)';
%mean square erro preparation
sum= sum+(e(1)*e(1)+e(2)*e(2))/2;
['iteration #: ' int2str(a) ':']
% pause
end
%calculate mean square error
mse=sum/max_tests
end
toc
iw
lw