Matrix indexing error in MATLAB

I keep getting this error in Matlab:

An attempt to access r (0,0); the index must be a positive integer or logical.

Error in ==> Romberg at 15

I launched it with Romberg(1.3, 2.19,8)

I think the problem is that the statement is not logical because I made it positive and still got the same error. Has anyone got some ideas on what I can do?

function Romberg(a, b, n)
    h = b - a;
    r = zeros(n,n);
    for i = 1:n
        h = h/2;
        sum1 = 0;

        for k = 1:2:2^(i)
            sum1 = sum1 + f(a + k*h);
        end

        r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;

        for j = 1:i
            r(i,j) = r(i,j-1) + (r(i,j-1) - r(i-1,j-1))/((4^j) - 1);
        end
    end
    disp(r);
end

function f_of_x = f(x)
    f_of_x = sin(x)/x;
end
+3
source share
3 answers

There are two lines in which you use 0 for indexing, which you cannot do in Matlab:

r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;

and

r(i,j) = r(i,j-1) + (r(i,j-1) - r(i-1,j-1))/((4^j) - 1);

when j == 1 or i == 1.

I suggest you run your loops starting at 2, and replace the indices i and j with (i-1) and (j-1) respectively.

:

for k = 1:2:2^(i)

   sum1 = sum1 + f(a + k*h);

end

k = 1:2:2^i;
tmp = f(a + k*h);
sum1 = sum(tmp);

f_of_x

sin(x)./x
+6

MATLAB 1. , r 0.

+3

You have zero indices in

r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;

This is not possible in MATLAB - all indexes begin with form 1.

+1
source

Source: https://habr.com/ru/post/1738122/


All Articles