% Jake Bobowski
% August 18, 2017
% Created using MATLAB R2014a

% This tutorial demonstrates how to create log-log scale plots and semilog
% (log-linear & linear-log) plots.

clearvars

% Log-log plots are created using the 'loglog' command.  It has all the
% same formating options as 'plot' which are discussed in the basic_plots.m
% script.

% First, we define a function to plot.  This function is the output of a
% simple low-pass RC filter.
syms f f0
lowpass = @(f, f0) 1./sqrt(1 + (f/f0).^2);

% Here's the first log-log plot.
f0 = 1e4;
ff = logspace(1, 5, 1000);
loglog(ff, lowpass(ff, f0), 'k-', 'LineWidth', 2.5)
xlabel('Frequency (Hz)')
ylabel('Low-pass Filter')
axis([10 1e5 0.1 1.2])

% We'll use 'hold on' to add the curve from a second low-pass filter with
% a different cut-off frequency.
hold on;
f0 = 1e3;
loglog(ff, lowpass(ff, f0), 'r--', 'LineWidth', 1.5)
legend({'$f_0 = 10$ kHz', '$f_0 = 1$ kHz'}, 'Interpreter', 'LaTeX', 'location', 'southwest')
hold off;

% Semilog plots are created using 'semilogx' and 'semilogy'.  Here's
% semilogx.
figure();
f0 = 1e4;
semilogx(ff, lowpass(ff, f0), 'k-', 'LineWidth', 2.5)
xlabel('Frequency (Hz)')
ylabel('Low-pass Filter')
axis([10 1e5 0.1 1.2])

% Add another curve to the log-linear plot.
hold on;
f0 = 1e3;
semilogx(ff, lowpass(ff, f0), 'r--', 'LineWidth', 1.5)
legend({'$f_0 = 10$ kHz', '$f_0 = 1$ kHz'}, 'Interpreter', 'LaTeX', 'location', 'southwest')
hold off;

% Here's semilogy.
figure();
f0 = 1e4;
semilogy(ff, lowpass(ff, f0), 'k-', 'LineWidth', 2.5)
xlabel('Frequency (Hz)')
ylabel('Low-pass Filter')
axis([10 1e5 0.1 1.2])

% Add another curve to the linear-log plot.
hold on;
f0 = 1e3;
semilogy(ff, lowpass(ff, f0), 'r--', 'LineWidth', 1.5)
legend({'$f_0 = 10$ kHz', '$f_0 = 1$ kHz'}, 'Interpreter', 'LaTeX', 'location', 'northeast')
hold off;

% The easiest way that I've found to do plots with error bars on log scales
% is to use 'errorbar' (as shown in basic_plots.m) and then modify whatever
% axis you want to be on a log scale afterwards.  Below, 'set(gca, ...)' is
% used to change the x-axis to be on a log scale.
figure();
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
y = [1.02, 4.3, 8.6, 16, 22, 34, 45, 60.2, 80.1, 96, 125, 144];
dy = [.3, .3, .5, 2, 2, 2, 4, 4, 6, 10, 20, 20];
formattedPlot = errorbar(x, y, dy, 'ko', 'MarkerSize', 6, 'LineWidth', 1.5, 'MarkerFaceColor', 'y');
xlabel('x-axis label', 'FontSize', 14, 'FontName', 'Times')
ylabel('y-axis label', 'FontSize', 14, 'FontName', 'Times')
set(gca, 'FontSize', 12, 'FontName', 'Times')
axis([0.8 15 0 180])
set(gca, 'XScale', 'log');

% Here's an error bar plot with the y-axis on a log scale.
figure();
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
y = [1.02, 4.3, 8.6, 16, 22, 34, 45, 60.2, 80.1, 96, 125, 144];
dy = [.3, .3, .5, 2, 2, 2, 4, 4, 6, 10, 20, 20];
formattedPlot = errorbar(x, y, dy, 'ko', 'MarkerSize', 6, 'LineWidth', 1.5, 'MarkerFaceColor', 'y');
xlabel('x-axis label', 'FontSize', 14, 'FontName', 'Times')
ylabel('y-axis label', 'FontSize', 14, 'FontName', 'Times')
set(gca, 'FontSize', 12, 'FontName', 'Times')
axis([0 13 0.4 300])
set(gca, 'YScale', 'log');

% Here's an error bar plot with both axes on a log scale.
figure();
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
y = [1.02, 4.3, 8.6, 16, 22, 34, 45, 60.2, 80.1, 96, 125, 144];
dy = [.3, .3, .5, 2, 2, 2, 4, 4, 6, 10, 20, 20];
formattedPlot = errorbar(x, y, dy, 'ko', 'MarkerSize', 6, 'LineWidth', 1.5, 'MarkerFaceColor', 'y');
xlabel('x-axis label', 'FontSize', 14, 'FontName', 'Times')
ylabel('y-axis label', 'FontSize', 14, 'FontName', 'Times')
set(gca, 'FontSize', 12, 'FontName', 'Times')
axis([0.8 15 0.4 300])
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');

% Just for fun, we can use 'hold on' to add a line to this last plot.  'hold
% on' will preserve the format of the axes from the previous plot.  Therefore,
% we can just use plot and the axes will still be on log scales.
hold on;
xx = logspace(-1, 2, 1000);
plot(xx, xx.^2, 'b-', 'LineWidth', 1.5)
hold off;