
% first we did 3 studies of the trapezoidal rule, first by halving
% the intervals then by dividing them into thirds, then by dividing them
% into fifths

% here we're halving them
>> [area,h,error(1)] = trap(1/2,2,2+1);
>> [area,h,error(2)] = trap(1/2,2,4+1);
>> [area,h,error(3)] = trap(1/2,2,8+1);
>> [area,h,error(4)] = trap(1/2,2,16+1);
>> [area,h,error(5)] = trap(1/2,2,32+1);
>> [area,h,error(6)] = trap(1/2,2,64+1);
>> format long
>> error

error =

  Columns 1 through 4 

   0.06551028791421   0.01719796097309   0.00436849859690   0.00109695981312

  Columns 5 through 6 

   0.00027455285855   0.00006865795423

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

% look: the ratio is going to 4 = 2^2!
ratio =

  Columns 1 through 4 

   3.80918924148628   3.93681274964785   3.98236885678875   3.99544123825312

  Column 5 

   3.99884997478365

% now we divide the intervals into thirds:
>> [area,h,error(1)] = trap(1/2,2,3+1);
>> [area,h,error(2)] = trap(1/2,2,3^2+1);
>> [area,h,error(3)] = trap(1/2,2,3^3+1);
>> [area,h,error(4)] = trap(1/2,2,3^4+1);
>> [area,h,error(5)] = trap(1/2,2,3^5+1);
>> [area,h,error(6)] = trap(1/2,2,3^6+1);
>> error

error =

  Columns 1 through 4 

   0.03013539734578   0.00345584759630   0.00038559482734   0.00004286436943

  Columns 5 through 6 

   0.00000476296169   0.00000052922110

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

% look, the ratio's going to 9 = 3^2!
ratio =

  Columns 1 through 4 

   8.72011757059565   8.96238059026674   8.99569578289656   8.99952009847873

  Column 5 

   8.99994666510977

% now divide the intervals by 5
>> [area,h,error(1)] = trap(1/2,2,5+1);
>> [area,h,error(2)] = trap(1/2,2,5^2+1);
>> [area,h,error(3)] = trap(1/2,2,5^3+1);
>> [area,h,error(4)] = trap(1/2,2,5^4+1);
>> [area,h,error(5)] = trap(1/2,2,5^5+1);
>> error

error =

  Columns 1 through 4 

   0.01108781654781   0.00044971767121   0.00001799954648   0.00000071999927

  Column 5 

   0.00000002880000

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

>> ratio
% look, the ratio's going to 5^2!
ratio =

  24.65506084717369  24.98494458275768  24.99939529159459  24.99997614176852


% now we ask the same question for the left-hand rule and Simpsons
% rule, just for the halving case

>> [area,h,error(1)] = left_hand(1/2,2,2+1);
>> [area,h,error(2)] = left_hand(1/2,2,2^2+1);
>> [area,h,error(3)] = left_hand(1/2,2,2^3+1);
>> [area,h,error(4)] = left_hand(1/2,2,2^4+1);
>> [area,h,error(5)] = left_hand(1/2,2,2^5+1);
>> [area,h,error(6)] = left_hand(1/2,2,2^6+1);
>> error

error =

  Columns 1 through 4 

   0.58537067333416   0.27712815368307   0.13433359495189   0.06607950799061

  Columns 5 through 6 

   0.03276582694729   0.01631429499860

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

% look, the ratio's going to 2 = 2^1!
ratio =

  Columns 1 through 4 

   2.11227428738115   2.06298471936468   2.03290852242692   2.01672028900429

  Column 5 

   2.00841206745957

% repeat for Simpsons rule:
>> for i=1:6, [area,h,error(i)] = simp(1/2,2,2^i+1); end
>> error

error =

  Columns 1 through 4 

   0.00972440008565   0.00109385199272   0.00009201113817   0.00000644688519

  Columns 5 through 6 

   0.00000041720702   0.00000002631946

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

% look, the ratio's going to 16!
ratio =

  Columns 1 through 4 

   8.89005107670603  11.88825629696268  14.27218501561591  15.45248482009106

  Column 5 

  15.85165612974168

>> [area,h,error(7)] = simp(1/2,2,2^7+1);
>> [area,h,error(8)] = simp(1/2,2,2^8+1);
>> [area,h,error(9)] = simp(1/2,2,2^9+1);
>> for i=1:8, ratio(i) =error(i)/error(i+1); end
>> ratio

ratio =

  Columns 1 through 4 

   8.89005107670603  11.88825629696268  14.27218501561591  15.45248482009106

  Columns 5 through 8 

  15.85165612974168  15.96206969728998  15.99044642821094  15.99794597527419


% now we damage the simp.m program by consciously introducing a bug
% and then repeat the calculation.
>> for i=1:9, [area,h,error(i)] = simp(1/2,2,2^i+1); end
>> for i=1:8, ratio(i) =error(i)/error(i+1); end
>> ratio

% look, the ratio's now going to 2 and the errors aren't small
ratio =

  Columns 1 through 4 

   1.91190255834378   1.97895359032390   1.99634636107983   1.99948176664261

  Columns 5 through 8 

   1.99993267671661   1.99999149738850   1.99999893437774   1.99999986670871

>> error(6)

ans =

  -0.00541518602866

>> diary off
