Professional Documents
Culture Documents
2- Function Approximation
%%
X=linspace(-1,1,21);
Y=0.8*sin(pi*X);
%plot(X,Y)
net=newfit(X,Y,5);
net.divideFcn='';
XTest=-1:0.05:1;
net=train(net,X,Y);
out=sim(net,XTest);
plot(X,Y,'o',XTest,out,'-');
%% What is the network function?
%read the layers
HB=net.B{1}; %hidden layer bias
HW=net.IW{1}; %hidden layer weights, stored ROW-wise (each neuron is a ROW)
OB=net.B{2};
OW=net.LW{2,1};
myOut=zeros(size(out));
%apply preprocessing used by the network
myIn=mapminmax(XTest,net.inputs{1}.processSettings{3});
for i=1:size(XTest,2) %iterate over all inputs
in=XTest(:,i);
myOut(:,i)=OW*tansig(HW*in+HB)+OB;
%
^ input neurons^
%
^ output activation, no output sigmoid
end
%postprocess the output
myOut=mapminmax('reverse',myOut,net.outputs{1,2}.processSettings{2});
%compare
disp(['net out~=myOut: ' mat2str(find(abs(out(:)-myOut(:))>1e-6))]);