function plotSol(m, xSol, pAll, icAll, P0, opts)
% *** Plots ***
% 
% This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative 
% Works 3.0 United States License. To view a copy of this license, visit 
% http://creativecommons.org/licenses/by-nc-nd/3.0/us/ or send a letter to Creative Commons,
% 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

nSim = length(xSol);
nP   = length(opts.useParams);
nIC  = length(opts.useICs);

warning('off', 'MATLAB:divideByZero');
warning('off', 'MATLAB:plot:IgnoreImaginaryXYPart');

for i = 1:nSim
    subplot(nSim+1,1,i)
%     N     = max(m.c*xSol(i).y(1:end-1,:), [], 2)*ones(1, size(xSol(i).y,2));
    N     = 1;
    yPlot = m.c*xSol(i).y(1:m.nX,:)./N;
    plot(xSol(i).x, yPlot, 'LineWidth',2);
    xlabel('time'),ylabel('normalized concentration')
    %         title(sprintf('obj tolerance = %g', obj_tol))
end
warning('on', 'MATLAB:divideByZero');

subplot(nSim+1,1,nSim+1)
if nIC && nP
    semilogy(1:nP, pAll(opts.useParams)./(eps + P0(1:nP)), 'o', (nP+1):(nP+nIC), icAll(opts.useICs)./(eps+P0((nP+1):(nP+nIC))), 'o')
    yl = get(gca, 'YLim');
    set(gca, 'YLim', [min(yl(1), 10.^(-1e-9)) max(yl(2), 10.^(1e-9))]);
elseif nIC
    semilogy(1:nIC, icAll(opts.useICs)./(eps + P0(1:nIC)), 'o')
    yl = get(gca, 'YLim');
    set(gca, 'YLim', [min(yl(1), 10.^(-1e-9)) max(yl(2), 10.^(1e-9))]);
elseif nP
    semilogy(1:nP, pAll(opts.useParams)./(eps + P0(1:nP)), 'o')
    yl = get(gca, 'YLim');
    set(gca, 'YLim', [min(yl(1), 10.^(-1e-9)) max(yl(2), 10.^(1e-9))]);
end
xlabel('parameter number'), ylabel('parameter change')
drawnow

warning('on', 'MATLAB:plot:IgnoreImaginaryXYPart');
