
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  Title: StatisCLAS: A Package of Classification Methods   %%%%
%%%%  Author: David Casado de Lucas                            %%%%
%%%%  Version: 29th May 2013                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PURPOSE: This function plots some functions of two samples.

% INPUT VARIABLES:
%    t: Vector with the values of the independent variable.
%    groupFD1: Matrix with the functions of group 1 as rows.
%    groupFD2: Matrix with the functions of group 2 as rows.
%    figureTitle: Optional string variable with the title of the figure.
%    percentMargin: Optional variable (in percent) to control de distance from
%                   the more external data to the axes.
%    curvesJointly: Optional logic variable to indicate whether or not the two
%                   samples are represented jointly.
%    refGroupFD1: Optional vector with a function of population 1 (usually the
%                 reference function, e.g. the mean or the trimmed mean).
%    refGroupFD2: Optional vector with a function of population 2 (usually the
%                 reference function, e.g. the mean or the trimmed mean).

% COMMENTS:
%   (1) The reference functions can have been calculated from samples different to 'groupFD1' and 'groupFD2'.

function plotFunctions(t, groupFD1, groupFD2, figureTitle, percentMargin, curvesJointly, refGroupFD1, refGroupFD2)

if ~exist('figureTitle','var') || isempty(figureTitle)
  figureTitle = 'Functions';
end
if ~exist('percentMargin','var') || isempty(percentMargin)
  percentMargin = 5;
end
if ~exist('curvesJointly','var') || isempty(curvesJointly)
  curvesJointly = true;
end

xMin = t(1);
xMax = t(end);
% xRange = xMax - xMin;
yMin = min(min([groupFD1; groupFD2; refGroupFD1; refGroupFD2]));
yMax = max(max([groupFD1; groupFD2; refGroupFD1; refGroupFD2]));
yRange = yMax - yMin;
if isequal(yRange,0)
  yRange = 0.01;
end

if curvesJointly
  plot(t,groupFD1','b')
  hold on
  plot(t,groupFD2','r')
  if exist('refGroupFD1','var') && ~isempty(refGroupFD1)
    plot(t,refGroupFD1,'b-','LineWidth',2)
  end
  if exist('refGroupFD2','var') && ~isempty(refGroupFD2)
    plot(t,refGroupFD2,'r-','LineWidth',2)
  end
  hold off
  xlabel('Independent variable')
  ylabel('Dependent variable')
  title(figureTitle)
  set(gca,'XLim',[xMin xMax])
  set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
else
  subplot(2,1,1)
    plot(t,groupFD1','b')
    hold on
    if exist('refGroupFD1','var') && ~isempty(refGroupFD1)
      plot(t,refGroupFD1,'b-','LineWidth',2)
    end
    hold off
    xlabel('Independent variable')
    ylabel('Dependent variable')
    title([figureTitle ' (group 1)'])
    set(gca,'XLim',[xMin xMax])
    set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
  subplot(2,1,2)      
    plot(t,groupFD2','r')
    hold on
    if exist('refGroupFD2','var') && ~isempty(refGroupFD2)
      plot(t,refGroupFD2,'r-','LineWidth',2)
    end
    hold off
    xlabel('Independent variable')
    ylabel('Dependent variable')
    title([figureTitle ' (group 2)'])
    set(gca,'XLim',[xMin xMax])
    set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
end
  
end
