Help Index exceeds the number of array elements?

11 views (last 30 days)
The error i am recieving is:
Index exceeds the number of array elements. Index must not exceed 0.
Error Line 39: disp(['Book 1: ', num2str(x(1))]);
I bellive the issues might be occuring in:
Line 34: options = optimoptions('linprog', 'Display', 'off'); or
Line 35 [x, fval] = linprog(f, A, b, [], [], lb, [], options);
But can't find what is causing the error.

Answers (2)

Shubham
Shubham on 21 Aug 2024
Hi James,
The error message you're encountering, "Index exceeds the number of array elements. Index must not exceed 0," indicates that you're trying to access an element of the array x that doesn't exist. This typically happens if the linprog function fails to find a feasible solution, resulting in an empty x.
Here's a breakdown of how you can troubleshoot and resolve this issue:
Troubleshooting Steps
  1. Check the Output of linprog:
  • Ensure that linprog is returning a valid solution. If it fails to find a feasible solution, x will be empty, which leads to the error when you try to access x(1).
2. Inspect the Inputs to linprog:
  • Verify that the inputs to linprog are correctly defined. Check the dimensions and values of f, A, b, lb, and any other constraints you are using. Ensure that they are consistent and correctly set up to define a feasible problem.
3. Check for Feasibility:
  • Make sure that the linear programming problem is feasible. If the constraints are too strict or contradictory, linprog will not find a solution. You might want to try running linprog without the options argument initially to see if it provides any useful diagnostic messages.
4. Examine the options Configuration:
  • Although unlikely to be the direct cause, double-check the options set in optimoptions. Ensure that they are appropriate for your problem. You can temporarily set 'Display', 'iter' to see more detailed output from the solver, which might help diagnose the issue.
  2 Comments
Walter Roberson
Walter Roberson on 21 Aug 2024
Also: ask linprog for at least three outputs:
[x, fval, exitflag] = linprog(f, A, b, [], [], lb, [], options);
James
James on 21 Aug 2024
  1. Not sure how to check the output of linprog. I treid adding an upper bound and nothing changed.
  2. all the inputs are correct from what i can tell. the only diference i have from a working model is constrains for x3 and x4.
  3. checking the feasibility by removing [options = optimoptions('linprog', 'Display', 'off');] results in Unrecognized function or variable 'options'. Error in line 35 [x, fval, EXITFLAG] = linprog(f, A, b, [], [], lb, [], options);
  4. The options configuration produces:
Running HiGHS 1.6.0: Copyright (c) 2023 HiGHS under MIT licence terms
Presolving model
Solving the original LP with primal simplex to determine infeasible or unbounded
Using EKK primal simplex solver
Iteration Objective Infeasibilities num(sum)
0 -1.5999991911e+02 Ph1: 2(320); Du: 1(1) 0s
2 -5.6000000000e+02 Pr: 0(0); Du: 4(25.5) 0s
Using EKK primal simplex solver
Iteration Objective Infeasibilities num(sum)
2 -5.5999990948e+02 Pr: 0(0); Du: 4(80) 0s
3 -1.3463333333e+03 Pr: 0(0); Du: 3(25.5556) 0s
Model status : Unbounded
Simplex iterations: 3
Objective value : -1.3463333333e+03
HiGHS run time : 0.00
Problem is unbounded.
I am not certain what the options should be but they seem correct.

Sign in to comment.


Saurav
Saurav on 21 Aug 2024
Edited: Saurav on 21 Aug 2024
Hi James,
The error message you're encountering, "Index exceeds the number of array elements. Index must not exceed 0," indicates that you are trying to access an element in an array that does not exist. Specifically, it seems you are trying to access ‘x(1)’ on line 39, but ‘x’ is either empty or not defined properly.
Here's a demonstration of the issue:
x=[];
x(1)
Index exceeds the number of array elements. Index must not exceed 0.
In this example, ‘x’ is an empty array, so attempting to access ‘x(1)’ results in an error. You might want to check how ‘x’ is being defined or populated in your code to resolve this issue.
Here are some potential reasons and solutions:
[x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, [], options);
disp('x:');
disp(x);
exitflag
output
By addressing these points, you should be able to identify why ‘x’ is not being populated correctly and fix the issue.
If the problem persists, consider providing more context or code for further assistance.
Thanks!
  2 Comments
James
James on 21 Aug 2024
I still wasn't able to fiqure it out but here is the code I have so hopefully you can tell me where the mistake is.
clear
clc
f = [-10, -12, -8, -20];
A1 = [3, 8, 2, 0];
b1 = 620 - (2 * 13);
A2 = [50, 70, 80, 0];
b2 = 9500 - 65 * 13;
A3 = [-0.5, 0, 1, 0];
b3 = 0;
A4 = [0, 0, -1, 0];
b4 = -20;
A = [A1; A2; A3; A4];
b = [b1; b2; b3; b4];
lb = [0, 0, 20, 0];
options = optimoptions('linprog', 'Display', 'off');
[x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, [], options);
disp('Optimal number of books to order with updated profit for Book 4:');
Optimal number of books to order with updated profit for Book 4:
disp(['Book 1:', num2str(x(1))]);
Index exceeds the number of array elements. Index must not exceed 0.
disp(['Book 2:', num2str(x(2))]);
disp(['Book 3:', num2str(x(3))]);
disp(['Book 4:', num2str(13)]);
disp(['Maximum Profit: ', num2str(-fval)]);
Walter Roberson
Walter Roberson on 21 Aug 2024
f = [-10, -12, -8, -20];
A1 = [3, 8, 2, 0];
b1 = 620 - (2 * 13);
A2 = [50, 70, 80, 0];
b2 = 9500 - 65 * 13;
A3 = [-0.5, 0, 1, 0];
b3 = 0;
A4 = [0, 0, -1, 0];
b4 = -20;
A = [A1; A2; A3; A4];
b = [b1; b2; b3; b4];
lb = [0, 0, 20, 0];
options = optimoptions('linprog', 'Display', 'off');
[x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, [], options);
if exitflag < 0
fprintf('exitflag negative, was: %d\n', exitflag)
else
disp('Optimal number of books to order with updated profit for Book 4:');
disp(['Book 1:', num2str(x(1))]);
disp(['Book 2:', num2str(x(2))]);
disp(['Book 3:', num2str(x(3))]);
disp(['Book 4:', num2str(13)]);
disp(['Maximum Profit: ', num2str(-fval)]);
end
exitflag negative, was: -3
-3 Problem is unbounded.

Sign in to comment.

Categories

Find more on Problem-Based Optimization Setup 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!