我试图在Octave中使用递归来实现和自适应正交算法,并且我得到一个对我来说没有意义的数组越界错误。
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: 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
31行和32行不访问数组,只是进行递归调用。第15行只是使用访问数组的不同元素,其长度为5,因此,为什么要么给出超出范围的错误也没有意义。