Array indices must be positive integers or logical values.

5 views (last 30 days)
I am not sure why my code is coming up with this:
Array indices must be positive integers or logical values.
Error in P9_07_Casey_Galerneau (line 18)
monthly =
principal*((interest(1+interest)^number_payments)/((1+interest)^number_payments)-1);
>>
  8 Comments
Walter Roberson
Walter Roberson on 23 Apr 2018

"for" must be followed in the same line by something in the form of an assignment.

Casey Galerneau
Casey Galerneau on 24 Apr 2018

principal = input('Enter principal in dollars: ');

years = input('Enter number of years: ');

interest = 0.0025;

number_payments = years*12;

top = (interest(1+interest)^number_payments);

bottom = ((1+interest)^number_payments)-1;

fprintf('****************************************************\n');

fprintf(' MORTGAGE PAYMENT CALCULATIONS\n');

fprintf('\nPrincipal = $%6.0d\n\n', principal);

while years <= 30

    for interest = 0.0025:0.0025:0.05
       monthly = (principal*(top/bottom));
       fprintf('Interest Rate     Monthly Payment\n');
       fprintf('     %4.2d             %7.2d', interest, monthly);
    end

end

Ok, now I really don't understand. I am getting this error after all of this code:

Array indices must be positive integers or logical values.

Error in P9_07_Casey_Galerneau (line 11) top = -(interest(1+interest)^number_payments);

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Apr 2018
interest(1+interest) attempts to index the vector or array interest at location(s) 1+interest . That is not necessarily impossible -- for example if interest = [0 1 2] then 1+interest would be 1 2 3 and interest([1 2 3]) would be valid. It is, however, rather suspect.
You should probably recheck your formula to see whether the formula asked for indexing at that point, or asked for multiplication.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!