when i run my code, it says index exceeds number of array elements. But i have used sum() which should by itself determine the array length, right? The final value of expression is a single integer which varies continuously in each iteration of loop.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 30일
편집: Ameer Hamza 2020년 9월 30일

1 개 추천

No, the error is caused by missing multiplication operators on line 33. * Should be placed after Kad at two places on this line.
A new error occurs after the above correction that is caused because you are trying to access the 299th element of vector Cs, whereas it only has 294 elements.

댓글 수: 3

why is sum() bothered with value of i? it should simply sum the elements in array and give me the result. right? what should i do if i want to code like what i want
Stephen23
Stephen23 2020년 9월 30일
편집: Stephen23 2020년 9월 30일
"why is sum() bothered with value of i?"
The error has nothing to do with sum.
"what should i do if i want to code like what i want"
If you want to multiply two variables, then you need to use a multiplication operator.
Abhishek Varma
Abhishek Varma 2020년 9월 30일
편집: Abhishek Varma 2020년 9월 30일
No, i made the change to code.
Its now showing 'index exceed array element (294)'.
which means since i is varying from 299 to 1, it contradicts Cs which has only 294 elements. So, if i want to vary i from 299 to 1 at the same time, have to find the sum of elements in Cs, Cl, Cm which has 294,6,6 elements respectively, what should i do?
Or is this error purely because i missed * operator at other points im not aware of?

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 9월 30일

0 개 추천

You have
Ef = (((Kad -(Aem*Kad(1+(((i-1)*sum(Cs,2))/K1s)))-(((i-1)*(sum(Cl,2)-Cl(1)-Cl(2)))/K1l)- (Cl(1)/KG1)- (Cl(2)/KG2)-1)) + ((((Kad -(Aem*Kad(1+(((i-1)*sum(Cs,2))/K1s)))-((i-1)*((sum(Cl,2)-Cl(1)-Cl(2))/K1l))- (Cl(1)/KG1)- (Cl(2)/KG2)-1))^2) + 4*Kad*((((i-1)*sum(Cl,2)-Cl(1)-Cl(2))/K1l)+(Cl(1)/KG1)+(Cl(2)/KG2)+1))^0.5)/(2*Kad*((((i-1)*sum(Cl,2)-Cl(1)-Cl(2))/K1l)+(Cl(1)/KG1)+(Cl(2)/KG2)+1));
That is a request to index the scalar Kad at offset (1+(((i-1)*sum(Cs,2))/K1s)) . That offset happens to be an integer that is greater than 1.
Reminder; MATLAB has absolutely no implied multiplication. When you see something of the form A(B) then it always means either invoking function A with parameter B, or else indexing array A with index B. It **never* means multiplying A by B. Not anywhere in MATLAB. Not even inside the internal programming language of the symbolic engine.

카테고리

질문:

2020년 9월 30일

편집:

2020년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by