function ShowSolution(x,z,E) % % Function ShowSolution provides a pretty display of the solution % set of an optimization program. z is the optimal objective value % and x is the vector of optimal values. This function shows only % the nonzero values in x (those that are within E of zero). % % vector x must be provided. % value z will only be displayed if it is finite. % value E defaults to eps^(2/3) % % Version: October 30, 2017 % %%%%% Input Checking and Default Setting if nargin<1,return,end if ~exist('E','var')||isempty(E),E=eps^(2/3);end if ~exist('z','var')||isempty(z),z=NaN;end %%%%% Preliminaries % set small values in x to be equal to zero x(abs(x)<=E)=0; % get indices of nonzero entries in x idx=find(x); % determine the scale of each entry in x (and z) Sx=ceil(log10(abs(x))); Sz=ceil(log10(abs(z))); fprintf('\n###################################################\n') fprintf('#\n'); if isfinite(z) % a solution exists %%%%% Display Optimal Objective Value prec=max(12,Sz); fprintf('# Optimal objective value:\n#\n'); fprintf(['# z = ',num2str(z,prec),'\n'],z); fprintf('#\n'); %%%%% Display Optimal Nonzero Decision Variable Values if isempty(idx) fprintf('# Optimal decision variable values are all zero.\n'); fprintf('#\n'); else M=max(1,ceil(log10(idx(end)+0.5))); fprintf('# The nonzero optimal decision variable values:\n#\n'); for k=1:length(idx) i=idx(k); m=max(1,ceil(log10(i+0.5))); prec=max(12,Sx(i)); if x(i)>0,fmt='x(%d) = +';else;fmt='x(%d) = -';end fprintf(['# ',repmat(' ',1,M-m),fmt,num2str(abs(x(i)),prec),'\n'],i); end fprintf('#\n'); end else % there is no solution fprintf('# The Problem seems to have no Solution.\n'); fpriuntf('#\n'); end fprintf('###################################################\n\n\n') return