%% Simulation 1 Companion
%% MATLAB Onramp
% In order to complete Simulation 1 you should complete the following modules from the MATLAB Onramp.
% Commands
% MATLAB Desktop and Editor
% Vectors and Matrices
% Array Indexing and Modification
% Array Calculations
% Function Calls
% Documentation
% Plots
% It will be assumed that you are familiar with the content of these modules.
%
%% Plotting Example
% Let us use MATLAB to plot sine and cosine.
% First we create an x-axis from 0 to 2\pi with step size \pi/24. We do this by creating a vector called x containing these values. You should have seen this in the Vectors and Matrices modules.
% Uncomment by deleting the percent sign at the start of the line and then select Run Section to see the output.
% Note: while the percent sign is present the code in that line will be ignored, indicating it is a comment. Removing the percent sign is called uncommenting. There is a shortcut for uncommenting highlighted text. On Mac it is Command+T and on Windows it is Ctrl+T.

% x = 0:pi/24:2*pi

% Next we obtain the values of \sin(x) and \cos(x) for each x in the row vector we just created. We do this by creating two vectors.
% Again uncomment by deleting the percent sign and then select Run Section to see the output.
% Note: I will now longer give you this instruction but you should continue uncommenting and selecting Run Section as you proceed.

% y1 = sin(x) % contains sin(x) for each value in the row vector x
% y2 = cos(x) % contains cos(x) for each value in the row vector x

% Next we plot y = \sin(x) and note a viewing window will automatically be selected.

% plot(x,y1)

% I would prefer having a grid.

% grid

% I want to adjust the viewing window so that x is between 0 and 2\pi and y is between -1.25 and 1.25.
% Note: if you want more details on a function, like the axis function below, search for it in the Matlab documentation.

% axis([0 2*pi -1.25 1.25])

% Next we label the x-axis x and the y-axis y.

% xlabel('x')
% ylabel('y')

% If we were to now enter plot(x,y2) then the plot would be placed on another grid. You can see this by running after only uncommenting plot(x,y2) below. To ensure our plot is placed on the same grid, we first enter hold on to hold to the same grid.

% hold on
% plot(x,y2)

% Now we add a legend. We enter the labels in the order that the graphs were plotted.

% legend('y1','y2')

% Since we are done with this grid we type hold off to conclude the hold on that we had activated earlier.

% hold off

% To summarize: what we would have entered into our .m file is as follows.
% Note: the semicolon at the end of a line hides the output of the command. I do not care to see the contents of the vectors x, y1, y2 printed on the screen. I only care to see their graphs.

% x = 0:pi/24:2*pi;

% y1 = sin(x);
% y2 = cos(x);

% plot(x,y1)

% grid
% axis([0 2*pi -1.25 1.25])
% xlabel('x')
% ylabel('y')
% 
% hold on
% plot(x,y2)
% 
% legend('y1','y2')
% hold off

%
%% Modified Plotting Example
% Suppose we instead want to plot graphs of y = x^2e^{-x} and y = 1/(x+1) in two separate axes but such that the output will be placed in particular positions in the same figure.
% We begin by redefining x and y1 and y2.
% Note: we use .* instead of * for multiplication, ./ instead of / for division, and .^2 instead of ^2 for squaring. The . indicates that the operation is pointwise. This ensures that for example when we write x.^2 we are in fact squaring each element in the array x. If you type x^2 you are instructing Matlab to execute a matrix multiplication which is not the same as pointwise multiplication. In fact, in this icase it will lead to an error.

% x = linspace(0,10,101) % you saw the meaning of this in the Matlab Onramp
% 
% y1 = x.^2 .* exp(-x)
% y2 = 1./(x+1)

% We next create a figure window that will contain our two plots.

% figure

% We divide our figure into 2 rows and 1 column, then create axes in position 1: the 1st column of the 1st row of our figure.
% Note: remember to look at Matlab documentation whenever you do not understand the content of a function, like the subplot function below.

% subplot(2,1,1)

% We plot y1 and make it red and dashed using '--r'. We create a grid and label the axes.

% plot(x,y1,'--r')
% grid
% xlabel('x')
% ylabel('x^2e^{-x}')

% Next we create axes in position 2: the 1st column of the second row.

% subplot(2,1,2)

% We plot y1 and make it magenta using 'm' and extra thick, with a thickness of 2 points using 'LineWidth',2. We create a grid and label the axes.

% plot(x,y2,'m','LineWidth',2)
% grid
% xlabel('x')
% ylabel('1/(x+1)')

% We have now plotted two graphs in specified positions in the same figure. Now we clear the figure using clf so we can begin a new figure in the next example.

% clf

%
%% Matrices Example
% Let us construct and modify arrays, aka matrices. It goes without saying that you should have completed the Matlab Onramp modules listed at the top in order to understand the basics in order to proceed.
% Let us generate the matrix A = \left(\begin{array}{cccc}1&3&6&4\\3&8&0&1\\2&4&7&9\end{array}\right).

% A = [1 3 6 4;3 8 0 1; 2 4 7 9]

% Let us confirm that its size is 3 \times 4 i.e. 3 rows and 4 columns.

% size(A)

% Let us convert entry A_{13} (row 1 column 3) to 0.

% A(1,3) = 0

% And let us take the square root of every entry in the last column:

% A(:,end) = sqrt(A(:,end))

% And let us take the third row and multiply it pointwise by the last row.

% A(3,:) = A(3,:).*A(end,:)

% Now let us delete the third column to obtain a square 3 \times 3 matrix.

% A(:,3) = []

% Next let us swap the entries A_{32} and A_{33}.
% Note: only uncomment the start of the line. The second percentage sign is genuinely a coment indicating what the step is doing and not code to be run.

% temp = A(3,2); % temporarily save A(3,2) as the variable temp and conceal the output
% A(3,2) = A(3,3); % replace A(3,2) with A(3,3) and conceal the output
% A(3,3) = temp % replace A(3,3) with the old value of A(3,2)

% Lastly let us plot the first row against the last row as a scatter plot. A scatter plot will not include connecting lines between data points.

% scatter(A(end,:),A(1,:))
% axis([0 20 0 5])