%Lab 2
%Written by Jeannette Spuhler, 2011-09-07
%Modified 2011-11-03

%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)));
%      disp('Stepsize h = : ');
%      disp(h);
%      disp('Value calcuated with trapezoidal rule: ');
%      disp(T);
     SaveT = [SaveT; T];
end

%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([1e-2 1e-1],[1e-4 1e-2], 'g');  %Show reference curve for the order

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

