'Array indices must be positive integers or logical values' error in my code

조회 수: 2 (최근 30일)
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?

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 23일
You missed multiplication operator (*) at several locations in the file test.m. Check the attached file and see the difference.
  댓글 수: 6
Steven Lord
Steven Lord 2020년 9월 23일
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
Abhishek Varma 2020년 9월 24일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by