function LoopCMean(nexptos,N) 

% LoopCMean  Calculates the I from the Cmean(T) and saves it in LoopCmeanData
% 
% LoopCMean(nexp,N)
% nexp: number of experiment
% N: # of samples

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 7 y 8: Monte Carlo I y II
% Autores: Eva Besada-Portas, Sergey M. Plis


Data;
hmN=length(N);
fmean=zeros(nexptos,hmN);

for l=1:hmN
    N(l)               
    for k=1: nexptos
        disp(sprintf('Experiment:%d',k));
        t=s5*rand(N(l),1);
        f=CMean(t,[s1,s2,s3,s4,s5],[m1,m2,m3]);
        fmean(k,l)=mean(f);
    end
end

save LoopCMeanData fmean

function f=CMean(t,steps,m)

%CMean        Cmean(T)
%steps: s1,s2,s3,s4,s5
%m: m1, m2, m3

steps=[0,steps];
l=length(steps)-1;
for i=1:l
    k=find(t>steps(i)& t<steps(i+1));
    switch i
        case 1,
            f(k)=m(1);
        case 2,
            f(k)=m(1)+(m(2)-m(1))*(t(k)-steps(i))/(steps(i+1)-steps(i));
        case 3,
            f(k)=m(2);
        case 4,
            f(k)=m(2)+(m(3)-m(2))*(t(k)-steps(i))/(steps(i+1)-steps(i));
        case 5,
            f(k)=m(3);
    end
end

