%Solutions to the exercises in session 1


%Define the following variables: a = 3, b = -1, d = -c:
a = 3;
b = -1;
c = 7;
d = -c;

%Note how they're all followed by a semi-colon (;). This way, when the
%script is ran there is no output from those lines (because we don't need
%to see it). Also note that the last statement sets the value for d = -7.
%If we change the value of "c", "d" will NOT be updated automatically.

%Evaluate the following:
a*b - c
(a+b)/c
a + b/c + d
(a+b)/c + d
(a+b)/(c+d)
a - b*c + 2^a + 2

%All of these will generate output. Notice the output of the penultimate is
%Inf.

%Add parenthesis to make the expressions clearer.
%To do this, we will run the two of them (with and without parenthesis) to
%see that the results are the same:

TempA = a^b/2^c;
TempB = (a^b)/2^c;
fprintf('Without parenthesis: %f, with: %f \n', TempA, TempB);

%Don't mind the fprintf command. It just looks prettier than the usual
%no-semicolon display

TempA = a*b^c*3-d;
TempB = a*(b^c)*3-d;

fprintf('Without parenthesis: %f, with: %f \n', TempA, TempB);
%% Exercises with vectors. 

%Define the vectors A = [-2,-3,-5], B = [2;3;5]
A = [-2,-3,-5]; B = [2;3;5];

%Note you can put more than one command in a line, separated by ; or ,

%Find the sum of the elements of A
SumofA = A(1) + A(2) + A(3);
%or use sum(A). You can check they're the same with:
fprintf('SumofA - sum(A) = %f \n', SumofA - sum(A));

%Find the inner product of A and B
InnerProductAB = A(1)*B(1) + A(2)*B(2) + A(3)*B(3); % = A*B
%Right, you can put comments at the end of command lines too!

%Define the vector C = [1, 2, ..., 1030]
C = [1:1030];

%Read out the first three and last thirty elements of C. Name the result X:
X = C([1:3, 1001:30]); % or X = [C(1:3), C(1001:end)];

%Read out all even elements of C
EvenofC = C(2:2:1030);

%Odds of C
OddsofC = C(1:2:1030); %C(1030) will not be included, so = C(1:2:1029)

%Create a vector Y with the elements of C in reverse order
Y = C(1030:-1:1);

%Replace the fifth, sixth, ..., twelfth eleemnts of Y with the vector
%[10,15,...,45]
Y(5:12) = 10:5:45;



