%% Example 4 - RBFN
clear all; clc; 
%close all;
load input4;
load target4;
% Target
figure()
plot(X,T,'+'); %plot input over output-target
title('Training Vectors');
xlabel('Input Vector X');
ylabel('Target Vector T');

eg = 0.0000001; % sum-squared error goal
sc = 0.2;    % spread constant
net = newrb(X,T,eg,sc);
%net=newrbe(X,T,sc);

X = -1:.01:1;
Y = net(X); % Prediction


hold on;
plot(X,Y); % plot input over prediction
hold off;
legend({'Target','Output'})
%%
%Calculate the cost function
cost_function=0;
for i=1:length(T)
    cost_function=cost_function+(T(i)-Y(i))^2;
end
cost_function=cost_function/2;

fprintf('\nCost Function: %.4f \n',cost_function);

