%The input file is A4_Input1.txt and is inside the given folder, and the
%sample input is currently stored inside it. Kindly change the data inside
%that input file if you want to try out some other input.
fileIP = fopen('A4_Input1.txt','r');
A = fgetl(fileIP);
B = fscanf(fileIP,'%s',1);
a = str2double(B(1));
b = str2double(B(3));
e = fscanf(fileIP,'%f',1);
met = fscanf(fileIP,'%d',1);
for i = 1:length(A);
if A(i)=='=';
func = A(1,i+1:length(A));
end
end
f = str2func(['@(x)',func]);%assuming the function variable to be x!
switch (method)
case 1
%Romberg Integration
h = b-a;
I = zeros(1,100);
I(1) = h*(f(a)+f(b))/2;
err = 1;
for j = 2:100
h = h/2;
for i = a:h:b-h
I(j) = I(j) + h*(f(i)+f(i+h))/2;
end
for i = j:-1:2
I(i-1) = (I(i)*4^(j-i+1) - I(i-1))/(4^(j-i+1)-1);
end
err = (I(1)-I(2))*100/I(1);
if abs(err)<e
break;
end
end