% session 2
clear all; clf


% animation of a function with text
n=300;
c=jet(n);
for tre=1:50
tvec=linspace(0,2*pi,n);
for ind=1:n;
t=tvec(ind);
N=100;
x=linspace(-3,3,N);
f=cos(5*(x-t)).*exp(-x.^2);
plot(x,f,'.-','color',c(ind,:));

% put a text at the peak
sel=2:N-1;
peakloc=find((f(sel)>f(sel+1))&(f(sel)>f(sel-1)));

for gre=1:length(peakloc)

    val=f(peakloc(gre));
    mytext=sprintf('%0.2g',val); ;
fs=ceil(30*val);
text(x(peakloc(gre)),0.1+f(peakloc(gre)),mytext,'fontsize',fs);hold on    
end
hold off


axis([-3,3,-1,1.3]);
drawnow

end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
break
% drawing many circles

subplot(1,1,1);

n=1000;
r=0.005*randn(1,n).^2;

np=50;
th=linspace(0,2*pi,np);
x=cos(th);
y=sin(th);
x0=rand(1,n);
y0=rand(1,n);

for gre=1:1000;
x0=x0+0.01*randn(1,n);
y0=y0+0.01*randn(1,n);

    
    % 
% tic
% subplot(2,1,1);
% for ind=1:n
%     xx=(x*r(ind))+x0(ind);
%     yy=(y*r(ind))+y0(ind);
%     
%     
%     plot(xx,yy);
%    axis([0,1,0,1]);
%        %drawnow
%     hold on
% end
% t=toc

% using kronecker
X=kron(r',[x'; NaN])+kron(x0',ones(np+1,1));
Y=kron(r',[y'; NaN])+kron(y0',ones(np+1,1));
plot(X,Y,'r');
axis([0,1,0,1]);
drawnow
end








%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
break
% plotting a surface with nested loops
disp('nested loops')
tic
n=20000;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
f=zeros(n,n);
for i=1:n
    for j=1:n
        f(i,j)=cos(5*x(i))*exp(-x(i)^2-y(j)^2);
    end
end
%mesh(x,y,f);
%axis equal tight
t=toc

% using meshgrid
disp('meshgrid')
tic
[X,Y]=meshgrid(x,y);
f=cos(5*X).*exp(-X.^2-Y.^2);
%mesh(X,Y,f);   
%axis equal tight
toc/t


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ù
break

% differentiation of a function using subarray shifting
nvec=10:10:1000;
e=NaN*nvec;

for gre=1:length(nvec);
n=nvec(gre);
x=linspace(0,pi,n);
dx=x(2)-x(1);
f=cos(x);
%plot(x,f,'k.-');

df=-sin(x); % exact derivative

    % derivative using shifting
sel=2:n-1;
dfn=[(f(2)-f(1))/dx, ...
    (f(sel+1)-f(sel-1))/(2*dx), ...
    (f(n)-f(n-1))/dx];

%plot(x,df,'r',x,dfn,'b.-');

e(gre)=sum((df-dfn).^2);
loglog(nvec,e); hold on
drawnow
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ùù
%%%% the script from the year 2012
break

% drawing circles
n=30;
th=linspace(0,2*pi,n);
x=cos(th); y=sin(th);

nc=10000;
xx=randn(nc,1);
yy=randn(nc,1);
r=(randn(nc,1).^2/30);

disp('with krontrick')
% second way with array operations

tic
X=kron(r,[x'; NaN])+kron(xx,[ones(n,1);NaN]);
Y=kron(r,[y';NaN])+kron(yy,[ones(n,1);NaN]);

plot(X,Y,'k-');
axis equal tight
t1=toc

disp('with loop');
tic
for ind=1:nc;
    plot(r(ind)*x+xx(ind),r(ind)*y+yy(ind),'k-');
    hold on
end
axis equal tight
t2=toc



t2/t1



break

% auxiliary for 2D plots
n=30;
x=linspace(-2*pi,2*pi,n);
y=linspace(-2*pi,2*pi,n);

[X,Y]=meshgrid(x,y);

for t=linspace(0,4*pi,200);
    

f=sin(X*2-t).*exp(-X.^2/2-Y.^2/2);
mesh(X,Y,f);
ll=0.8;
zlim([-1,1]*ll);
caxis([-1,1]*ll);

drawnow;
end

break




% BINARISATION
a=imread('pingpong.png');
b=sum(a,3);


for lim=1:5:500
sel=b>lim;
c=b;
c(sel)=1;
c(~sel)=0;

imagesc(c);
colormap gray
drawnow
end



break


% logical operations

for n=2:1000:10000;

np=sum(rand(n,1)>0.5)/n;

plot(n,np,'r.',n,0.5,'k.');
hold on
end
ylim([0,1]);


break



% shifting subarrays
n=1000;
x=linspace(0,2*pi,n);
f=sin(x);
dx=x(2)-x(1);
I=sum(f(1:n-1)+f(2:n))*dx/2


break

% memory management
n=1000
disp('one by one')
tic
f=[];
for ind=1:n;
    f=[f,ind];
end
t1=toc



disp('all together');
tic;
for ind=1:n
f(ind)=ind;
end
t2=toc

t1/t2




break

% plotting example
n=1000;
x=randn(n,1);
y=randn(n,1);

disp('one by one');
tic;
for ind=1:n;
    plot(x(ind),y(ind),'k.');
    hold on;
end
t1=toc

clf;
disp('all together');
tic
plot(x,y,'k.');
t2=toc;

t1/t2





break
% exemple of vectorization
n=1000;
A=randn(n,n);
B=randn(n,n);

disp('function operation');
tic
C=A*B;
t1=toc

disp('with loops')
tic
C=zeros(n,n);
for i=1:n;
    for j=1:n;
        for k=1:n
            C(i,j)=A(i,k)*B(k,j);
        end
    end
end
t2=toc

t2/t1










