%ALIANJECON OTERO
%%
clc, clear, close all;
fs = 'rmse = %d';
fs2 = 'dx = %.2f';

dxsteps = 0.1:-0.01:0.01;
fdaerror = zeros(1,size(dxsteps,2));
bdaerror = fdaerror;
cdaerror = bdaerror;

loop = 1;
k = 1;
%%
for dx = 0.1:-0.01:0.01
x = 0:dx:2*pi;
N = size(x,2);
y = sin(x);
ypanalyticfda = cos(x(1:N-1));
ypanalyticbda = cos(x(2:N));
ypanalyticcda = cos(x(2:N-1));

%%

ypfda = (y(2:N)-y(1:N-1))/dx;

figure(loop);
plot(x,y,'k');
hold on;
plot(x(1:N-1),ypanalyticfda,'r');
plot(x(1:N-1),ypfda,'b');
axis([0 2*pi -1 1])

fdaresid = ypfda - ypanalyticfda;
fdaresidsq = fdaresid.^2;
fdameansquare = mean(fdaresidsq);
fdarmse = fdameansquare.^(1/2);

text(0,-0.75,0,num2str(fdarmse,fs))
title(["fda" , num2str(dx,fs2)])
fdaerror(k) = fdarmse;
loop = loop + 1;

%%

ypbda = (y(2:N)-y(1:N-1))/dx; 

figure(loop);
plot(x,y,'k');
hold on;
plot(x(2:N),ypanalyticbda,'r');
plot(x(2:N),ypbda,'b');
axis([0 2*pi -1 1])

bdaresid = ypbda - ypanalyticbda;
bdaresidsq = bdaresid.^2;
bdameansquare = mean(bdaresidsq);
bdarmse = bdameansquare.^(1/2);

text(0,-0.75,0,num2str(bdarmse,fs))

title(["bda" , num2str(dx,fs2)])
bdaerror(k) = bdarmse;
loop = loop + 1;

%%

ypcda = (y(3:N)-y(1:N-2))/(2*dx);

figure(loop)
plot(x,y,'k');
hold on;
plot(x(2:N-1),ypanalyticcda,'r');
plot(x(2:N-1),ypcda,'b');
axis([0 2*pi -1 1])

cdaresid = ypcda - ypanalyticcda;
cdaresidsq = cdaresid.^2;
cdameansquare = mean(cdaresidsq);
cdarmse = cdameansquare.^(1/2);

text(0,-0.75,0,num2str(cdarmse,fs))

title(["cda" , num2str(dx,fs2)])
cdaerror(k) = cdarmse;
loop = loop + 1;
k = k + 1;
end

figure(loop)
plot(dxsteps,fdaerror)
hold on;
plot(dxsteps, bdaerror)
plot(dxsteps, cdaerror)
title('Error vs Dx')
