%Lab 2
%Written by Jeannette Spuhler, 2011-09-07
%Modified 2011-11-03
%Simpson rule
%NUM 6.2a
clear all; clc;
n=9;
h=3/n;
x=0;
y=x+h; 
simsum = 0;

for i =1:n
    simsum = simsum+((y-x)/6)*(f62a(x)+4*f62a((x+y)/2)+f62a(y));
    x= y;
    y=x+h;
end

Savesimsum = simsum;

%Bisection of the step size
times = 5;
steph = [];
for steghalvering = 1:times
     n=2*n;
     h=h/2;
     steph = [steph; h];
     x=0;
     simsum = 0;
     y=x+h;
     for i =1:n
        simsum = simsum+((y-x)/6)*(f62a(x)+4*f62a((x+y)/2)+f62a(y));
        x= y;
        y=x+h;
      end
     Savesimsum = [Savesimsum; simsum];
end

%Calculate order of the trapezoidal rule
DiffT = [];
for i =1:times
 DiffT = [DiffT;Savesimsum(i+1)- Savesimsum(i)];
end

loglog(steph, abs(DiffT), '-o'); hold on;
loglog([1e-2 1e-1],[1e-8 1e-4], 'g');  %Show reference courve for the order
legend('Calculated','Reference');
grid on;
title('Lab2  - Noggrannhetsordning Simpsons regel'); 
ylabel('loglog');
xlabel('h');
hold off;
