I'm trying to implement and adaptive quadrature algorithm using recursion in Octave, and I'm getting an array out of bounds error that doesn't quite make sense to me.
function integral = adaptive(f, a, b, TOL, count)
max_count = 20;
h = (b-a)/2;
i1_midpoints = a:h:b;
i2_midpoints = a:(h/2):b;
i1 = f(i1_midpoints(1)) + 4*f(i1_midpoints(2)) + f(i1_midpoints(3)); //line 15
i2 = f(i2_midpoints(1)) + 4*f(i2_midpoints(2)) + 2*f(i2_midpoints(3)) + 4*f(i2_midpoints(4)) + f(i2_midpoints(5));
if count == max_count
integral = i1;
return;
endif
disp(length(i1_midpoints));
disp(length(i2_midpoints));
disp(i1_midpoints);
disp(i2_midpoints);
if abs(i1 - i2) < (10 * TOL)
integral = i1;
return;
endif
count = count + 1;
adaptive(f, a, b/2, TOL, count); //line 31
adaptive(f, b/2, b, TOL, count); //line 32
Error message:
error: adaptive: A(I): index out of bounds; value 1 out of bound 0
error: called from:
error: C:\Users\ADam\Desktop\Adam\Numerical_Analysis\adaptive.m at line 15, column 4
error: C:\Users\ADam\Desktop\Adam\Numerical_Analysis\adaptive.m at line 31, column 1
error: C:\Users\ADam\Desktop\Adam\Numerical_Analysis\adaptive.m at line 32, column 1
Lines 31 and 32 aren't accessing arrays, just making recursive calls. Line 15 is just using accessing different elements of the array, which has a length of 5, so it doesn't make sense why either would give out of bounds errors.