% This matlab script will implement the perceptron weight learning rule.
% It will take two data sets d-10.csv and d-100.csv. The weight will be
% initialized randomly between 0 and 1 and a bias term will be added.


clc; clear all; close all;

% Import the data and initiate the learning rate, number of epochs, and 
% error matrix of zeros. 
alpha = 0.1:0.1:1;      % learning rate
epochs = [100, 500];    % number of epochs
error = zeros( length(alpha), length(epochs));

% Use function "impdata" to import csv data file and to convert it to a
% matrix form without header row
nameofdata = 'd-10.csv';
[values] = impdata(nameofdata);

% Define min and max value and the bias value for the weight vector and 
% Call the function  int_weight to initiate weights 
minV = 1; maxV = 10; bias_weight = 1;
[weights] = int_weight(minV,maxV,bias_weight);
numberofRows = size(values, 1);
values = [ones(numberofRows, 1) values]; % add a bias term to the data matrix

% The last column is the class values, we don't need to include it for
% weight update
numberofColumns = size(values, 2) - 1; 

% Call the "cal_error" function to calculate the error after 100 and 500
% epochs
[error1] = cal_error(error, alpha, epochs, numberofRows, numberofColumns, weights, values)
