% Teaching the use of the first few lines to create a help file for this
% document
%I encourage students to do this to help them inculcate the culture of
%making their scripts understandable by everyone else.


%% Comparison of Common Mathematical Functions

clc, clear, close all 

% Define domain
x = linspace(-2, 2, 400); 

% Define functions
y1 = x;                  % Linear
y2 = x.^2;               % Quadratic
y3 = x.^3;               % Cubic
y4 = exp(x);             % Exponential
y5 = log(x + 3);         % Logarithmic (shifted right to avoid log(0))
y6 = sin(x);             % Sine
y7 = cos(x);             % Cosine
y8 = 1./(1+exp(-x));     % Sigmoid
y9 = abs(x);             % Absolute value

% Plot all
figure; hold on; grid on;
plot(x, y1, 'r', 'LineWidth', 1.5);
plot(x, y2, 'b', 'LineWidth', 1.5);
plot(x, y3, 'g', 'LineWidth', 1.5);
plot(x, y4, 'm', 'LineWidth', 1.5);
plot(x, y5, 'c', 'LineWidth', 1.5);
plot(x, y6, 'k--', 'LineWidth', 1.5);
plot(x, y7, 'y--', 'LineWidth', 1.5);
plot(x, y8, 'Color', [0.5 0 0.5], 'LineWidth', 1.5);
plot(x, y9, 'Color', [0 0.5 0.5], 'LineWidth', 1.5);

% Labels and legend
xlabel('x');
ylabel('f(x)');
title('Comparison of Different Functions');
legend({'x','x^2','x^3','exp(x)','log(x+3)','sin(x)','cos(x)','sigmoid','|x|'}, ...
        'Location','northwest');



%% 2D Comparison of Signals


% Time domain
t = linspace(0, 2*pi, 400);

% Define engineering-related signals
y1 = sin(t);                      % Pure sinusoid (AC signal)
y2 = cos(t);                      % Orthogonal sinusoid
y3 = exp(-0.5*t).*sin(5*t);       % Damped oscillation (RLC response)
y4 = square(2*t);                 % Square wave (digital signal)
y5 = sawtooth(2*t);               % Sawtooth wave (PWM signal)
y6 = sin(2*t) + 0.5*sin(5*t);     % Composite signal (Fourier synthesis)

% Plot in 2D
figure; hold on; grid on;
plot(t, y1, 'r', 'LineWidth', 1.5);
plot(t, y2, 'b', 'LineWidth', 1.5);
plot(t, y3, 'g', 'LineWidth', 1.5);
plot(t, y4, 'm', 'LineWidth', 1.5);
plot(t, y5, 'c', 'LineWidth', 1.5);
plot(t, y6, 'k', 'LineWidth', 1.5);

xlabel('Time (s)');
ylabel('Amplitude');
title('Comparison of Engineering Signals (2D)');
legend({'Sine wave','Cosine wave','Damped oscillation','Square wave','Sawtooth','Composite'}, ...
       'Location','northeastoutside');
