clear all; clf


%%%% animation of a moving surface

n=25;l=3;
x=linspace(-l,l,n);
y=x;
[X,Y]=meshgrid(x,y);
for gre=1:500
for t=linspace(0,2*pi,700);
f=sin(2*(X-t)).*cos(2*(Y-t)).*exp(-X.^2-Y.^2);
mesh(X,Y,f);
colormap jet
axis([-l l -l l -1 1]);
caxis([-1,1]*0.5);
view(3)
drawnow
end
end
break

% integral using auxiliary array
n=20;
x=linspace(0,pi,n);
dx=x(2)-x(1);
f=sin(x);
plot(x,f,'k.-')

I=ones(1,n)*dx;
I([1,n])=I([1,n])/2;

int=I*f';



break
%%%%%%%
% binarisation of an image
subplot(1,2,1)

a=imread('rayleigh.jpg');
image(a);
axis equal tight

for gre=1:20
for ind=1:235

sel=a>ind;

o=zeros(size(a));
o(sel)=1;
o(~sel)=0;

subplot(1,2,2)
imagesc(sel);
axis equal tight


drawnow
end
end



break
% subarray shifting for integration

n=20;
x=linspace(0,pi,n);
dx=x(2)-x(1);
f=sin(x);
plot(x,f,'k.-')

% do the integral
I=0;
for ind=1:n-1
    I=I+dx*(f(ind)+f(ind+1))/2;
end

% with shifting of the subarrays
sel=1:n-1;
I=sum(f(1:n-1)+f(2:n))/2*dx;



break
% computing factorial
n=10;
f=1;
for ind=1:n
    f=f*ind;
end

f

factorial(n)

v=1:n % auxiliary array
prod(v) % reducing dimensionality




break

% vectorization
% plotting a function

for t=linspace(0,4*pi,500);
n=100;
x=linspace(-5,5,n);

% more efficient
f=cos(5*(x-t)).*exp(-x.^2);

plot(x,f,'k-');
drawnow
end






break

% matrix multiplication

n=1000;
a=randn(n,n);
b=randn(n,n);

% using compiled function
tic
c=a*b;
t=toc;

% using nested loops
tic
C=zeros(n,n);
for i=1:n;
    for j=1:n;
        for k=1:n;
        C(i,j)=C(i,j)+a(i,k)*b(k,j);
        end
    end
end
toc/t





break

% memory allocation

% elementwise
nvec=10:100:10000
rapvec=zeros(1,length(nvec));

for gre=1:length(nvec);
    n=nvec(gre);
tic
f=[];
for ind=1:n
    f=[f,ind];
end
t=toc

% vectorized
tic
f=zeros(1,n);
for ind=1:n
    f(ind)=ind;
end


rapvec(gre)=t/toc;

end
plot(nvec,rapvec,'k*-');



%%%%%
break
% subarray selection using index vectors
load penlong.mat

for ind=1:421;
    imagesc(squeeze(m(ind,:,:))); 
    colormap gray; 
    axis tight; 
    drawnow; 
end





