'Array indices must be positive integers or logical values' error in my code
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abhishek Varma
le 23 Sep 2020
Commenté : Abhishek Varma
le 24 Sep 2020
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?
0 commentaires
Réponse acceptée
Ameer Hamza
le 23 Sep 2020
You missed multiplication operator (*) at several locations in the file test.m. Check the attached file and see the difference.
6 commentaires
Steven Lord
le 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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!