Here is an improved version starting from the example suggested by Engineer (thanks!).
I did use the sinc function of Octave, which is defined in zero (not getting warning messages and not introducing that small error due to wrong calculation).
Moreover, I did show a step by step plotting to see how further samples change the signal and how the errors at the end of the range changes.
%% Sampling and reconstruction demo
clear,clc,close all;
%% Parameters
F = 30; % frequency of signal [Hz]
Fs = 2*F; % sampling rate [Hz]
Ts = 1/Fs; % sampling period [sec]
%% Generate "continuous time" signal and discrete time signal
tc = 0:1e-4:5/F; % CT axis
xc = cos(2piFtc); % CT signal
td = 0:Ts:5/F; % DT axis
xd = cos(2piFtd); % DT signal
N = length(td); % number of samples
%% Reconstruction by using the formula:
% xr(t) = sum over n=0,...,N-1: x(nT)sin(pi(t-nT)/T)/(pi(t-nT)/T)
% Note that sin(pi(t-nT)/T)/(pi(t-nT)/T) = sinc((t-nT)/T)
% sinc(x) = sin(pix)/(pix) according to MATLAB
xr = zeros(size(tc)); %initialization
sinc_train = zeros(N,length(tc)); %initialization
for n = 0:N-1
%unless we define our sinc with a value in zero it will introduce NaN which
%lead to a small error
%sinc_train(n+1,:) = sin(pi(tc-nTs)/Ts)./(pi(tc-nTs)/Ts); %sinc train
sinc_train(n+1,:) = sinc((tc-nTs)/Ts); %sinc train
current_sinc=xd(n+1)*sinc_train(n+1,:); %a sinc scaled by the sample value
xr = xr + current_sinc; %generation of the reconstructed signal summing the sinc scaled
end
%% Plot the results
figure
hold on
grid on
plot(tc,xc,'b','linewidth',2)
stem(td,xd,'k','linewidth',2)
plot(tc,xr,'r','linewidth',2)
legend('Continuos Signal','Sampled Signal','Reconstructed Signal')
xlabel('Time [sec]')
ylabel('Amplitude')
%% Sinc train visualization
figure
%all at once display
%hold on
%grid on
%plot(tc,xd'.*sinc_train)
%plot(tc,xr,'r','linewidth',2)
%stem(td,xd)
%progress display
xr_progress=zeros(size(tc)); %initialization
for n = 0:N-1
clf;hold on;grid on;
current_sinc=xd(n+1)sinc_train(n+1,:);
stem(td(1:n+1),xd(1:n+1),'k','linewidth',2)
plot(tc,xd(1:n+1)'.sinc_train(1:n+1,:))
xr_progress=xr_progress+current_sinc;
plot(tc,xr_progress,'r','linewidth',2)
xlabel('Time [sec]')
ylabel('Amplitude')
title(['Step ',num2str(n+1),' (Having ',num2str(n+1),' Sincs)'])
sleep(5)
end