//
// Lab 3 Program 2
//
// A simple program to integrate a function
//

// define the function
function foo=f(x)
	foo = (5 * x) + ((sin((9 * x) - 5)) / x);
endfunction

// Set the time length and step size
steps = input("How many iterations?")
x_start = input("Enter starting point");
x_end = input("Enter ending point");
x_delta = (x_end - x_start) / steps;


//
// Loop for rectangular integration
//
total = 0; // set the initial sum to zero
for i=0:steps,
	x = x_start + i * x_delta;
	total = total + f(x);
end
total = total * x_delta;
printf("Rectangular integration value %f\n", total);


//
// Loop for trapezoidal integration
//
total = 0; // set the initial sum to zero
for i=0:steps,
	x = x_start + i * x_delta;
	if i == 0 then
		total = total + f(x);
	elseif i == steps then
		total = total + f(x);
	else
		total = total + 2 * f(x);
	end
end
total = total * x_delta / 2;
printf("Trapezoidal integration value %f\n", total);


//
// Loop for Simpson's rule integration
//
total = 0; // set the initial sum to zero
even = 0;
for i=0:steps,
	x = x_start + i * x_delta;
	if i == 0 then
		total = total + f(x);
	elseif i == steps then
		total = total + f(x);
	else
		even = even + 1;
		if even > 1 then
			total = total + 4 * f(x);
			even = 0;
		else
			total = total + 2 * f(x);
		end
	end
end
total = total * x_delta / 3;
printf("Simpsons rule integration value %f\n", total);
