Running a Simulink Model in a for loop - Index exceeds number of array elements

Hello everyone,
I am really trying to get this going, but i keep getting the error :
Index exceeds the number of array elements (2).
Error in Temperaturaenderung_cargo (line 71)
T_cargo2(j)=((V_in2(j-1)*dt*T_in)+(T_cargo2(j-1)*(V_cargo-V_in2(j-1)*dt-V_out))+(T_ambient*V_out))/(V_cargo);
So the first values look reasonable, the simulation always stops at a point that is defined by the SIMULINK model I guess. If i increase the simulationtime (have a look at the 2nd comment line in the script) The same error occurs, but the bold number, about the Index increases to one above the time i used for the SIMULINK.
So i feel like the loop runs dry on input provided through the SIMULINK model, as it stops after those 5 steps (seconds in this case).
Code:
%% Loop-Start
T_cargo(1)=T_cargo_start;
T_cargo2(1)=T_cargo_start;
T_AFT_duct2(1)=T_cargo2+18;
T_cargo2_set=T_cargo_start;
rho_cargo(1)=p_cargo/(T_cargo_start*R);
t_sim=60*60;
n=60*60;
dt=t_sim/n;
t3(1)=0;
t2(1)=0;
alpha(1)=1;
for j=2:n
t2(j)=dt*(j-1);
T_cargo2(j)=((V_in2(j-1)*dt*T_in)+(T_cargo2(j-1)*(V_cargo-V_in2(j-1)*dt-V_out))+(T_ambient*V_out))/(V_cargo);
T_AFT_duct2(j)=T_cargo2(j)+18;
T_cargo2_set=T_cargo2(1,j); %SIMULINK INPUT, in Order to have a single value as an inupt (made the simulink work in the first place)
sim_opts=simset('Solver','ode5','FixedStep',dt);
sim('OuterLoop',dt,sim_opts), %So here is the thing, if i vary THIS dt value (like 10*dt or sth) Simulation will stop and the error is 10*dt+1
[m_sub]=m_function(alpha_AFT); %This function converts alpha_AFT (Result of the SIMULINK) to m_sub
m_rot=rot90(m_sub);
V_in2=m_rot./rho;
end
Preset values for the code (if anybody is going to paste the stuff, ignore comments)
%% Konstante Parameter
R=287;
T_in=215+273.15;%[K] Bleed air Temperatur (konstant)
p_in=80000;%[Pa] Druck der bleed air unmittelbar vor Eintreten in den Cargobereich
d=44.45*10^-3;%[m] Innendurchmesser der Rohrleitung
p_cargo=79979.18;%[Pa] Druck im Cargobereich (konstant im Reiseflug)
T_ambient = 250;%[K] Außentemperatur (konstant im Reiseflug)
%% Massetrom berechnung (nur zu Testzwecken, wird von HATV vorgegeben)
u_in=20;%[m/s] Eintrittgeschwindigkeit der bleed air
rho=p_in/(T_in*R);%[kg/m^3] Dichte der bleed air
m_in=u_in*rho*pi/4*d^2;%[kg/s] Eingehender Massestrom der bleed air (Vorgegeben vom HATV(alpha))!!!!!!!!!!!!!
%% Rahmenbedingungen (Variabel)
T_cargo_start=20.5+273.15;%[K] Ausgangstemperatur des Vargobereichs
V_cargo=26.2;%[m^3] Gesamtvolumen des Cargobereichs (AFT)
T_AFT_soll=23+273.15;%[K] Vom Pilot vorgegebene Temperatur des Cargobereichs
V_leak=17*10^-3;%[m^3] Volumen der Leckageluft pro Sekunde (AFT)
rho=p_in/(T_in*R);%[kg/m^3] Dichte der bleed air
V_in=m_in/rho;
V_in2(1)=m_in/rho;%[m^3/s] Eingehender Volumenstrom(Bleed air)
V_out=0.128*V_leak;
What i am trying to do is calculate a temperature (through T_cargo2(i)...)
The difference between this value and a predefined value goes through the SIMULINK model (PID, PT1- functions and stuff happening) and produce an angle (alpha_AFT). This angle determines the Massflow m_sub through m_function.
With the massflow, the amount of "hot air" changes (V_in2), making temperature (T_cargo2) rise (or fall) faster or slower.
So the temp. is calculated (with the new massflow) and the difference should make the angle (changing the massflow once again) change and so on.
Without the Simulink part, the Loop did work. It works with a set angle (e.g. 30°) and even with an array, "opening" the angle from 0° to 60° (max) over the course of the loop.
I modified the simulink model to input -> function->set max->output to be really simple (so the error wont occure from here)

Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 14 Nov 2019

Edited:

on 14 Nov 2019

Community Treasure Hunt

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

Start Hunting!