function TCM_constrained_minimization

% TCM_constraint_minimization Interactive illustration of the minimization of a
% bivariate polynomial on a curve.
%
%   TCM_minima_curve illustrates the minimization of a bivariate
%   polynomial on a curve. The minimization under constraints can in
%   general be solved with the help of Lagrange-multiplier. In case of one
%   constraint, it is replaced by finding the points with parallel
%   gradients.
%   
%   EXAMPLES
%
%   Basic Example: The minimum of a quadratic polynomial on a hyperbel can
%   easily calculated analytically by a substitution.
%
%   Singular point: If the constraint contains a singular point with
%   vanishing derivatives, the minima can even attained at this point.
%   
%   Multiple Extrema: Depending on the shape of the function, there can
%   even be multiple extrema on the boundary of the bounding box.
%    
%   To investigate other cases, insert a function, a constraint function and
%    a rectangle in the corresponding fields. 
%
%   TCM_constraint_minimization is part of the <a href="http://www2.imng.uni-stuttgart.de/LstNumGeoMod/TCM/">TCM</a> project.
%   Related course materials can be found at <a href="http://www.mathematics-online.org/inhalt/aussage/aussage687">Mathematics Online</a>.

% Authors: Florian Martin, Lars von Wolff
% Last updated: 03.10.2017

% Initialize data
TCM_Data.fun = @(x,y) x.*exp(-x.^2-y.^2);  
TCM_Data.funstring = '@(x,y) x.*exp(-x.^2-y.^2)';
TCM_Data.con = @(x,y) x.^2+y.^2;  
TCM_Data.constring = '@(x,y) @(x,y) x.*y+2';
TCM_Data.fdeg = [];
TCM_Data.fcoeff = [];
TCM_Data.gdeg = [];
TCM_Data.gcoeff = [];
TCM_Data.hdeg = [];
TCM_Data.hcoeff = [];

TCM_Data.minima = [];

TCM_Data.rect = [];
TCM_Data.selected = 0;
TCM_Data.move = 0;

% Do not delete these Variables
TCM_Data.plot_window = [-2 2 -2 2];
TCM_Data.LmbMenuString = '';
TCM_Data.examples = {'Basic Example','Singular Point','Multiple Extrema'};

% Layout:
% ---------------------------------------
% |               Title                 |
% | ------------ --------- ------------ |
% | | Settings | |Buttons| | Feedback | |
% | ------------ --------- ------------ |
% | ----------------------------------- |
% | |           Axis (Plot)           | |
% | ----------------------------------- |
% | Info-Button         Hint            |
% ---------------------------------------
% Initialize Layout:
TCM_UI.TitleHeightMin = 10;        % minimal height of the title
TCM_UI.TitleHeightMax = 32;        % maximal height of the title
TCM_UI.HintHeightMin = 8;          % minimal height of the hint
TCM_UI.HintHeightMax = 15;         % maximal height of the hint
TCM_UI.SettingsHeightMin = 40;     % minimal height of the feedback, buttons and 
                                   % setting panel
TCM_UI.SettingsHeightMax = 70;     % maximal height of the feedback, buttons and 
                                   % setting panel
TCM_UI.SettingsRatio = 4.5;          % aspect ratio of the settings  
                                   % (increase if more settings needed)
TCM_UI.ButtonsRatio = 1.4;         % aspect ratio of the Buttons panel
TCM_UI.FeedbackRatio = 3.4;        % aspect ratio of the feedback
TCM_UI.AxisHeightTarget = 0.73;    % desired fraction of figure 
                                   % used for the plot
TCM_UI.InfoPortion = 1/8;          % Portion of the Info Button in the last  
                                   % line 
TCM_UI.Title = 'Constrained Minimization of Polynomials';
TCM_UI.mono = get(0,'FixedWidthFontName');
% Strings for generating the information window
TCM_UI.Author = 'F. Martin and L. von Wolff';
TCM_UI.Date = '03.10.2017';
TCM_UI.Web = 'http://www.mathematics-online.org/inhalt/aussage/aussage687';
      
CreateUI();

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% UI-Elements in Settings %%%%%%%%%%%%%%%%%%%%
% Text for Function
TCM_UI.funtext = uicontrol('Units','normalized', 'Parent',TCM_UI.settingspanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'FontUnits','normalized','FontSize',0.67,...
        'HorizontalAlignment','left', ...
	'Position',[.01,.70,.5,.30],...
	'String','Function:',...
	'Style','text');

% Input for Function
TCM_UI.funinput=uicontrol('Units','normalized', 'Parent',TCM_UI.settingspanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'FontWeight','bold',...
        'FontUnits','normalized','FontSize',0.65,'FontName',TCM_UI.mono,...
        'HorizontalAlignment','left', ...
	'Position',[.35,.70,.65,.30],...
	'String','',...
	'Style','edit',...
	'Callback',@function_update);

% Text for the bounding box
TCM_UI.recttext = uicontrol('Units','normalized', 'Parent',TCM_UI.settingspanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'FontUnits','normalized','FontSize',0.67,...
        'HorizontalAlignment','left', ...
	'Position',[.01,.01,.39,.30],...
	'String','Bounding Box:',...
	'Style','text');

% Input the bounding box
TCM_UI.rectinput=uicontrol('Units','normalized', 'Parent',TCM_UI.settingspanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'FontWeight','bold',...
        'FontUnits','normalized','FontSize',0.65,'FontName',TCM_UI.mono,...
        'HorizontalAlignment','left', ...
	'Position',[.35,.01,.65,.30],...
	'String','[0 1 0 1]',...
	'Style','edit','Callback',@rectangle_update);

% Text for the constraint
TCM_UI.context=uicontrol('Units','normalized','Parent',TCM_UI.settingspanel,...
        'BackgroundColor',get(gcf,'Color'), ...
        'HorizontalAlignment','left', ...
        'FontUnits','normalized','FontSize',0.7,...
	'Position',[.01,.35,.29,.3],...
	'String','Constraint:',...
	'Style','text');

% Popup for choice of integration points
TCM_UI.coninput=uicontrol('Units','normalized', 'Parent',TCM_UI.settingspanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'FontWeight','bold',...
        'FontUnits','normalized','FontSize',0.65,'FontName',TCM_UI.mono,...
        'HorizontalAlignment','left', ...
	'Position',[.35,.35,.65,.30],...
	'String','@(x,y) x.^2+y.^2',...
	'Style','edit',...
	'Callback',@constraint_update);

%%%%%%%%%%%%%%%%%%%%%%%%% UI-Elements in Buttons Panel %%%%%%%%%%%%%%%%%%%%
% Reset-Button
TCM_UI.reset=uicontrol('Units','normalized','Parent',TCM_UI.buttonspanel, ...
	'BackgroundColor',get(gcf,'Color'), ...
	'String','Reset', ...
    'FontUnits','normalized','FontSize',0.7,...
	'Position',[.01,.70,.98,.30], ...
	'Callback',@reset,...
	'Style','pushbutton');

% Example button
TCM_UI.examples=uicontrol('Units','normalized','Parent',TCM_UI.buttonspanel, ...
	'BackgroundColor',get(gcf,'Color'), ...
	'String','Examples', ...
    'FontUnits','normalized','FontSize',0.7,...
	'Position',[.01,.35,.98,.30], 'Callback',@OpenExampleMenu,...
	'Style','pushbutton');

% Next button
TCM_UI.next=uicontrol('Units','normalized','Parent',TCM_UI.buttonspanel, ...
	'BackgroundColor',get(gcf,'Color'), ...
	'String','Next Point', ...
    'FontUnits','normalized','FontSize',0.7,...
	'Position',[.01,0,.98,.30], ...
	'Callback',@examples,...
	'Style','pushbutton','Callback',@next);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% UI-Elements in Feedback %%%%%%%%%%%%%%%%%%%%

% Text for the current point
TCM_UI.pointtext=uicontrol('Units','normalized','Parent',TCM_UI.feedbackpanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'HorizontalAlignment','left', ...
        'FontUnits','normalized','FontSize',0.7,...
	'Position',[.1,.7,1,.3],...
	'String','Point: NaN',...
	'Style','text');

% Text for the function value
TCM_UI.valuetext=uicontrol('Units','normalized','Parent',TCM_UI.feedbackpanel, ...
        'BackgroundColor',get(gcf,'Color'), ...
        'HorizontalAlignment','left', ...
        'FontUnits','normalized','FontSize',0.7,...
	'Position',[.1,.35,1,.3],...
	'String','Functionvalue: NaN',...
	'Style','text');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% UI-Elements in Axis %%%%%%%%%%%%%%%%%%%%%%%%
% Axis for Function display
TCM_UI.ax=axes('Position',[.05,.1,.45,.85],'Parent',TCM_UI.axispanel);
axis(TCM_UI.ax,'equal');
TCM_UI.modification = uicontextmenu('Callback',@reset_zoom_limits);  
TCM_UI.mpan = uimenu('Parent',TCM_UI.modification,'Label','Enable pan',...
        'Callback',@axis_context);
TCM_UI.mzoomi = uimenu('Parent',TCM_UI.modification,'Label','Enable zoom in',...
    'Callback',@axis_context); 
TCM_UI.mzoomo = uimenu('Parent',TCM_UI.modification,'Label','Enable zoom out',...
        'Callback',@axis_context); 
TCM_UI.ax.UIContextMenu = TCM_UI.modification;

% functions for smoothless zooming and panning
panfun = pan(TCM_UI.fig);
panfun.UIContextMenu = TCM_UI.modification;
set(panfun,'ActionPostCallback',@pan_modify);
zoomfun = zoom(TCM_UI.fig);
zoomfun.UIContextMenu = TCM_UI.modification; 
set(zoomfun,'ActionPostCallback',@zoom_modify);
set(zoomfun,'ButtonDownFilter',@set_zoom_limits);
set(zoomfun,'RightClickAction','PostContextMenu');
set(findall(TCM_UI.fig,'tag','Exploration.Pan'),'enable','off');
set(findall(TCM_UI.fig,'tag','Exploration.ZoomOut'),'enable','off');
set(findall(TCM_UI.fig,'tag','Exploration.ZoomIn'),'enable','off');

% Second axis for a 3D plot with its own Contextmenu
TCM_UI.ax2=axes('Position',[.55,.1,.44,.85],'Parent',TCM_UI.axispanel);
linkaxes([TCM_UI.ax, TCM_UI.ax2], 'xy');
axis equal
TCM_UI.rotatemenu = uicontextmenu;
TCM_UI.rotate = uimenu('Parent',TCM_UI.rotatemenu,'Label','Enable rotate',...
        'Callback',@axis2_context);
TCM_UI.ax2.UIContextMenu = TCM_UI.rotatemenu;
rotatefun = rotate3d(TCM_UI.ax2);
rotatefun.UIContextMenu = TCM_UI.rotatemenu;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Start the program %%%%%%%%%%%%%%%%%%%%%%%%%%
changeexample(1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Callback functions %%%%%%%%%%%%%%%%%%%%%%%%%%%

% new function input
function function_update(varargin)
TCM_Data.funstring = get(TCM_UI.funinput,'String');
var1 = TCM_Data.funstring(find(TCM_Data.funstring=='(',1)+1:...
    find(TCM_Data.funstring==',',1)-1);
var2 = TCM_Data.funstring(find(TCM_Data.funstring==',',1)+1:...
    find(TCM_Data.funstring==')',1)-1);


TCM_Data.fun = eval(TCM_Data.funstring);
try
    [TCM_Data.fdeg,TCM_Data.fcoeff] = multi_poly(TCM_Data.fun);
catch
    disp('No polynomial input');
end
TCM_Data.fun = eval([TCM_Data.funstring '+0*' var1 '+0*' var2]);
reset;
end

% new constraint input
function constraint_update(varargin)
TCM_Data.constring = get(TCM_UI.coninput,'String');
var1 = TCM_Data.funstring(find(TCM_Data.funstring=='(',1)+1:...
    find(TCM_Data.funstring==',',1)-1);
var2 = TCM_Data.funstring(find(TCM_Data.funstring==',',1)+1:...
    find(TCM_Data.funstring==')',1)-1);

TCM_Data.con = eval(TCM_Data.constring);  
try
    [TCM_Data.gdeg,TCM_Data.gcoeff] = multi_poly(TCM_Data.con);
catch
    disp('No polynomial input');
end
TCM_Data.con = eval([TCM_Data.constring '+0*' var1 '+0*' var2]);
reset;
end

% rectangle input
function rectangle_update(varargin)
rect = eval(get(TCM_UI.rectinput,'String'));
h = diff(rect);
if h(1) > 0 && h(3) > 0
    TCM_Data.rect = rect;
    TCM_Data.plot_window = TCM_Data.rect + [-h(1) h(1) -h(3) h(3)];
else
    set(TCM_UI.rectinput,'String',['[' num2str(TCM_Data.rect) ']']);
end
reset;
end

% cycle through the minimal points
function next(varargin)
TCM_Data.selected = TCM_Data.selected + 1;
myplot;
end

% refresh all data from UI
function reset(varargin)
window_disable.Label = 'Disable pan';
axis_context(window_disable);
axis2_context(window_disable);
TCM_Data.selected = 1;
myplot;
end

% mouse button pressed 
function myDownfct(varargin)
p = getClickPosition(TCM_UI.ax);
if isnan(p)
    return
end
if strcmp(get(gcf,'SelectionType'),'normal')    
    h = diff(TCM_Data.plot_window);
    maxd = 0.05*sqrt(h(1)^2+h(3)^2);
    
    if abs(p(1)-TCM_Data.rect(1))<maxd && p(2)>= TCM_Data.rect(3) && p(2)<= TCM_Data.rect(4)
        TCM_Data.move = 1;
        TCM_Data.rect(1) = p(1);
    elseif abs(p(1)-TCM_Data.rect(2))<maxd && p(2)>= TCM_Data.rect(3) && p(2)<= TCM_Data.rect(4)
        TCM_Data.move = 2;
        TCM_Data.rect(2) = p(1);       
    elseif abs(p(2)-TCM_Data.rect(3))<maxd && p(1)>= TCM_Data.rect(1) && p(1)<= TCM_Data.rect(2)
        TCM_Data.move = 3;
        TCM_Data.rect(3) = p(2);  
    elseif abs(p(2)-TCM_Data.rect(4))<maxd && p(1)>= TCM_Data.rect(1) && p(1)<= TCM_Data.rect(2)
        TCM_Data.move = 4;
        TCM_Data.rect(4) = p(2);         
    end
    
    if TCM_Data.move > 0
        myplot();
        set(TCM_UI.rectinput,'String',sprintf('[%.3f %.3f %.3f %.3f]',TCM_Data.rect(1),TCM_Data.rect(2),...
            TCM_Data.rect(3),TCM_Data.rect(4)));
    end
end
end

% moving the mouse in the axis
function myMovefct(varargin)
if TCM_Data.move > 0
    p = getClickPosition(TCM_UI.ax);
    if(isnan(p))
       return;
    end
    switch TCM_Data.move
        case 1
            TCM_Data.rect(1) = p(1);
        case 2
            TCM_Data.rect(2) = p(1);
        case 3
            TCM_Data.rect(3) = p(2);
        case 4
            TCM_Data.rect(4) = p(2);
    end
    myplot();
    set(TCM_UI.rectinput,'String',sprintf('[%.3f %.3f %.3f %.3f]',TCM_Data.rect(1),TCM_Data.rect(2),...
        TCM_Data.rect(3),TCM_Data.rect(4)));
end
end

% releasing a mouse button
function myUpfct(varargin)
TCM_Data.move = 0;
myplot;
end

function p = getClickPosition(myaxes)
x = get(myaxes,'xlim');
y = get(myaxes,'ylim');
newpoint=get(myaxes,'CurrentPoint');
if (newpoint(1) < x(1) || newpoint(1) > x(2) || ...
    newpoint(3) < y(1) || newpoint(3) > y(2))
    p = NaN;
else
    p = newpoint([1 3]);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% kernel functions %%%%%%%%%%%%%%%%%%%%%%%%%%%
function calculate_minima(varargin)
warning off
if isempty(TCM_Data.fdeg) || isempty(TCM_Data.gdeg) || isempty(TCM_Data.rect)
    return
end
bivariate_interpolation();
crit_points = compute_common_roots(TCM_Data.gdeg,TCM_Data.gcoeff,TCM_Data.hdeg,TCM_Data.hcoeff);

deg = TCM_Data.gdeg; coeff = TCM_Data.gcoeff;
xdeg = [deg(1,:)-1;deg(2,:)]; xcoeff = coeff.*deg(1,:);
del = find(xdeg(1,:)==-1); xdeg(:,del) = []; xcoeff(del) = [];
ydeg = [deg(1,:);deg(2,:)-1]; ycoeff = coeff.*deg(2,:);
del = find(ydeg(2,:)==-1); ydeg(:,del) = []; ycoeff(del) = [];

sing_points = compute_common_roots(deg,coeff,xdeg,xcoeff);
for i=1:size(sing_points,2)
    ydervalue = ycoeff*(sing_points(1,i).^ydeg(1,:)'.*sing_points(2,i).^ydeg(2,:)');
    if abs(ydervalue) <= 10^-6
        crit_points(:,end+1) = sing_points(:,i);
    end
end

if ~isempty(crit_points)
    crit_points = [crit_points(:,crit_points(1,:) >= TCM_Data.rect(1) & crit_points(1,:) <= TCM_Data.rect(2) & ...
        crit_points(2,:) >= TCM_Data.rect(3) & crit_points(2,:) <= TCM_Data.rect(4)) boundary_intersections()];
else
    crit_points = boundary_intersections();
end
values = TCM_Data.fun(crit_points(1,:),crit_points(2,:));
[val,~] = min(values);
TCM_Data.minima = crit_points(:,abs(values-val)<10^-6);
warning on
end

% calculate the critical points
function root = compute_common_roots(deg1,coeff1,deg2,coeff2)   
dR = max(deg1(1,:))*max(deg2(2,:)) + max(deg1(2,:))*max(deg2(1,:));
if dR == 0
    if (isequal(deg1,[0;0]) && coeff1 ~= 0) || (isequal(deg2,[0;0]) && coeff2 ~= 0)
        TCM_Data.crit_points = [];
        return
    end
    if isequal(deg1,[0;0]) && coeff1 == 0 
        ypoly = zeros(1,max(deg2(2,:))+1);
        for i=1:numel(coeff2)
            ypoly(deg2(2,i)+1) = ypoly(deg2(2,i)+1) + coeff2(i);
        end
        root = roots(fliplr(ypoly));
        root(abs(imag(root))>10^-6) = [];
        root = [0*root; root];
        if isempty(root) && isequal(ypoly,0)
            root = [0;0];
        end
        return
    end
    if isequal(deg2,[0;0]) && coeff2 == 0  
        xpoly = zeros(1,max(deg1(1,:))+1);
        for i=1:numel(coeff1)
            xpoly(deg1(1,i)+1) = xpoly(deg1(1,i)+1) + coeff1(i);
        end
        root = roots(fliplr(xpoly));
        root(abs(imag(root))>10^-6) = [];
        root = [root;0*root];
        if isempty(root) && isequal(xpoly,0)
            root = [0;0];
        end
        return
    end
end
points = 0:dR;
yR = 0*points;

xmax = max(deg1(2,:)); 
ymax = max(deg2(2,:));
Res = zeros(xmax+ymax);

for i=1:numel(points)
    xpoly = zeros(1,max(deg1(2,:))+1);
    ypoly = zeros(1,max(deg2(2,:))+1);
    for j=1:numel(coeff1)
        xpoly(deg1(2,j)+1) = xpoly(deg1(2,j)+1) + coeff1(j)*points(i)^deg1(1,j);
    end
    for j=1:numel(coeff2)
        ypoly(deg2(2,j)+1) = ypoly(deg2(2,j)+1) + coeff2(j)*points(i)^deg2(1,j);
    end
    xpoly = fliplr(xpoly);
    ypoly = fliplr(ypoly);

    for j=1:ymax
        Res(j,j+[0:numel(xpoly)-1]) = xpoly;
    end
    for j=1:xmax
        Res(ymax+j,j+[0:numel(ypoly)-1]) = ypoly;
    end
    yR(i) = det(Res);
end
R = round(polyfit(points,yR,dR),6);
zero = unique(round(roots(R),5));
zero(abs(imag(zero))>10^-6) = [];
if isempty(zero) && max(abs(R))<10^-6
    zero = 0;
end

totalzeros = {};
for i=1:numel(zero)
    newcoeff1 = coeff1.*(zero(i).^deg1(1,:));
    f1 = zeros(1,max(deg1(2,:))+1);
    for j=1:numel(deg1(2,:))
        f1(deg1(2,j)+1) = f1(deg1(2,j)+1) + newcoeff1(j);
    end
    xzeros = roots(fliplr(f1));
    xzeros(abs(imag(xzeros))>10^-6) = [];
    
    newcoeff2 = coeff2.*(zero(i).^deg2(1,:));
    f2 = zeros(1,max(deg2(2,:))+1);
    for j=1:numel(deg2(2,:))
        f2(deg2(2,j)+1) = f2(deg2(2,j)+1) + newcoeff2(j);
    end
    yzeros = roots(fliplr(f2));
    yzeros(abs(imag(yzeros))>10^-6) = [];
    
    if ~isempty(xzeros) && isempty(yzeros) && max(abs(f2))<10^-6
        totalzeros{i} = xzeros;
    elseif ~isempty(yzeros) && isempty(xzeros) && max(abs(f1))<10^-6
        totalzeros{i} = yzeros;
    else
        totalzeros{i} = intersect(xzeros,yzeros);
    end
end
root = [];
for i=1:numel(totalzeros)
    for j=1:numel(totalzeros{i})
        if abs(imag(zero(i))) < 10^-6 && abs(imag(totalzeros{i}(j))) < 10^-6
            root = [root real([zero(i); totalzeros{i}(j)])];
        end
    end
end
end

% convert a function handle input into an adequate internal representation
function [degree,coeff] = multi_poly(p)
ps = func2str(p);
var1 = ps(find(ps=='(',1)+1:find(ps==',',1)-1);
var2 = ps(find(ps==',',1)+1:find(ps==')',1)-1);
ps = ps(find(ps==')',1)+1:end);
plus = find(ps=='+');
minus = find(ps=='-');
terms = union(plus,minus);

if isempty(terms) || terms(1) ~= 1
    terms = [1 terms];
end
terms = [terms numel(ps)+1];

coeff = zeros(1,numel(terms)-1);
degree = zeros(2,numel(terms)-1);
for i=1:numel(terms)-1
    term = ps(terms(i):terms(i+1)-1);
    if strcmp(term(1),'+') || strcmp(term(1),'-')
        sign = term(1);
        term = [term(2:end) blanks(max(numel(var1),numel(var2)))];
        length = numel(term)-1;
    else
        sign = '+';
        length = numel(term);
    end
    
    ind = find(term=='*',1)-1;
    if isempty(ind) 
        if strcmp(term(1:numel(var1)),var1) || strcmp(term(1:numel(var2)),var2)
            coeffstr = '1';
        else
            coeffstr = term;
        end
        ind = -1;        
    else
        if strcmp(term(1:numel(var1)),var1) || strcmp(term(1:numel(var2)),var2)
            coeffstr = '1';
            ind = -1; 
        else
            coeffstr = term(1:ind);
        end
    end
    
    if coeffstr(end) == '.'
        coeffstr = coeffstr(1:end-1);
    end
    
    coeff(i) = str2double([sign coeffstr]);
    term = term(1:length);
    degree(:,i) = extract_degree(term(ind+2:end),var1,var2);
end

end

% extract the degrees (a,b) of a term in the form var1.^a.*var2.^b
function degree = extract_degree(string,var1,var2)
degree = [0 0];
if isempty(string)
    return
end
mult = strfind(string,'.*');
string([mult mult+1]) = '';
v1 = [strfind(string, var1) numel(string)+1];
v2 = [strfind(string, var2) numel(string)+1];
l1 = numel(var1); l2 = numel(var2);
string = [string blanks(2)];

for i=1:numel(v1)-1
    if strcmp('.^',string(v1(i)+l1+[0:1]))
        degree(1) = degree(1)+str2double(string(v1(i)+l1+2:(min(v1(i+1),v2(find(v2>v1(i),1)))-1)));
    else 
        degree(1) = degree(1)+1;
    end
end

for i=1:numel(v2)-1
    if strcmp('.^',string(v2(i)+l2+[0:1]))
        degree(2) = degree(2)+str2double(string(v2(i)+l2+2:(min(v2(i+1),v1(find(v1>v2(i),1)))-1)));
    else 
        degree(2) = degree(2)+1;
    end
end
end

function points = boundary_intersections(varargin)
points = [];
for i=1:2
    coeff = TCM_Data.gcoeff.*TCM_Data.rect(2+i).^TCM_Data.gdeg(2,:);
    poly = zeros(1,max(TCM_Data.gdeg(1,:))+1);
    for j=1:numel(coeff)
        poly(TCM_Data.gdeg(1,j)+1) = poly(TCM_Data.gdeg(1,j)+1) + coeff(j);
    end
    poly = fliplr(poly);
    crit = unique(roots(poly)');
    crit(abs(imag(crit))>10^-6) = [];
    crit(crit < TCM_Data.rect(1) | crit > TCM_Data.rect(2)) = [];
    if max(abs(poly)) < 10^-8
        crit = linspace(TCM_Data.rect(1),TCM_Data.rect(2));
    end
    points = [points [crit; TCM_Data.rect(2+i)*ones(1,numel(crit))]];
end

for i=1:2
    coeff = TCM_Data.gcoeff.*TCM_Data.rect(i).^TCM_Data.gdeg(1,:);
    poly = zeros(1,max(TCM_Data.gdeg(2,:))+1);
    for j=1:numel(coeff)
        poly(TCM_Data.gdeg(2,j)+1) = poly(TCM_Data.gdeg(2,j)+1) + coeff(j);
    end
    poly = fliplr(poly);
    crit = unique(roots(poly)');
    crit(abs(imag(crit))>10^-6) = [];
    crit(crit < TCM_Data.rect(3) | crit > TCM_Data.rect(4)) = [];
    if max(abs(poly)) < 10^-8
        crit = linspace(TCM_Data.rect(3),TCM_Data.rect(4));
    end
    points = [points [TCM_Data.rect(i)*ones(1,numel(crit)); crit]];
end
end

function bivariate_interpolation(varargin)
deg = TCM_Data.fdeg; coeff = TCM_Data.fcoeff;
fxdeg = [deg(1,:)-1;deg(2,:)]; fxcoeff = coeff.*deg(1,:);
del = find(fxdeg(1,:)==-1); fxdeg(:,del) = []; fxcoeff(del) = [];
if isempty(fxdeg)
    fxdeg = [0;0]; fxcoeff = 0;
end
fydeg = [deg(1,:);deg(2,:)-1]; fycoeff = coeff.*deg(2,:);
del = find(fydeg(2,:)==-1); fydeg(:,del) = []; fycoeff(del) = [];
if isempty(fydeg)
    fydeg = [0;0]; fycoeff = 0;
end

deg = TCM_Data.gdeg; coeff = TCM_Data.gcoeff;
gxdeg = [deg(1,:)-1;deg(2,:)]; gxcoeff = coeff.*deg(1,:);
del = find(gxdeg(1,:)==-1); gxdeg(:,del) = []; gxcoeff(del) = [];
if isempty(gxdeg)
    gxdeg = [0;0]; gxcoeff = 0;
end
gydeg = [deg(1,:);deg(2,:)-1]; gycoeff = coeff.*deg(2,:);
del = find(gydeg(2,:)==-1); gydeg(:,del) = []; gycoeff(del) = [];
if isempty(gydeg)
    gydeg = [0;0]; gycoeff = 0;
end

hdegx = max(max(fxdeg(1,:))+max(gydeg(1,:)),max(fydeg(1,:))+max(gxdeg(1,:)));
hdegy = max(max(fxdeg(2,:))+max(gydeg(2,:)),max(fydeg(2,:))+max(gxdeg(2,:)));

hdeg = zeros(2,(hdegx+1)*(hdegy+1));
for i=0:hdegx
    for j=0:hdegy
        hdeg(:,i*(hdegy+1)+j+1) = [i;j];
    end
end

[X,Y] = meshgrid(0:hdegx,0:hdegy);
X=X(:); Y = Y(:);
A = zeros(numel(X));
y = zeros(size(X));

for i=1:numel(X)
    y(i) = det([fxcoeff*(X(i).^fxdeg(1,:)'.*Y(i).^fxdeg(2,:)') ...
        fycoeff*(X(i).^fydeg(1,:)'.*Y(i).^fydeg(2,:)'); ...
        gxcoeff*(X(i).^gxdeg(1,:)'.*Y(i).^gxdeg(2,:)') ...
        gycoeff*(X(i).^gydeg(1,:)'.*Y(i).^gydeg(2,:)')]);
    A(i,:) = X(i).^hdeg(1,:).*Y(i).^hdeg(2,:);
end
hcoeff = (A\y)';
ind = abs(hcoeff) > 10^-8;
if max(ind) == 0
    TCM_Data.hcoeff = 0;
    TCM_Data.hdeg = [0;0];
else
    TCM_Data.hcoeff = hcoeff(ind); TCM_Data.hdeg = hdeg(:,ind);
end
end


% plot function
function myplot(varargin)
axes(TCM_UI.ax)
cla reset; hold off; axis equal

calculate_minima();

% evaluating and plotting of the input function on a square containing the
% current window for smoothless panning and zooming
hx = TCM_Data.plot_window(2) - TCM_Data.plot_window(1);
x = linspace(TCM_Data.plot_window(1)-hx,TCM_Data.plot_window(2)+hx,100);
hy = TCM_Data.plot_window(4) - TCM_Data.plot_window(3);
y = linspace(TCM_Data.plot_window(3)-hy,TCM_Data.plot_window(4)+hy,100);
[X,Y] = meshgrid(x(x>=TCM_Data.plot_window(1) & x<=TCM_Data.plot_window(2)),...
    y(y>=TCM_Data.plot_window(3) & y<=TCM_Data.plot_window(4)));
f = TCM_Data.fun(X,Y);
[~,h1]=contour(X,Y,f);
Level1 = h1.LevelList;

if ~isempty(get(TCM_UI.ax2, 'Children'))
    [az,el]=view(TCM_UI.ax2);
else
    az = NaN;
end
cla(TCM_UI.ax2,'reset');
s = surf(TCM_UI.ax2,X,Y,f,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
s.UIContextMenu = TCM_UI.rotatemenu;
shading(TCM_UI.ax2,'interp');
hold(TCM_UI.ax2,'on');
xlim(TCM_UI.ax2,[TCM_Data.plot_window(1) TCM_Data.plot_window(2)]);
ylim(TCM_UI.ax2,[TCM_Data.plot_window(3) TCM_Data.plot_window(4)]);
if ~isnan(az)
    view(TCM_UI.ax2,az,el);
end

[X,Y] = meshgrid(x,y);
f = TCM_Data.fun(X,Y);
[~,h2]=contour(X,Y,f);
Level2 = h2.LevelList;
contour(X,Y,f,union(Level1,Level2));
hold on
xlim([TCM_Data.plot_window(1) TCM_Data.plot_window(2)]);
ylim([TCM_Data.plot_window(3) TCM_Data.plot_window(4)]);

if ~isempty(TCM_Data.rect)
    plot(TCM_UI.ax,TCM_Data.rect([1 2 2 1 1]),TCM_Data.rect([3 3 4 4 3]),'black',...
        'LineWidth',1);
    x = linspace(TCM_Data.rect(1),TCM_Data.rect(2));
    y = linspace(TCM_Data.rect(3),TCM_Data.rect(4));
    plot3(TCM_UI.ax2,x,TCM_Data.rect(3)*ones(size(x)),TCM_Data.fun(x,TCM_Data.rect(3)*ones(size(x))),...
        'black','LineWidth',1);
    plot3(TCM_UI.ax2,x,TCM_Data.rect(4)*ones(size(x)),TCM_Data.fun(x,TCM_Data.rect(4)*ones(size(x))),...
        'black','LineWidth',1);
    plot3(TCM_UI.ax2,TCM_Data.rect(1)*ones(size(x)),y,TCM_Data.fun(TCM_Data.rect(1)*ones(size(x)),y),...
        'black','LineWidth',1);
    plot3(TCM_UI.ax2,TCM_Data.rect(2)*ones(size(x)),y,TCM_Data.fun(TCM_Data.rect(2)*ones(size(x)),y),...
        'black','LineWidth',1);  
end

x = linspace(TCM_Data.plot_window(1)-hx,TCM_Data.plot_window(2)+hx,500);
y = linspace(TCM_Data.plot_window(3)-hy,TCM_Data.plot_window(4)+hy,500);
[X,Y] = meshgrid(x,y);
g = TCM_Data.con(X,Y);
limz = zlim(TCM_UI.ax2);
[C,~] = contour(TCM_UI.ax,X,Y,g,[-10^-6 10^-6],'red','LineWidth',1);
counter = 1;
while counter < size(C,2)
    plot3(TCM_UI.ax2,C(1,counter+[1:round(C(2,counter))]),C(2,counter+[1:round(C(2,counter))]),...
        TCM_Data.fun(C(1,counter+[1:round(C(2,counter))]),C(2,counter+[1:round(C(2,counter))])),'red','LineWidth',1);
    counter = counter + round(C(2,counter)) + 1;
end
zlim(TCM_UI.ax2,limz);

if ~isempty(TCM_Data.minima)
    if TCM_Data.selected > size(TCM_Data.minima,2)
        TCM_Data.selected = 1;
    end
    
    value = TCM_Data.fun(TCM_Data.minima(1,TCM_Data.selected),TCM_Data.minima(2,TCM_Data.selected));
    plot(TCM_UI.ax,TCM_Data.minima(1,:),TCM_Data.minima(2,:),'ored','MarkerSize',7,'MarkerFaceColor','red');
    scatter3(TCM_UI.ax2,TCM_Data.minima(1,:),TCM_Data.minima(2,:),value*ones(size(TCM_Data.minima(1,:))),'red','filled');    
    
    plot(TCM_UI.ax,TCM_Data.minima(1,TCM_Data.selected),TCM_Data.minima(2,TCM_Data.selected),'oblack','MarkerSize',12,'LineWidth',1.5);
    scatter3(TCM_UI.ax2,TCM_Data.minima(1,TCM_Data.selected),TCM_Data.minima(2,TCM_Data.selected),value,'black','filled');
    
    TCM_UI.pointtext.String = sprintf('Marked Point: [%.3f,%.3f]',TCM_Data.minima(1,TCM_Data.selected),TCM_Data.minima(2,TCM_Data.selected));
    TCM_UI.valuetext.String = sprintf('Functionvalue: %.3f',value);
else
    TCM_UI.pointtext.String = 'No point found';
    TCM_UI.valuetext.String = '';
end

SetWindowLimits;
end

% Update the Hinttext
function UpdateHinttext
LmbString = 'Left Mouse = Move boundaries. ';
if(numel(TCM_Data.LmbMenuString) == 0)
    set(TCM_UI.hinttext,'String',[LmbString 'Right Mouse = Access window menus.']);
else
    set(TCM_UI.hinttext,'String',[TCM_Data.LmbMenuString 'Right Mouse = Access window menus.']);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% example functions %%%%%%%%%%%%%%%%%%%%%%%%%%%

function CreateExampleMenu
TCM_UI.UICexamples = uicontextmenu;
for i=1:numel(TCM_Data.examples)
    uimenu('Parent',TCM_UI.UICexamples,'Label',TCM_Data.examples{i},'Callback',{@changeexample,i});
end
end

function OpenExampleMenu(varargin)
TCM_UI.examples.Units = 'pixels';
TCM_UI.buttonspanel.Units = 'pixels';
TCM_UI.UICexamples.Position = TCM_UI.examples.Position(1:2) + TCM_UI.buttonspanel.Position(1:2);
TCM_UI.examples.Units = 'normalized';
TCM_UI.buttonspanel.Units = 'points';
TCM_UI.UICexamples.Visible = 'on';
end
 
              
function changeexample(varargin) 
switch varargin{numel(varargin)}      
    case 1
        set(TCM_UI.funinput,'String','@(x,y) x.^2+y.^2');
        set(TCM_UI.coninput,'String','@(x,y) x.*y-1');
        TCM_UI.rectinput.String = '[-1.75 0.5 -1.5 0.5]';
        function_update; 
        constraint_update;
        rectangle_update;
        view(TCM_UI.ax2,88,36);
    case 2
        set(TCM_UI.funinput,'String','@(x,y) x.*y+x.^2');
        set(TCM_UI.coninput,'String','@(x,y) x.^3-y.^2');
        TCM_UI.rectinput.String = '[-1. 0.5 -1.5 0.5]';
        function_update; 
        constraint_update;
        rectangle_update;
        view(TCM_UI.ax2,15,31);
    case 3
        set(TCM_UI.funinput,'String','@(x,y) y');
        set(TCM_UI.coninput,'String','@(x,y) 2*x.^4-x.^2+5-y');
        TCM_UI.rectinput.String = '[-10 10 10 20]';
        function_update; 
        constraint_update;
        rectangle_update;
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% zoom functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% function for updating the internal plot window
function zoom_modify(varargin)
if max(abs(TCM_Data.plot_window-axis(TCM_UI.ax)))< 10^-10
    ZoomOut;
end
pan_modify;
end

function pan_modify(varargin)
TCM_Data.plot_window = axis(TCM_UI.ax);
myplot;
end

% functions for proper zooming in (prevent double-click zoom out)
function [flag] = set_zoom_limits(varargin)
if(strcmp(TCM_UI.fig.SelectionType,'open'))
    flag = true;
else
    flag = false;
end
end

function reset_zoom_limits(varargin)
SetWindowLimits;
end

% handling the axis context menu
function axis_context(varargin)
zoom(TCM_UI.fig, 'off'); pan(TCM_UI.fig, 'off');
mode_strings = {'pan','zoom in','zoom out'};
status_strings = {'Enable ','Disable '};
switch varargin{1}.Label(1:7)
    case 'Enable '
        mode = strcmp(varargin{1}.Label(8:end),mode_strings);
        TCM_Data.LmbMenuString = ['Left Mouse = ' mode_strings{mode} ' the window. '];
        set(TCM_UI.mpan,'Label',horzcat(status_strings{mode(1)+1},mode_strings{1}));
        set(TCM_UI.mzoomi,'Label',horzcat(status_strings{mode(2)+1},mode_strings{2}));
        set(TCM_UI.mzoomo,'Label',horzcat(status_strings{mode(3)+1},mode_strings{3}));
        split_mode = strsplit(mode_strings{mode},' ');
        if numel(split_mode) == 1
            pan(TCM_UI.fig, 'on');
        else
            zoomh = zoom(TCM_UI.fig);
            zoomh.Enable = 'on';
            zoomh.Direction = split_mode{2};
        end
    case 'Disable'
        TCM_Data.LmbMenuString = '';
        set(TCM_UI.mpan,'Label','Enable pan');
        set(TCM_UI.mzoomi,'Label','Enable zoom in');
        set(TCM_UI.mzoomo,'Label','Enable zoom out');     
end
UpdateHinttext;
end

function axis2_context(varargin)
if strcmp(varargin{1}.Label,'Enable rotate')
    set(TCM_UI.rotate,'Label','Disable rotate');
    TCM_Data.LmbMenuString = 'Left Mouse = Rotate the Box. ';
    rotate3d(TCM_UI.ax2,'on');
else
    rotate3d(TCM_UI.ax2,'off');
    set(TCM_UI.rotate,'Label','Enable rotate');
    TCM_Data.LmbMenuString = '';
end
UpdateHinttext;
end

function SetWindowLimits
ZoomOut;
xlim(TCM_UI.ax,[TCM_Data.plot_window(1) TCM_Data.plot_window(2)]);
ylim(TCM_UI.ax,[TCM_Data.plot_window(3) TCM_Data.plot_window(4)]);
end

function ZoomOut
hx = TCM_Data.plot_window(2) - TCM_Data.plot_window(1);
hy = TCM_Data.plot_window(4) - TCM_Data.plot_window(3);
xlim(TCM_UI.ax,[TCM_Data.plot_window(1)-hx/2 TCM_Data.plot_window(2)+hx/2]);
ylim(TCM_UI.ax,[TCM_Data.plot_window(3)-hy/2 TCM_Data.plot_window(4)+hy/2]);
zoom(TCM_UI.fig, 'reset');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% layout functions %%%%%%%%%%%%%%%%%%%%%%%%%%%

% functions for creating the different UI-Parts and managing the resize of
% the window
function CreateUI
TCM_UI.fig = figure('Visible','off', 'Units','points','Toolbar','none',...
    'SizeChangedFcn',@calculate_layout, 'WindowButtonDownFcn',@myDownfct,'WindowButtonMotionFcn',...
    @myMovefct,'WindowButtonUpFcn',@myUpfct,'numbertitle','off','name',TCM_UI.Title);
TCM_UI.titletext = uicontrol('Units','points','BackgroundColor',TCM_UI.fig.Color, ...
	'HorizontalAlignment','center',  'FontWeight','bold', ...
    'FontUnits','normalized',        'FontSize',0.7,...
	'String',TCM_UI.Title,        'Style','text');
TCM_UI.hinttext = uicontrol('Units','points','BackgroundColor',TCM_UI.fig.Color, ...
    'HorizontalAlignment','right',...
    'FontUnits','normalized',        'FontSize',0.635,...
	'String','','Style','text');
TCM_UI.infobutton=uicontrol('Units','points', ...
	'BackgroundColor',TCM_UI.fig.Color, ...
	'String','Info', 'HorizontalAlignment','left',...
    'FontUnits','normalized','FontSize',0.7,...
	'Callback',@information,'Style','pushbutton');
TCM_UI.feedbackpanel = uipanel('Units','points','BackgroundColor',TCM_UI.fig.Color,'BorderType','none');
TCM_UI.buttonspanel = uipanel('Units','points','BackgroundColor',TCM_UI.fig.Color,'BorderType','none');
TCM_UI.settingspanel = uipanel('Units','points','BackgroundColor',TCM_UI.fig.Color,'BorderType','none');
TCM_UI.axispanel = uipanel('Units','points','BackgroundColor',TCM_UI.fig.Color,'BorderType','none');
CreateExampleMenu
TCM_UI.fig.Visible= 'on';
end


function calculate_layout(varargin) 
figpos = TCM_UI.fig.Position;
WMax = TCM_UI.SettingsHeightMax*(TCM_UI.SettingsRatio + TCM_UI.FeedbackRatio + TCM_UI.ButtonsRatio);
WMin = TCM_UI.SettingsHeightMin*(TCM_UI.SettingsRatio + TCM_UI.FeedbackRatio + TCM_UI.ButtonsRatio);
HMax = TCM_UI.TitleHeightMax + TCM_UI.HintHeightMax;
HMin = TCM_UI.TitleHeightMin + TCM_UI.HintHeightMin;

wratio = max(0,min(1,(min(figpos(3),1.3*figpos(4))-WMin)/(WMax-WMin)));
XCoords = [1,0,0,0,0,0,0,figpos(3)];
SWidth =      wratio*TCM_UI.SettingsHeightMax*TCM_UI.SettingsRatio...
        + (1-wratio)*TCM_UI.SettingsHeightMin*TCM_UI.SettingsRatio;
BWidth =      wratio*TCM_UI.SettingsHeightMax*TCM_UI.ButtonsRatio...
        + (1-wratio)*TCM_UI.SettingsHeightMin*TCM_UI.ButtonsRatio; 
FWidth =      wratio*TCM_UI.SettingsHeightMax*TCM_UI.FeedbackRatio...
        + (1-wratio)*TCM_UI.SettingsHeightMin*TCM_UI.FeedbackRatio; 
RemWidth = figpos(3)-3-SWidth-FWidth-BWidth;
XCoords(2) = max(1,0.3*RemWidth);
XCoords(3) = XCoords(2) + 1 + SWidth;
XCoords(4) = XCoords(3) + max(0,0.2*RemWidth);
XCoords(5) = XCoords(4) + 1 + BWidth;
XCoords(6) = XCoords(5) + max(0,0.2*RemWidth);
XCoords(7) = XCoords(6) + 1 + FWidth;
SHeight = wratio*TCM_UI.SettingsHeightMax + (1-wratio)*TCM_UI.SettingsHeightMin;
if(HMin > min(figpos(4),0.9*figpos(3))*(1-TCM_UI.AxisHeightTarget)-5-SHeight)
    hratio = 0;
elseif(HMax <  min(figpos(4),0.9*figpos(3))*(1-TCM_UI.AxisHeightTarget)-5-SHeight)
    hratio = 1;
else
    hratio = (( min(figpos(4),0.9*figpos(3))*(1-TCM_UI.AxisHeightTarget)-5-SHeight)  -HMin)/(HMax-HMin);
end
YCoords = [1,0,0,0,figpos(4)-1];
YCoords(2) = YCoords(1) + 1 + hratio*TCM_UI.HintHeightMax     + (1-hratio)*TCM_UI.HintHeightMin;
YCoords(4) = YCoords(5) - 1 - hratio*TCM_UI.TitleHeightMax    - (1-hratio)*TCM_UI.TitleHeightMin;
YCoords(3) = YCoords(4) - 1 - SHeight;
TCM_UI.titletext.Position =     [XCoords(1)+1,  YCoords(4)+1,  XCoords(8)-XCoords(1)-1,  YCoords(5)-YCoords(4)-1];
TCM_UI.settingspanel.Position = [XCoords(2)+1,  YCoords(3)+1,  XCoords(3)-XCoords(2)-1,  YCoords(4)-YCoords(3)-1];
TCM_UI.buttonspanel.Position =  [XCoords(4)+1,  YCoords(3)+1,  XCoords(5)-XCoords(4)-1,  YCoords(4)-YCoords(3)-1];
TCM_UI.feedbackpanel.Position = [XCoords(6)+1,  YCoords(3)+1,  XCoords(7)-XCoords(6)-1,  YCoords(4)-YCoords(3)-1];
TCM_UI.axispanel.Position =     [XCoords(1)+1,  YCoords(2)+1,  XCoords(8)-XCoords(1)-1,  max(0,YCoords(3)-YCoords(2)-1)];
TmpXCoord = TCM_UI.InfoPortion*(XCoords(8)-XCoords(1)-1);
TCM_UI.infobutton.Position =    [XCoords(1)+1,  YCoords(1)+1,  TmpXCoord-XCoords(1)-1,   YCoords(2)-YCoords(1)-1];
TCM_UI.hinttext.Position =      [TmpXCoord+1,   YCoords(1)+1,  XCoords(8)-TmpXCoord-1,   YCoords(2)-YCoords(1)-1];
end

% function for opening the information webpage
function information(varargin)
pos = TCM_UI.fig.OuterPosition;
infodialog = dialog('Name','Information','Units','points',...
    'OuterPosition',[pos(1)+pos(3)/4 pos(2)+pos(4)/4 pos(3)/2 pos(4)/2]);
uicontrol('Units','normalized', 'Parent',infodialog, ...
        'FontUnits','normalized', 'FontSize',0.67,...
        'HorizontalAlignment','center', 'Position',[.01,.6,.98,.15],...
	'String','Mathematics Online Link', 'Style','text',...
    'ForegroundColor','blue','ButtonDownFcn',@linkfun,...
    'Enable','inactive');
uicontrol('Units','normalized', 'Parent',infodialog, ...
        'FontUnits','normalized', 'FontSize',0.4,...
        'HorizontalAlignment','center', 'Position',[.01,.4,.98,.15],...
	'String',{horzcat('Created by ',TCM_UI.Author,'.');horzcat('Version: ',TCM_UI.Date)} , 'Style','text');
end

function linkfun(varargin)
web(TCM_UI.Web)
end
end
