Hello, I am trying to perform active battery equalisation by using flyback topology. However, when ever I use more than 4 batteries, I get the error "Index exceeds number of array elements (4)".
How do I resolve this issue?
In the code, pwm(n) is a pulse given to the battery through a switch. soc(n) is the state of charge of a battery. s represents a stop signal when all state of charge is equal.
function [pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
pwm5 = 0;
if (soc1 > soc2 || soc1 > soc3 || soc1 > soc4 || soc1 > soc5)
pwm1 = dutycycle(50, 1/47000);
end
if (soc2 > soc1 || soc2 > soc3 || soc2 > soc4 || soc2 > soc5)
pwm2 = dutycycle(50, 1/47000);
end
if (soc3 > soc1 || soc3 > soc2 || soc3 > soc4 || soc3 > soc5)
pwm3 = dutycycle(50, 1/47000);
end
if (soc4 > soc1 || soc4 > soc2 || soc4 > soc3 || soc4 > soc5)
pwm4 = dutycycle(50, 1/47000);
end
if (soc5 > soc1 || soc5 > soc2 || soc5 > soc3 || soc5 > soc4)
pwm5 = dutycycle(50, 1/47000);
end
if (soc1 == soc2 && soc1 == soc3 && soc1 == soc4 && soc1 == soc5)
s = 1;
else
s = 0;
end
end

4 Kommentare

Scott MacKenzie
Scott MacKenzie am 27 Jun. 2021
There are no vectors in your function. The error must be originating outside the function in the rest of your code somewhere. More details would help.
I've posted all the code that I've written.
Here is the model:
Scott MacKenzie
Scott MacKenzie am 27 Jun. 2021
Hmm, well I don't know then. I don't use this toolbox. Does the error message give a line number or a line of code?
What about the dutycycle function? Isn't the first argunt supposed to be a vector? You're passing in a scalar.
The error has been resolved. The transformer winding resistance was set for only 3 secondaries, so it was showing index error.
I have now set the winding resistance for 5 secondaries, corresponding to 5 batteries. I'm not getting the error now.
Thank you for your time professor MacKenzie.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 27 Jun. 2021
The error is not coming from this function. You don't index anything. Somewhere else in your code you are trying to extract an element of your array that doesn't exist.
a = 1:4;
% works
a(4)
ans = 4
% doesn't work
a(5)
Index exceeds the number of array elements (4).

9 Kommentare

I've posted all the code that I've written.
I do understand what the error is trying to say. However, I can't understand why I'm getting the error with this code when I haven't used any loops.
Perhaps you have overwritten the dutycycle function accidentally? Type the following command and share the results:
which dutycycle
On my computer, the result is "C:\Program Files\MATLAB\R2021a\toolbox\signal\signal\dutycycle.m".
Your function returns a result and no error if I pass in made-up values.
soc1 = 1;
soc2 = 2;
soc3 = 3;
soc4 = 4;
soc5 = 5;
[pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
pwm1 = 0
pwm2 = 0.0011
pwm3 = 0.0011
pwm4 = 0.0011
pwm5 = 0.0011
s = 0
% your function
function [pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
pwm5 = 0;
if (soc1 > soc2 || soc1 > soc3 || soc1 > soc4 || soc1 > soc5)
pwm1 = dutycycle(50, 1/47000);
end
if (soc2 > soc1 || soc2 > soc3 || soc2 > soc4 || soc2 > soc5)
pwm2 = dutycycle(50, 1/47000);
end
if (soc3 > soc1 || soc3 > soc2 || soc3 > soc4 || soc3 > soc5)
pwm3 = dutycycle(50, 1/47000);
end
if (soc4 > soc1 || soc4 > soc2 || soc4 > soc3 || soc4 > soc5)
pwm4 = dutycycle(50, 1/47000);
end
if (soc5 > soc1 || soc5 > soc2 || soc5 > soc3 || soc5 > soc4)
pwm5 = dutycycle(50, 1/47000);
end
if (soc1 == soc2 && soc1 == soc3 && soc1 == soc4 && soc1 == soc5)
s = 1;
else
s = 0;
end
end
Cris LaPierre
Cris LaPierre am 28 Jun. 2021
I just noticed that this is in relation to a simulink model. Can you please share a screenshot with the error message showing?
Cris LaPierre
Cris LaPierre am 28 Jun. 2021
Bearbeitet: Cris LaPierre am 28 Jun. 2021
The error does not appear to be with your MATLAB Function block (my best guess). If it were, I believe the info under the error message would be "Component: MATLAB Function | Category: Coder error". Try commenting out the MATLAB Function block and see if you still get the error.
Also, is that the full error message? I feel like there should be more that what is shown. If there is, please copy/paste all of the text.
Yes, this is the full error message.
Cris LaPierre
Cris LaPierre am 28 Jun. 2021
And what happens when you comment out the function block?
The error has been resolved. The transformer winding resistance was set for only 3 secondaries, so it was show index error.
I have now set the winding resistance for 5 secondaries, corresponding to 5 batteries. I'm not getting the error now.
Thank you for your time Cris.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Jun. 2021

Bearbeitet:

am 28 Jun. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by