Dat Minh Ha
Dat Minh Ha

Reputation: 103

Why does MATLAB say that my array index must either be a positive integer or a logical value?

Here's the code section that I'm having trouble with:

function coeff(p, I) %linear coefficients for the adam-moulton method
    beta = [];
    syms u;
    B = 1;
    
    for j = 0 : (p-1)
        for i = 0 : (p-1)
            if ~(i == j)
                B = B * (u + i);
            end
        end
        beta = [beta, ((-1).^j)./( factorial(j) * factorial(p - j - 1) ) * integral(@(u) B(u), I(1), I(2))];
    end 
end

and MATLAB is telling me that right here:

beta = [beta, ((-1).^j)./( factorial(j) * factorial(p - j - 1) ) * integral(@(u) B(u), I(1), I(2))];

my array index must be a positive integer or a logical value, but I don't see why this should be an issue, since I'm simply trying to append to the pre-existing array beta. How could I make this work ?

Upvotes: 0

Views: 117

Answers (1)

BillyBouw
BillyBouw

Reputation: 348

The integral function needs an function with a function handle, you can transform B and adjust the integral statement;

for j = 0 : (p-1)
    for i = 0 : (p-1)
        if ~(i == j)
            B = B * (u + i);
        end
    end
    B = matlabFunction(B);
    beta = [beta, ((-1).^j)./( factorial(j) * factorial(p - j - 1) ) * integral(B, I(1), I(2))];

Upvotes: 2

Related Questions