Write a function called approximateIntegral that uses a loop
Write a function called approximateIntegral that uses a loop to compute a numerical approximation that adaptively changes ?x to smaller and smaller step sizes, until the approximation is accurate and precise within the number of decimal points defined by precision (difference between approxi+1 and approxi must be less than 1
Solution
function res = approximateIntegral( dx,refinement,precision )
approx = [];
j=1;
error = 10000;
increment = 1/dx;
increment = ceil(increment);
dx = 1/increment;
dx_a(1) = dx;
sum=0;
for i=1:dx:2
sum = sum + sin(i)*dx/i;
end
approx(j) = sum;
j=j+1;
dx = dx*refinement;
while(error > 10^-precision)
increment = 1/dx;
increment = ceil(increment);
dx = 1/increment;
dx_a(j)=dx;
sum=0;
for i=1:dx:2
sum = sum + sin(i)*dx/i;
end
approx(j) = sum;
error = abs(approx(j)-approx(j-1));
dx = dx*refinement;
j=j+1;
end
res = [dx_a\' approx\'];
end
