%-----------------------------%
% Exercises unit 1 --- part 1 %
%-----------------------------%
%%
% Ex. 1.2.2
clear all
clc
rad2deg=180/pi;
asin(sqrt(3)/2)*rad2deg
%%
%-----------------------------%
% Ex 2.0.3
clear all
clc
M=[1.1, 12.4, 3.6; 7.5, 2.4, 7.8; 10.3, 33.5, 18.4];
index=find(M>5.0)
N=M(index)
%%
%-----------------------------%
% Ex 2.0.4
clear all
clc
M=[4, 12, 23; 2, 1, 3; 4, 2, 1]
M(2,3)
M(8)
a=M(:)
a(3)
b=M(2,:)
b(3)
c=M(1,1:2)
%%
%-----------------------------%
% Ex 2.2.1
clear all
clc
a=0; b=10; step=0.5;
x1=(a:step:b)
% equivalent to
x2=linspace(a,b,length(x1))
%%
%-----------------------------%
% Ex 2.2.2
clear all
clc
format long
n=(0:1:20);
e_est=sum(1./factorial(n))
% compare with
exp(1)

m=1.0e05;
e_est2=(1+1/m)^m

%%
%-----------------------------%
% Ex 2.3.1
clear all
clc
A=[1,2,3; 5,4,3]
%element (1,3)
A(1,3)
%compare with
A(5)
%vector a2 equal to the second row of the matrix A;
a2=[A(2,(1:1:3))]

%equivalently
a2=A(2,:)

sort(a2)

b2=a2( find(a2>3.5) )
%%
%-----------------------------%
% Ex 2.3.2
clear all
clc
a=(2:2:8)
b=sort((1:2:7),'descend')

%vector dot product; following procedures give the same result
sum(a.*b)
dot(a,b)
%the modulus of each vector can be found equivalently as
norm(a)
sqrt(sum(a.*a))
norm(b)
sqrt(sum(b.*b))

%the cos of the angle is given by
costheta=dot(a,b)/( norm(a)*norm(b) );
%answer in radiants
theta=acos( costheta )
%anser in degrees
theta*180/pi
%%
%-----------------------------%
% Ex 2.3.3
clear all
clc
c=sort([0:3:99],'descend');
c
% equivalently you could have used
c_2=(99:-3:0)

% extract every 10th element of that vector
i=[1:10:length(c)];
e=c(i);
e

% generate an index vector that accesses the first and
% last element of c
i=[1:length(c)-1:length(c)];
f=c(i);
f

% or equivalently
j=[1 length(c)];
g=c(j)
g
