Clear Filters
Clear Filters

index must be a positive integer or logical.

1 view (last 30 days)

Hi all,

Currently I'm trying to create a battery simulation in matlab simulink. I'm still in the middle of myresearch so I'm trying different things. I came across this book: 'Electric Vehicle Technology Explained' by James Larminie & John Lowry. I wanted to try one of their scripts for battery simulation but I keep getting the following error:

    Attempted to access open_circuit_voltage_LA(0,6); index must be a positive integer or logical. 
Error in TEST (line 28)
V(0)= open_circuit_voltage_LA(0,NoCells) - I(1)*Rin;

I was hoping you guys could help me out and explain to me the problem and solution. Thanks a bunch, I'll post the code down below.

CODE:
% M file for Figure 2.15
% Simple battery discharge experiment for a large 5 cell NiCad
% battery.
% Form some arrays for holding data.
% Time will run from 0 to 10000 seconds
T=(0:50:50000);
DoD=zeros(1,1001); % Depth of discharge, start off fully charged.
V=zeros(1,1001); % Battery voltage will be calculated at each time
			% step, so fill array with zeros initially.
I=linspace(100,100,1001); % Make current constant, and = 20 amps
NoCells=6	;% 6 cell battery
Capacity=50;  % This is the normal "10 hour" capacity
k=1.12		;% Peukert coefficient, not much greater than 1.
deltaT = 50;	% Take 50 second time steps, OK for constant current
% Calculated values
Rin= (0.022/Capacity)*NoCells;	% Internal resistance
PeuCap= ((Capacity/10)^k)*10; % See equations 2.7 to 2.9
% Starting voltage set outside loop
V(0)= open_circuit_voltage_LA(0,NoCells) - I(1)*Rin;
for n=2:1001
 I_cor= (I(n))^k; % Current corrected by Peukert
 DoD(n)= ((PeuCap * DoD(n-1)) + ((I_cor * deltaT)/3600))/PeuCap;
 if DoD(n)>1 
    DoD(n)=1;
 end
 V(n)=open_circuit_voltage_LA(DoD(n),NoCells) - I(n)*Rin;
 % We will say that the battery is "dead" if the
 % depth of discharge exceeds 99%
 if DoD(n)>0.99
    V(n)=0;
    end
end
C=zeros(1,1001);
C(1)=0;
for n=2:1001
 if V(n)>0
    C(n)=C(n-1)+ ((I(n)*deltaT)/3600);
 else 
    C(n)=C(n-1);
 end
end
for n=1:1001
 I(n)=5;
end
V2=zeros(1,1001);
% Starting voltage set outside loop
V2(1)= open_circuit_voltage_LA(0,NoCells) - I(1)*Rin;
for n=2:1001
 I_cor= (I(n))^k; % Current corrected by Peukert
 DoD(n)= ((PeuCap * DoD(n-1)) + ((I_cor * deltaT)/3600))/PeuCap;
 if DoD(n)>1 
    DoD(n)=1;
 end
 V2(n)=open_circuit_voltage_LA(DoD(n),NoCells) - I(n)*Rin;
 % We will say that the battery is "dead" if the
 % depth of discharge exceeds 99%
 if DoD(n)>0.99
    V2(n)= 0;
    end
end
C2=zeros(1,1001);
C2(1)=0;
for n=2:1001
 if V2(n)>0
    C2(n)=C2(n-1)+ ((I(n)*deltaT)/3600);
 else 
    C2(n)=C2(n-1);
 end
end
for n=1:1001
 I(n)=50;
end
V3=zeros(1,1001);
% Starting voltage set outside loop
V3(1)= open_circuit_voltage_LA(0,NoCells) - I(1)*Rin;
for n=2:1001
 I_cor= (I(n))^k; % Current corrected by Peukert
 DoD(n)= ((PeuCap * DoD(n-1)) + ((I_cor * deltaT)/3600))/PeuCap;
 if DoD(n)>1 
    DoD(n)=1;
 end
 V3(n)=open_circuit_voltage_LA(DoD(n),NoCells) - I(n)*Rin;
 % We will say that the battery is "dead" if the
 % depth of discharge exceeds 99%
 if DoD(n)>0.99
    V3(n)= 0;
    end
end
C3=zeros(1,1001);
C3(1)=0;
for n=2:1001
 if V3(n)>0
    C3(n)=C3(n-1)+ ((I(n)*deltaT)/3600);
 else 
    C3(n)=C3(n-1);
 end
end
plot(C,V,'b.',C2,V2,'r.',C3,V3,'m.');
axis([0 55 7 14]);
end

Accepted Answer

Image Analyst
Image Analyst on 16 Sep 2015
I think that describes it well enough that you can figure it out using the debugger.
  3 Comments
Image Analyst
Image Analyst on 16 Sep 2015
OK, I tried to run it but after removing the extra "end" I could not get past this line:
V(0)= open_circuit_voltage_LA(0,NoCells) - I(1)*Rin;
You should provide open_circuit_voltage_LA and any other data necessary to demonstrate the error.
Michel de Jongh
Michel de Jongh on 16 Sep 2015
Edited: Michel de Jongh on 16 Sep 2015
That is the exact line that I keep getting stuck on. This is the exact code I got from the book mentioned above. (except for the extra end btw) I believe it is supposed to calculate open_circuit_voltage_LA.
I didn't get any other data with this code. Even when I state that open_circuit_voltage_LA = 6; I still get the same error.

Sign in to comment.

More Answers (0)

Categories

Find more on Energy Storage in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!