%Written by Jeannette Spuhler, 2011-09-07
%Modified 2012-10-11

%trapezoidal rule
%NUM 6.2a
% Trapetsmetod
clear all; clc;
n=9;
h=3/n; 
xapp=0:h:3; 
fapp=f62a(xapp);
T = h*(sum(fapp)-0.5*(fapp(1)+fapp(n+1)));
SaveT = T;

%Bisection of the step size
times = 5;
steph = [];
for steghalvering = 1:times
     n=2*n;
     h=h/2;
     steph = [steph; h];
     xapp=0:h:3;
     fapp=f62a(xapp);
     T = h*(sum(fapp)-0.5*(fapp(1)+fapp(n+1)));
     SaveT = [SaveT; T];
end
SaveT
 %felet minskas med faktor 1/4 
 fel=diff(SaveT)
 %felet minskas med 1/4
 faktor=fel(2)/fel(1)
 
 %inbyggd funktion: I= 2.1703, quad r�knar med adaptive Simpson quadrature
 mintergral=quad('f62a',0,3,1e-5)
 
 
%Calculate order of the trapezoidal rule
DiffT = [];
for i =1:times
 DiffT = [DiffT;SaveT(i+1)- SaveT(i)];
end

loglog(steph, DiffT, '-o'); hold on;
loglog(steph,steph.^2, 'g');

legend('Calculated','Reference');
grid on;
title('Noggrannhetsordning Trapetsregel'); 
ylabel('loglog');
xlabel('h');
hold off;
