%-----------------------------%
% Exercises unit 1 --- part 2 %
%-----------------------------%
%%
% Ex. 1.0.1
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

%%
%-----------------------------%
% Ex. 2.0.2
clear all
clc
close all
a=0; b=10; N=200;
x=linspace(a,b,N);
f=x.*exp(-x);

hold on
plot(x,f,'-b')
plot(x,f,'sr')
xlabel('x','fontsize',16)
ylabel('y','fontsize',16)
title('plot of the function f(x)=x*exp(-x)')
hold off

%%
%-----------------------------%
% Ex 2.0.3
clear all
clc
close all
w=1;
a=0; b=10*pi; N=100;
t=linspace(a,b,N);

% figure 1
figure(1)

% subplot 1
A=1; v=0.1;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,1)
hold on
title('A=1; v=0.1')
plot(t,x,'-r')
plot(t,y,'-g')
plot(t,v*t,'--k')
xlabel('t')
ylabel('x,y')
legend('x','y')
hold off

% subplot 2
A=2; v=0.1;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,2)
hold on
title('A=2; v=0.1')
plot(t,x,'-r')
plot(t,y,'-g')
plot(t,v*t,'--k')
xlabel('t')
ylabel('x,y')
legend('x','y')
hold off

% subplot 3
A=1; v=0.5;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,3)
hold on
title('A=1; v=0.5')
plot(t,x,'-r')
plot(t,y,'-g')
plot(t,v*t,'--k')
xlabel('t')
ylabel('x,y')
legend('x','y')
hold off

% subplot 4
A=1; v=2;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,4)
hold on
title('A=1; v=2')
plot(t,x,'-r')
plot(t,y,'-g')
plot(t,v*t,'--k')
xlabel('t')
ylabel('x,y')
legend('x','y')
hold off
%%
% figure 2
figure(2)

% subplot 1
A=1; v=0.1;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,1)
hold on
title('A=1; v=0.1')
plot(x,y,'-b')
xlabel('x')
ylabel('y')
hold off

% subplot 2
A=2; v=0.1;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,2)
hold on
title('A=2; v=0.1')
plot(x,y,'-b')
xlabel('x')
ylabel('y')
hold off

% subplot 3
A=1; v=0.5;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,3)
hold on
title('A=1; v=0.5')
plot(x,y,'-b')
xlabel('x')
ylabel('y')
hold off

% subplot 4
A=1; v=2;
x=v*t+A*cos(w*t);
y=A*sin(w*t);

subplot(2,2,4)
hold on
title('A=1; v=2')
plot(x,y,'-b')
xlabel('x')
ylabel('y')
hold off
%%
%-----------------------------%
% Repetition of Ex 2.0.3 using the loop 'for'
% (for later!! the loop 'for' will be introduced next week)
clear all
clc
close all
w=1;
a=0; b=10*pi; N=100;
t=linspace(a,b,N);

for i_f=1:2
    % figure
    figure(i_f) 
    for i_s=1:4
        % subplot 
        if i_s==1
            A=1; v=0.1;
        elseif i_s==2
            A=2; v=0.1;
        elseif i_s==3
            A=1; v=0.5;
        elseif i_s==4
            A=1; v=2;
        end
        x=v*t+A*cos(w*t);
        y=A*sin(w*t);
            
        subplot(2,2,i_s)
        hold on
        if i_s==1
            title('A=1; v=0.1')
        elseif i_s==2
            title('A=2; v=0.1')
        elseif i_s==3
            title('A=1; v=0.5')
        elseif i_s==4
            title('A=1; v=2')
        end
        if i_f==1
            plot(t,x,'-r')
            plot(t,y,'-g')
            plot(t,v*t,'--k')
        elseif i_f==2
            plot(x,y,'-b')
        end
        xlabel('t')
        ylabel('x,y')
        legend('x','y')
        hold off
    end
end
%%
%-----------------------------%
% Ex 2.0.4
clear all
close all
clc
format long
% geometrical series: distance
% first rough estimate 
20+20/2+20/(2^2)+20/(2^3)+20/(2^4)
% in a more systematic way
n=(0:1:60);
% estimate of the distance (in meters) at which A and T meet
dist_est=sum(20./(2.^n))

% estimate of the time (in seconds) after which A and T meet
time_est=sum(4./(2.^n))

% A's velocity [m/s] 
v_A=20/4

% T's velocity [m/s] 
v_T=10/4

% plot
a=0; b=9; N=200;
t=linspace(a,b,N);
fA=0+v_A*t;
fT=20+v_T*t;

% actual figure
hold on
plot(t,fA,'-b',...
    'LineWidth',3)
plot(t,fT,'-r',...
    'LineWidth',3)
% cheap way of plotting the various segments for
% times and positions of A and the T:
plot([0 4 4 6 6 7 7],[20 20 30 30 35 35 37.5],'g','LineWidth',2)
plot(time_est,dist_est,'sk','MarkerSize',14,'LineWidth',2)
xlabel('t [s]', 'fontsize', 16)
ylabel('x [m]', 'fontsize', 16)
title('Zeno s paradox of Achilles and the Tortoise', 'fontsize', 16)
hold off

% find a more compact way to do the same thing, 
% i.e. by using the loop for (next week!)


%%
%-----------------------------%
% Same as before, but costy way to plot the
% various horizontal and vertical segments for
% the Achilles and the tortoise positions and times
% Ex 2.0.4
clear all
close all
clc
format long
% geometrical series: distance
% first rough estimate 
20+20/2+20/(2^2)+20/(2^3)+20/(2^4)
% in a more systematic way
n=(0:1:60);
% estimate of the distance (in meters) at which A and T meet
dist_est=sum(20./(2.^n))

% estimate of the time (in seconds) after which A and T meet
time_est=sum(4./(2.^n))

% A's velocity [m/s] 
v_A=20/4

% T's velocity [m/s] 
v_T=10/4

% plot
a=0; b=9; N=200;
t=linspace(a,b,N);
fA=0+v_A*t;
fT=20+v_T*t;

% 
t1=linspace(0,4,10);
t2=linspace(4,4+4/2,10);
t3=linspace(4+4/2,4+4/2+4/(2^2),10);
t4=linspace(4+4/2+4/(2^2),4+4/2+4/(2^2)+4/(2^3),10);

x2=linspace(20,20+20/2,10);
x3=linspace(20+20/2,20+20/2+20/(2^2),10);
x4=linspace(20+20/2+20/(2^2),20+20/2+20/(2^2)+20/(2^3),10);
one=ones(1,10);

% actual figure
hold on
plot(t,fA,'-b',...
    'LineWidth',3)
plot(t,fT,'-r',...
    'LineWidth',3)

plot(t1,20*one,'g','LineWidth',2)
plot(t2,(20+20/2)*one,'g','LineWidth',2)
plot(t3,(20+20/2+20/(2^2))*one,'g','LineWidth',2)

plot(4*one,x2,'-g','LineWidth',2)
plot((4+4/2)*one,x3,'-g','LineWidth',2)
plot((4+4/2+4/(2^2))*one,x4,'-g','LineWidth',2)

plot(time_est,dist_est,'sk','MarkerSize',14,'LineWidth',2)

xlabel('t [s]', 'fontsize', 16)
ylabel('x [m]', 'fontsize', 16)
title('Zeno s paradox of Achilles and the Tortoise', 'fontsize', 16)
hold off

% find a more compact way to do the same thing, 
% i.e. by using the loop for (next week!)

%%
% Intermediate excercise about geometric series
% and how their numerical evaluation tends asymptotically
% to the exact value
% N.B. we can largely improve this excercise later
% when we will introduce loops!
clear all
close all
clc
% Asymptotic limit of geometric series
% sum_{n=0}^{infinity} 1/2^n = 2
a_exact=2;

% finite sum up to N=5
N=5; n=(0:1:N);
a5=sum(1./2.^n)
% finite sum up to N=10
N=10; n=(0:1:N);
a10=sum(1./2.^n)
% finite sum up to N=15
N=15; n=(0:1:N);
a15=sum(1./2.^n)
% finite sum up to N=20
N=20; n=(0:1:N);
a20=sum(1./2.^n)

Nvect=(5:5:20);
aN=[a5 a10 a15 a20];

hold on
plot(Nvect,aN,'-r','LineWidth',3)
plot(Nvect,a_exact*ones(4),'--k')
legend('numerical','exact')
plot(Nvect,aN,'xb','MarkerSize',14,'LineWidth',2)
axis([4 21 1.96 2.02])
xlabel('N','fontsize',16)
ylabel('aN','fontsize',16)
hold off

%%
%-----------------------------%
% Ex 2.0.5
clear all
clc
close all
x=logspace(log10(1), log10(1000), 100)

figure(1)
plot(log10(x), log10(x.^3), 'o')

figure(2)
loglog(x,x.^3,'+')
%%
%-----------------------------%
% Ex 3.0.6
clear all
close all
clc
%fix number of points on the grid
N=100;

% generate x,y mesh
xmesh=linspace(-1,1,N);
ymesh=linspace(-1,1,N);
[x,y]=meshgrid(xmesh,ymesh);

r=sqrt(x.*x+y.*y);
f=r.^2-r.^4/2;

%2D plot of f
%contour(x,y,f)
%surf(x,y,f)
%surfl(x,y,f)
mesh(x,y,f)
colorbar

%Find the maximum value of f graphically

%compare with
max(max(f))

%%
%-----------------------------%
% Ex 4.0.7
clear all
close all
clc

N=10;
xm=linspace(0,2,N);
ym=linspace(-1,1,N);
[x,y]=meshgrid(xm,ym);

r=sqrt((x-1).^2+y.^2);

Ex=(x-1)./(r.^3);
Ey=y./(r.^3);

quiver(x,y,Ex,Ey)