% Compara errores de truncamiento para los esquemas de aproximacion 
% de diferencias finitas centradas, atras y adelante. Grafica el error y el 
% orden estimado de convergencia

% u(x) = sin(x) en x=1. Derivada exacta u'(1) = cos(1)

clear; close all
h=1.0;
for k = 1:33,
   a(k,1) = h;
   a(k,2) = (sin(1 + h) - sin(1)) / h - cos(1);
   a(k,3) = (sin(1) - sin(1 - h)) / h - cos(1);
   a(k,4) = (sin(1 + h) - sin(1 - h)) / (2 * h) - cos(1);
   h = h / 2;
end
	
	
format short e
a % Visualiza el resultado
	
a = abs(a); % Toma el valor absoluto
h1 = a(:,1); % Extrae la primer columna que es la de h
e1 = a(:,2); e2 = a(:,3); e3 = a(:,4);
	
	
loglog(h1, e1, h1, e2, h1, e3)
axis('square')
axis([1e-6 1e1 1e-6 1e1])
gtext('Esperada de adelante y atras =1')
gtext('Esperada de central = 2')




   1.0000e+00  -4.7248e-01   3.0117e-01  -8.5654e-02
   5.0000e-01  -2.2825e-01   1.8379e-01  -2.2233e-02
   2.5000e-01  -1.1025e-01   9.9027e-02  -5.6106e-03
   1.2500e-01  -5.3929e-02   5.1118e-02  -1.4059e-03
   6.2500e-02  -2.6639e-02   2.5936e-02  -3.5169e-04
   3.1250e-02  -1.3235e-02   1.3059e-02  -8.7936e-05
   1.5625e-02  -6.5958e-03   6.5519e-03  -2.1985e-05
   7.8125e-03  -3.2925e-03   3.2815e-03  -5.4962e-06
   3.9062e-03  -1.6449e-03   1.6421e-03  -1.3741e-06
   1.9531e-03  -8.2209e-04   8.2141e-04  -3.4351e-07
   9.7656e-04  -4.1096e-04   4.1079e-04  -8.5879e-08
   4.8828e-04  -2.0546e-04   2.0542e-04  -2.1470e-08
   2.4414e-04  -1.0272e-04   1.0271e-04  -5.3673e-09
   1.2207e-04  -5.1361e-05   5.1358e-05  -1.3417e-09
   6.1035e-05  -2.5680e-05   2.5679e-05  -3.3579e-10
   3.0518e-05  -1.2840e-05   1.2840e-05  -8.3860e-11
   1.5259e-05  -6.4199e-06   6.4199e-06  -2.2014e-11
   7.6294e-06  -3.2100e-06   3.2100e-06  -1.8607e-13
   3.8147e-06  -1.6050e-06   1.6050e-06   7.0899e-12
   1.9073e-06  -8.0249e-07   8.0247e-07  -7.4620e-12
   9.5367e-07  -4.0120e-07   4.0125e-07   2.1642e-11
   4.7684e-07  -2.0062e-07   2.0055e-07  -3.6566e-11
   2.3842e-07  -1.0050e-07   1.0020e-07  -1.5298e-10
   1.1921e-07  -4.9746e-08   4.9906e-08   7.9849e-11
   5.9605e-08  -2.5532e-08   2.4760e-08  -3.8581e-10
   2.9802e-08  -1.0630e-08   1.1721e-08   5.4551e-10
   1.4901e-08  -6.9051e-09   7.9961e-09   5.4551e-10
   7.4506e-09   5.4551e-10   5.4551e-10   5.4551e-10
   3.7253e-09   5.4551e-10   5.4551e-10   5.4551e-10
   1.8626e-09  -2.9257e-08  -2.9257e-08  -2.9257e-08
   9.3132e-10  -2.9257e-08  -2.9257e-08  -2.9257e-08
   4.6566e-10  -2.9257e-08  -2.9257e-08  -2.9257e-08
   2.3283e-10  -2.9257e-08  -2.9257e-08  -2.9257e-08