global N = 20;
global M = 10;
global R = 20;
global J = 2;
global P1N = 1:N;
global PDN = 0:2*N;
global NSG = 0:2*N;

function y = P1_ferme(x)
  global M;
  if ((x < 1) || (x > M))
    y = 0;
  else
    y = 1/M;
  end;
end;

function y = P1_limite(x)
  global J;
  global M;
  if (x < 1)
    y = 0;
  elseif (x < M)
    y = 1/M;
  elseif (x < J*M)
    y = P1_limite(x-M,M)/M;
  elseif (x == J*M)
    y = (1/M)^J;
  else
    y = 0;	
  end;
end;

function y = P1_ouvert(x)
  global M;
  if (x < 1)
    y = 0;
  elseif (x < M)
    y = 1/M;
  else
    y = P1_ouvert(x-M,M)/M;
  end;
end;

function y = P1(x)
  y = P1_limite(x);
end;

for i = 1:N
  P1N(i) = P1(i);
end;

function y = diff(x)
  global N;
  global P1N;
  if (x < 0)
    c = -x;
  else
    c = x;
  end

  if (c >= N)
    y = 0;
    return;
  end

  y = 0;
  for i = (c+1):N
    y = y + P1N(i) * P1N(i-c);
  end;
end;

for i = -N:N
  PDN(i+N+1) = diff(i,P1N,N);
  if (i < 0)
    NSG(i+N+1) = 0.5 * 2^(i/3);
  else
    NSG(i+N+1) = 1 - 0.5 * 2^(-i/3);
  end;
end;

IPDN = -N:N;

IPDN(1) = PDN(1);
for i = -(N-1):N
  IPDN(i+N+1) = IPDN(i+N) + PDN(i+N+1);
end;

RANGE=-R:R;
IRANGE=N-R+1:N+R+1;

IPDN(IRANGE)

title("courbe de probabilité");
plot(RANGE, IPDN(IRANGE), "-.", RANGE, NSG(IRANGE), "-");
r = scanf("%s","C");
title("erreur absolue");
plot(RANGE, abs(IPDN(IRANGE)-NSG(IRANGE))*100);
r = scanf("%s","C");
title("erreur relative");
plot(RANGE, abs(IPDN(IRANGE)-NSG(IRANGE))./NSG(IRANGE)*100);
r = scanf("%s","C");

