function dependant on previous value min for loop with different size
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Pedro Antunes
il 22 Dic 2021
Commentato: Walter Roberson
il 22 Dic 2021
this is my code, in the for loop running in t im trying to calculate I_batt which allows me to calculate the SOC which i then use to calculate the I_batt for the next t value, problem is I_batt has a different size and it gives me an error. how do i fix this?
clc;clear;close all;
%% fixed variables
theta = [-5:1:5]; % gradient of road in degrees
m = 1500; % mass of vehicle in kg
g = 9.81; % gravitational acc
Cr = 0.02; % rolling resistance coefficient
v_veh = 30; % velocity of vehicle in m/s
air_dens = 1.23; % air density in kg/m^3
Cd = 0.3; % drag coefficient
A = 1; % area of veh exposed to air in m^2
p_tyre = 1; %tyre pressure in Bar
Cr = 0.005+(1./p_tyre).*(0.01+0.0095.*(v_veh./100).^2); %rolling resistance depending on tyre pressure and velocity
Fc_max = 50000; %max power output of fuel cell in N
Voc = 4; % open circuit voltage in V
Ah = 70; % capacity of battery in Ah
Max_charge = Ah.*3600; %converting capacity of battery to Coulombs
%% for loop of simulation for hybrid vehicle
for n = 1:11
theta_n = theta(n); % gradient of road at each n
Fr(n) = m.*g.*Cr; % Resistance force due to rolling
Fa(n) = 0.5.*air_dens.*Cd.*A.*v_veh.^2; % resistance due to air drag
Fg(n) = m.*g.*sind(theta_n); % gradient resistance
Ft(n) = Fr(n)+Fa(n)+Fg(n);
Pt(n) = Ft(n).*v_veh;
if Pt<Fc_max
R0 = 1e-4;
C1 = 2e4;
R1 = 7.8e-4;
C2 = 2e5;
R2 = 3e-3;
Vh = -2e-3;
else
R0 = 8e-5;
C1 = 2e4;
R1 = 8e-4;
C2 = 2.2e5;
R2 = 3e-4;
Vh = 1e-3;
end
for t = 1:100
SOC{1} = 0.7;
if Pt < 0
SOC(t) = SOC(1);
end
if Pt(n)<Fc_max && Pt(n)>0
preq(n) = Fc_max - Pt(n);
Q{t} = SOC{t}.*Max_charge;
Vc1{t} = Q{t}./C1;
Vc2{t} = Q{t}./C2;
I_batt{t+1} = ((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n))-sqrt((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n)).^2-4.*R0(n).*preq(n)))./(2.*R0(n));
dSOC = -I_batt{t}./Q{t};
SOC{t+1} = SOC{t}+dSOC;
end
end
end
0 Commenti
Risposta accettata
Walter Roberson
il 22 Dic 2021
for t = 1:100
SOC{1} = 0.7;
Notice that this always assigns into SOC{1} no matter what the value of n or t are.
if Pt < 0
SOC(t) = SOC(1);
end
if Pt(n)<Fc_max && Pt(n)>0
preq(n) = Fc_max - Pt(n);
Q{t} = SOC{t}.*Max_charge;
Vc1{t} = Q{t}./C1;
Vc2{t} = Q{t}./C2;
I_batt{t+1} = ((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n))-sqrt((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n)).^2-4.*R0(n).*preq(n)))./(2.*R0(n));
dSOC = -I_batt{t}./Q{t};
SOC{t+1} = SOC{t}+dSOC;
end
Notice that I_batt{t+1} is assigned to inside the if statement. But t is minimum 1, so I_batt{2} can potentially get assigned to but not I_batt{1} . There is no place that assigns to I_batt{1} in your code.
Then on the line after that, I_batt{t} is referred to. But when t = 1, then that is I_batt{1} that was never assigned to, so I_batt{1} is empty. So you are computing with empty and so dSOC becomes empty.
Then SOC{t+1} is assigned depending on the calculation that uses empty dSOC, so SOC{t+1} --> SOC{t} becomes empty.
Then on the next iteration, when t becomes t, SOC{t} is referred to in calculating Q, and since SOC{2} is empty, Q{t} becomes empty. THne Vc1{t} becomes empty and so does Vc2{t} . Then in the calculation of I_batt{t+1} you have
I_batt{t+1} = ((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n))-sqrt((Voc-Vc1{t}-Vh+Vc2{t}-Vh(n)).^2-4.*R0(n).*preq(n)))./(2.*R0(n));
Notice the Voc-Vc1{t}-Vh+Vc2{t}-Vh(n) part. What size is Vh ? If it is a scalar then Vh(n) is not going to be valid for n > 1 (and in practice this code is not reached until n becomes 5). But if Vh is a vector, then -Vc1{t}-Vh becomes empty minus a complete vector, and that becomes a problem with mismatching array size dimension. Should it perhaps be Voc-Vc1{t}-Vh(n)+Vc2{t}-Vh(n) ? But if so, why subtract vh(n) two different times in the same sub-expression?
Also look at the size of Rh at that point.
2 Commenti
Walter Roberson
il 22 Dic 2021
When you get to
dSOC = -I_batt{t}./Q{t};
SOC{t+1} = SOC{t}+dSOC;
and you "ignore" the first value of I_batt, then you need some value for dSOC to add to SOC{t} to produce SOC{t+1}
if t == 1
SOC{t+1} = SOC{t} + SOMETHING
else
dSOC = -I_batt{t}./Q{t};
SOC{t+1} = SOC{t}+dSOC;
end
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Propagation and Channel Models in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!