'Array indices must be positive integers or logical values' error in my code
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have attached two codes here. when i try to run it, it gives the error 'array indices must be positive integers or logical values'. Now i tried initializing the code with initial values > 0. still the same error is occuring. Can someone help me clear it or tell me why is it so?
Accepted Answer
Ameer Hamza
on 23 Sep 2020
You missed multiplication operator (*) at several locations in the file test.m. Check the attached file and see the difference.
6 Comments
Abhishek Varma
on 23 Sep 2020
No, i haven't. I checked it once more. I dont see where i might have missed it. Can you specify location?
Ameer Hamza
on 23 Sep 2020
You can download the file i attached to the answer. It is on line 30 to 34. The variable Eendol needs to be multiplied.
Abhishek Varma
on 23 Sep 2020
ohh yes. Thank you sir. I didnt notice it before
Ameer Hamza
on 23 Sep 2020
I am glad to be of help!!! :)
Steven Lord
on 23 Sep 2020
As an FYI, when I tried running the test function on its own (rather than as the ODE function in a call to one of the ODE solvers, which I suspect is how you're using it) with some dummy data the error message specifically calls out the line where the error occurs. This isn't always the case, sometimes a problem caused by one line is only detected by MATLAB on the next line, but this information can provide a useful starting point for looking for the problem.
>> test(0, 1:21)
Array indices must be positive integers or logical values.
Error in test (line 30)
((C5m - C5l)/t5m) - 0.1335*Eexol*C5l + 0.473*Eendol(2*C6l-4*C5l) + 0.435*(0.11088/(1+0.88*Eendol))*(C7s+8+12);
After I found and fixed the problem on line 30, running that command again showed me a new problem on line 31.
>> test(0, 1:21)
Index exceeds the number of array elements (1).
Error in test (line 31)
((C4m - C4l)/t4m) + 0.1335*Eexol*(2*C6l - C4l) +
0.473*Eendol(2*C5l+2*C6l-3*C4l) +
0.435*(0.11088/(1+0.88*Eendol))*(C7s+8+12);
Repeat until you've corrected any syntax errors. [Yes, since this is supposed to be an ODE function I should have passed in a column vector as the second input, but your code is agnostic to the orientation of that second input.]
Also, I have one stylistic suggestion. There are several constants that appear throughout your equations: 0.1335, 0.473, etc. I'm guessing those are either concentration data or some physical or chemical properties of the enzymes or species whose behavior you're trying to model. Once those quantities appear more than two or three times in your equations I'd probably elevate them to a named constant in the "constants defined here" section of your code. Doing so will help you distinguish between constants that play different roles in your equations but have similar values (are any of those locations where you use 0.473 actually supposed to be 0.435, for instance.) If the variable names are chosen well, this can help self-document your code. As an example, can you tell me the likely purpose of this next line of code?
discount = calculateDiscount(quantities, pricesPerUnit);
Abhishek Varma
on 24 Sep 2020
discount is calculated based on quantitles taken and its corresponding price per unit. so that in variable with a relevant name its easy to see the purpose of code i think so.
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)