%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo Normal Distribution
% Written for GNU Octave
%
% INSTITUTE OF TELECOMMUNICATIONS
% University of Stuttgart
% www.inue-uni-stuttgart.de
% author: Marvin Geiselhart
% date: 17.04.2020
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This example script just prints the values of the given parameters.
% Make sure that the function prototype matches the prototype defined
% in the XML config file. (Parameter names are arbitrary but should be the same
% for better readability)


function [] = demo_q(show_q, show_chernov, show_ub, show_lb, logy, to, filename)
  
  from = 0;
  
  % define formats
  landscape = "-S930,350";
  portrait  = "-S640,480";
  
  q_color = [.2,.7,.2];
  
  % select format
  output_format = portrait;
  
  % Create an invisible figure.
  fh = figure(1);
  set(fh, "visible", "off");
  
  % define style
  set(0, "defaultlinelinewidth", 2);
  set(0, "defaultaxesfontsize", 8);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  
  % This to test and demonstrate the debug engine:
  % fprintf("This is a debug message. Checking format: %s", output_format);
  
  if logy
    plot_func = @semilogy;
  else
    plot_func = @plot;
  endif
  
  
  x = linspace(from, to, 101);
  
  clf;
  
  hold on;
  
  legendCounter = 1;
  legend_entries = {};
  
  %if show_phi
  %plot_func(x,PHI(x), "color", 'r')
  %legend_entries{legendCounter} = '\Phi(\tilde{x})';
  %legendCounter = legendCounter + 1;
  %endif
  
  if show_q
    plot_func(x,Q(x), "color", q_color)
    legend_entries{legendCounter} = "Q(x')";
    legendCounter = legendCounter + 1;
  endif
  
  if show_chernov
    plot_func(x,chernov(x), "color", 'c')
    legend_entries{legendCounter} = 'Chernov';
    legendCounter = legendCounter + 1;
  endif
  
  if show_ub
    plot_func(x(x>0.4),ub(x(x>0.4)), "color", 'm')
    legend_entries{legendCounter} = 'Upper Bound';
    legendCounter = legendCounter + 1;
  endif
  
  if show_lb
    plot_func(x(x>1),lb(x(x>1)), "color", 'b')
    legend_entries{legendCounter} = 'Lower Bound';
    legendCounter = legendCounter + 1;
  endif
  
  legend(legend_entries);
  
  xlim([from, to])
  
  xlabel("x'")
  
  grid on;
  
  
  % Create an image file. This png image is displayed on the webpage. Its size
  % must be 640x480.
  print(filename, "-dpng", output_format);
  
  
end

function y = pdf(x, sigma)
  y=  1/(sqrt(2*pi)*sigma)*exp(-1/(2*sigma^2)*x.^2);
endfunction

function y = chernov(x)
  y=  1/2*exp(-1/2*x.^2);
endfunction

function y = ub(x)
  y=  pdf(x, 1)./x;
endfunction

function y = lb(x)
  y=  ub(x).*(1-x.^(-2));
endfunction

function y = Q(x)
  y = 0.5*(1-erf(x/sqrt(2)));
end

function y = PHI(x)
  y = Q(-x);
end


