Example 1
clear,close all
x = 0:.05:2; y=humps(x);
P=x; T=y;
plot(P,T,'x')
grid; xlabel('time (s)'); ylabel('output');
title('humps function')
% DESIGN THE NETWORK
% ==================
%First try a simple one – feedforward
(multilayer perceptron) network
net=newff([0 2], [5,1],
{'tansig','purelin'},'traingd');
% Here newff defines feedforward network
architecture.
% The first argument [0 2] defines the range of
the input and initializes the network
parameters.
% The second argument the structure of the
network. There are two layers.
% 5 is the number of the nodes in the first
hidden layer,
% 1 is the number of nodes in the output layer,
% Next the activation functions in the layers
are defined.
% In the first hidden layer there are 5 tansig
functions.
% In the output layer there is 1 linear
function.
% ‘learngd’ defines the basic learning scheme –
gradient method
% Define learning parameters
[Link] = 50; % The result is shown
at every 50th iteration (epoch)
[Link] = 0.05; % Learning rate used
in some gradient schemes
[Link] =1000; % Max number of
iterations
[Link] = 1e-3; % Error tolerance;
stopping criterion
%Train network
net1 = train(net, P, T)
humps function
100
80
60
output
40
20
-20
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
time (s)
100
80
60
40
20
-20
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Humps function
100
80
Output of network and error
60
40
20
-20
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Time (s)
Best Training Performance is 17.5855 at epoch 1000
Train
Best
102 Goal
Mean Squared Error (mse)
100
10-2
10-4
0 100 200 300 400 500 600 700 800 900 1000
1000 Epochs
Example 2
x = -2:0.25:2; y = -2:0.25:2;
z = cos(x)'*sin(y);
mesh(x,y,z)
xlabel('x axis'); ylabel('y axis'); zlabel('z
axis');
title('surface z = cos(x)sin(y)');
gi=input('Strike any key ...');
pause
P = [x;y]; T = z;
net=newff([-2 2; -2 2], [25 17],
{'tansig''purelin'},'trainlm');
%Define parameters
[Link] = 50;
[Link] = 0.05;
[Link] = 300;
[Link] = 1e-3;
%Train network
net1 = train(net, P, T);
gi=input('Strike any key ...');
TRAINLM, Epoch 0/300, MSE 9.12393/0.001,
Gradient 684.818/1e-010
TRAINLM, Epoch 3/300, MSE 0.000865271/0.001,
Gradient 5.47551/1e-010
TRAINLM, Performance goal met.
mesh(x,y,a)
% Error surface
mesh(x,y,a-z)
xlabel('x axis'); ylabel('y axis');
zlabel('Error'); title('Error surface')
gi=input('Strike any key to continue......');
% Maximum fitting error
Maxfiterror = max(max(z-a))
Maxfiterror = 0.1116
Example 3
t=0:0.1:20; y=besselj(1,t);
plot(t,y)
grid
xlabel('time in secs');ylabel('y');
title('First order bessel function');
P=t; T=y;
%Define network. First try a simple one
net=newff([0 20], [10,1],
{'tansig','purelin'},'trainlm');
%Define parameters
[Link] = 50;
[Link] = 0.05;
[Link] = 300;
[Link] = 1e-3;
%Train network
net1 = train(net, P, T);
% Simulate result
a= sim(net1,P);
%Plot result and compare
plot(P,a,P,a-T)
xlabel('time in secs');ylabel('Network output
and error');
title('First order bessel function'); grid
First order bessel function
0.6
0.5
0.4
Network output and error
0.3
0.2
0.1
-0.1
-0.2
-0.3
-0.4
0 2 4 6 8 10 12 14 16 18 20
time in secs