
% For \int_1^2 sqrt(x), we're considering the exact error versus the
% asymptotic error.

>> for i=1:9, [area,h(i),error(i)] = trap(1,2,2^i+1); end
>> error

error =

  Columns 1 through 6 

  3.0256e-003  7.6109e-004  1.9058e-004  4.7665e-005  1.1917e-005  2.9794e-006

  Columns 7 through 9 

  7.4486e-007  1.8622e-007  4.6554e-008

% one way to find the asymptotic error:
>> error_asymp = -(fp(2)-fp(1))/12*h.^2

error_asymp =

  Columns 1 through 6 

  3.0510e-003  7.6274e-004  1.9069e-004  4.7671e-005  1.1918e-005  2.9795e-006

  Columns 7 through 9 

  7.4487e-007  1.8622e-007  4.6554e-008

% another way to find the asymptotic error:
>> for i=1:9, err_asymp(i) = -(fp(2)-fp(1))/12*h(i)^2 ; end
>> err_asymp

err_asymp =

  Columns 1 through 6 

  3.0510e-003  7.6274e-004  1.9069e-004  4.7671e-005  1.1918e-005  2.9795e-006

  Columns 7 through 9 

  7.4487e-007  1.8622e-007  4.6554e-008

% comparing the exact error to the asymptotic error, they are very close
>> error

error =

  Columns 1 through 6 

  3.0256e-003  7.6109e-004  1.9058e-004  4.7665e-005  1.1917e-005  2.9794e-006

  Columns 7 through 9 

  7.4486e-007  1.8622e-007  4.6554e-008

% look at their difference to see how close they are:
>> error - error_asymp

ans =

  Columns 1 through 6 

 -2.5381e-005 -1.6505e-006 -1.0429e-007 -6.5362e-009 -4.0880e-010 -2.5556e-011

  Columns 7 through 9 

 -1.5980e-012 -9.9850e-014 -6.3662e-015


  Columns 1 through 6 

  3.0256e-003  7.6109e-004  1.9058e-004  4.7665e-005  1.1917e-005  2.9794e-006

  Columns 7 through 9 

  7.4486e-007  1.8622e-007  4.6554e-008

% now we're comparing the trapezoidal rule to the corrected
% trapezoidal rule.  Start with the trapezoidal rule:
>> for i=1:9, [area,h(i),error(i)] = trap(1,2,2^i+1); end
>> for i=1:8, ratio(i) = error(i)/error(i+1); end
>> error

error =

  Columns 1 through 6 

  3.0256e-003  7.6109e-004  1.9058e-004  4.7665e-005  1.1917e-005  2.9794e-006

  Columns 7 through 9 

  7.4486e-007  1.8622e-007  4.6554e-008

% the ratio's going to 4
>> ratio

ratio =

  Columns 1 through 6 

  3.9753e+000  3.9935e+000  3.9984e+000  3.9996e+000  3.9999e+000  4.0000e+000

  Columns 7 through 8 

  4.0000e+000  4.0000e+000

% now repeat but with the corrected trapezoidal rule
>> for i=1:9, [area,h(i),error(i)] = corr_trap(1,2,2^i+1); end
>> error

error =

  Columns 1 through 6 

  2.5381e-005  1.6505e-006  1.0429e-007  6.5362e-009  4.0880e-010  2.5556e-011

  Columns 7 through 9 

  1.5981e-012  9.9920e-014  6.4393e-015

>> for i=1:8, ratio(i) = error(i)/error(i+1); end
% the ratio's going to 16
>> ratio

ratio =

  Columns 1 through 6 

  1.5378e+001  1.5826e+001  1.5955e+001  1.5989e+001  1.5996e+001  1.5992e+001

  Columns 7 through 8 

  1.5993e+001  1.5517e+001

% now compare simpsons rule to the corrected simpsons rule:
>> for i=1:9, [area,h(i),error(i)] = simp(1,2,2^i+1); end
>> error

error =

  Columns 1 through 6 

  8.5909e-005  6.2596e-006  4.1111e-007  2.6047e-008  1.6337e-009  1.0219e-010

  Columns 7 through 9 

  6.3884e-012  4.0012e-013  2.5979e-014

>> for i=1:8, ratio(i) = error(i)/error(i+1); end

% the ratio's going to 16
>> ratio

ratio =

  Columns 1 through 6 

  1.3724e+001  1.5226e+001  1.5783e+001  1.5944e+001  1.5986e+001  1.5997e+001

  Columns 7 through 8 

  1.5966e+001  1.5402e+001

>> for i=1:9, [area,h(i),error(i)] = corr_simp(1,2,2^i+1); end
>> error

error =

  Columns 1 through 6 

  2.1282e-005  4.3977e-007  7.6034e-009  1.2236e-010  1.9267e-012  3.0864e-014

  Columns 7 through 9 

  6.6613e-016 -8.8818e-016 -1.1102e-015

>> for i=1:8, ratio(i) = error(i)/error(i+1); end
% the ratio appears to be going to 64, but gets stopped by round-off
% error
>> ratio

ratio =

  Columns 1 through 6 

  4.8394e+001  5.7838e+001  6.2140e+001  6.3508e+001  6.2424e+001  4.6333e+001

  Columns 7 through 8 

 -7.5000e-001  8.0000e-001

>> diary off
