function [plotx,plotyGP,plotyGTACS,gprob,GraphTotalAveClusterSize] = GraphPercolation(r,c,pint,ncop)
% Unoptimized function used to find the critical probability threshold 
% of a generalized two-dimensional square lattice
%
% INPUTS
%    r = Number of horizontal rows in the rectangular lattice
%    c = Number of vertical columns in the rectangular lattice
% pint = Number of intervals for the cutoff probability p; e.g. pint = 10 
%        means the probabilities 0.1, 0.2, ... 0.9 will be tested
% ncop = Number of copies to be run per test probability
% 
% OUTPUTS
% The variables listed don't really matter much; it's the outputs from
% "saveas" which are important
% ===========================================================

%Measure the time taken
tic;

%Create sparse adjacency matrix
sparse = CreateSparseGraph(r,c);

psplit = 1/pint;
gprob = []; %Probability of graphs
%GraphAveClusterSize = []; %To store the average cluster size per graph

%Test pinterval-1 different values of p, equally spaced between 0 and 1.
for p = (psplit:psplit:(1-psplit))
    [Probability,AveClusterSize] = Percolation(r,c,p,ncop,sparse);
    gprob = cat(1,gprob,Probability);
    GraphTotalAveClusterSize = cat(1,GraphTotalAveClusterSize,AveClusterSize);
end

%The Average Cluster Size (denoted here with "GraphAveClusterSize") 
%can be normalized to a value between 0 and 1 (or some other value) as this 
%Average Cluster Size does depend on the size of the matrix being tested.
%NormAveClusterSize = GraphAveClusterSize/((r-1)*(c)+(r)*(c-1))

%===========================================================
%The following is to plot the graph, but I believe something screwed up
%with it. 

%To show the graph of the output Percolation Probability and Average 
%Cluster Size against p
set(0,'DefaultFigureVisible','on');
p = (psplit:psplit:(1-psplit));

GraphVar = gprob;
%GraphVar = cat(1,gprob,GraphAveClusterSize);

%Plot the results
figure;
[plotx,plotyGP,plotyGTACS] = plot(p,GraphVar);

%errorbar(p,GraphProbability,(GraphProbability-Probabilityste),(GraphProbability+Probabilityste));
%errorbar(AveClusterSize,AveClusterSizeste);

%fname includes directory and file names
fname = strcat('../ResultPicturesAnalysis/Graph_',num2str(pint),'_',num2str(ncop));
saveas([plotx,ploty],fname,'fig'); 
%saveas([plotx,ploty],fname,'jpeg');

toc

end
