%ANNregression.m
%ANNdemo for regression problem 
x=-1:0.001:1; 
t=0.2*(x.^3)+0.5*x+0.3; 
TRAININPUT=x(1:2:length(x))'; 
TRAINOUTPUT=t(1:2:length(x))';
TESTINPUT=x(2:2:length(x))';
TESTOUTPUT=t(2:2:length(x))';
W1=randn(1,3)*0.1;
W2=randn(1,4)*0.1;
JCOL=[];
for iteration=1:1:100 
    YCOLTRAIN=[]; 
    for n=1:1:length(TRAININPUT)
        H=logsig(TRAININPUT(n)*W1);
        H=[H 1];
        Y=H*W2';
        YCOLTRAIN=[YCOLTRAIN;Y(1)];
        %Updating weights W2 
        W2(1,:)=W2(1,:)+0.001*((TRAINOUTPUT(n)-Y(1))).*H;
        %Updating weights W1 
        G1=(TRAINOUTPUT(n)-Y(1)).*W2(1,:).*H.*(1-H); 
        G1=G1(1:1:3); 
        W1=W1+0.001*TRAININPUT(n,:)'*G1;
    end
    JCOL=[JCOL sum((YCOLTRAIN-TRAINOUTPUT).^2)];
end
figure
plot(JCOL)
title('Convergence of the Regression based BPNN')
YCOLTEST=[];
for n=1:1:length(TESTINPUT)
    H=logsig(TESTINPUT(n)*W1);
    H=[H 1];
    Y=H*W2';
    %Using sigmoidal function 
    YCOLTEST=[YCOLTEST;Y]; 
    end
figure 
subplot(2,1,1) 
plot(YCOLTRAIN,'k-') 
hold on 
plot(TRAINOUTPUT,'r-') 
subplot(2,1,2) 
plot(YCOLTEST,'k-') 
hold on 
plot(TESTOUTPUT,'r-')
