%% Exploring Sequences
%% Introduction
% In this activity we will explore sequences \{a_n\} geometrically and symbolically.
% First, we will use the command stem to plot sequences and try to determine whether they converge or diverge. Then we will calculate the limit of particular sequences using the commands syms and limit.
%
%% Before starting
% Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.
% Read each section carefully.
% Run the code within a section before starting to read the next.
% To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:
%
% Remark: Run the code of each section from top to bottom, otherwise you may get an error.
% - The end of a section is indicated with a thin line, like the next one -
%
%% 1. Geometric exploration
%% 1.1 Plotting sequences
% Consider the following sequences
% a) a_n =\sin \left({\left(-2\right)}^n \right)   and    b) b_n =\frac{2n^3 }{n^3 -4} .
% To plot them, first, we need to create a variable:

max = 20

% This variable is an integer defined between 1 and 150. Now, we need to use the command linspace to define the domain of the sequences. This information can be stored in a variable:

n = linspace(1, max, max);

% Then our sequences can be defined as follows:

an = sin((-2).^n);
bn = (2 * n.^3)./(n.^3 - 4);

% Remark: Here we are using row vectors (also known as arrays) which means that the operations multiplication, division and exponentiation must have the period "." before each operator: *, /, ^. Learn more about it here: Arithmetic operations.
% Finally, we plot each sequence separately:

stem(n, an, 'filled')
title('Plot of an')
stem(n, bn, 'filled')
title('Plot of bn')

% Run this section to see the plots. Then drag the slider of the variable max to show more terms of the sequences on the plots. What do you noctice? What do you wonder?
%
%% 1.2 Compare sequences
% You can compare the sequences by plotting them together as follows:

m = linspace(1, 100, 100)';
cm = [sin((-2).^m), (2 * m.^3)./(m.^3 - 4)];
stem(m, cm, 'filled')
legend('sin((-2)^m)', '(2  m^3)/(m^3 - 4)')
title('Comparing sequences')

% Remark: Observe that we added the apostrophe symbol (') at the end of the linspace() command, just before the semicolon (;). This is required to plot two data series using a two-column matrix.
% Modify the first line, in the previous code, to plot more values of the sequences. What do you notice? Based on the graphs of the sequences, which one converges? Which one diverges?
%
%% 2. Symbolic exploration
%% 2.1 Calculating limits symbolically
% Calculating limits of sequences in MATLAB is quite easy. We just need the command syms and limit. For example, let's calculate the limit of
% {c_n =\left(\frac{2n-3}{3n-7}\right)}^4.
% Here we need to create a symbolic variable for "n", that is:

syms n

% Then we just calculate the limit as follows:

limit(((2*n-3)/(3*n-7))^4, n, inf)

%
%% 2.2 Comparing results
% We will calculate the limits of a_n =\sin \left({\left(-2\right)}^n \right) and b_n =\frac{2n^3 }{n^3 -4}.
% But before running the code in this section, remember the behaviour of these sequences based on the plots we have made previously. Pause and think about what the limit of each of them would be. Once you have done this, run the following lines:

ans1 = limit(sin((-2)^n), n, inf)          % limit of sequence a_n
ans2 = limit((2 * n^3)/(n^3 - 4), n, inf)  % limit of sequence b_n

% What do you notice? What happens to the results ans1 and ans2? Can you confirm the results using the theorems covered in class?
%
%% 3. Hands on practice
% Let's practice what we just learned.
%% Activity 1:
% Use the commands linspace and stem to plot 150 terms of the following sequences.
% \frac{2^k +1}{e^k }
% 2+\cos \left(k\;\pi \right)
% {\left(-2\right)}^{k+1}
% \frac{\sin \left(k+\pi \right)}{k^3 }
% \sin \left(\frac{\pi }{2}\cos \left(\pi \left(\frac{n}{150}+1\right)\right)\right)+1
% Analyse the plots of each sequence. What do you notice? Can you determine whether the sequence converges or diverges based on the information provided by its plot? Is it enough to use 150 terms in the plot?
% Write your code here:





% Hint 1: In this case you have to define a variable k. Also check the previous examples to correctly type the syntax of the arithmetic operations.
% Hint 2: In MATLAB the constant \pi is defined as pi. The constant e\approx 2\ldotp 71828 can be defined using the function exp(), that is, exp(1).
%
%% Activity 2:
% Find the limits of each sequence in your notebook and then using the commands syms and limit. Do the results agree with the plots from Activity 1?





% Hint 3: To add comments to MATLAB code, use the percent (%) symbol. Comment lines can appear anywhere in a program file, and you can append comments to the end of a line of code.