how make an iteration of a function in simulink?

5 views (last 30 days)
Hi,
I would like to create a model in Simulink that would be an equivalent of the following code in Matlab:
T1(1)=400;
T2(1)=80;
Tp2=H2O_Sattdampfkurve_pvonT(T2(1));
p2(1)=Tp2(1,2);
h1(1)=1;
h2(1)=2;
delta_T(1)=T1(1)-T2(1);
%M1=400;
%M2=1000;
M1=200;
M2(1)=300;
A=700;
k=1000;
Q(1)=k*A*delta_T(1);
N=10;
delta_t=0.0000001;
t(1)=1;
%spezifische Kapazität für Wasser
%cp1=1.5*2.778/10000;
%cp2=0.0005;
cp1=1.5*2.778/1000;
%cp1=1.5*2.778/10000;
%cp2=H2O_IAPWS_cp([T2(1), p2(1)]);
cTp=H2O_IAPWS_cp([T2(1), p2(1)]);
cp2(1)=cTp(1,1);
%spezifische Kapazität
for j=1:3
for i=1:7
if Q<1
break;
end
h1(i+1)=h1(i)-(delta_t*k*A*delta_T(i))/M1;
T1(i+1)=T1(i)-(delta_t*k*A*delta_T(i))/(M1*cp1)
h2(i+1)=h2(i)+(delta_t*k*A*delta_T(i))/M2(j);
T2(i+1)=T2(i)+(delta_t*k*A*delta_T(i))/(M2(j)*cp2(i));
Q(i+1)=delta_t*M1*(h1(i)-h1(i+1));
Tp2=H2O_Sattdampfkurve_pvonT(T2(i+1));
p2(i+1)=Tp2(1,2);
cTp=H2O_IAPWS_cp([T2(1), p2(1)]);
cp2(i+1)=cTp(1,1);
if delta_T<40
break;
end
delta_T(i+1)=T1(i)-T2(i);
t(i+1)=t(i)+0.0000001;
end
M2(j+1)=M2(j)+5;
figure(1)
plot(t, T1, 'r')
hold on
plot(t, T2)
xlabel('t[s]');
ylabel('T[°C]');
figure(2)
plot(Q, T1, 'r')
xlabel('Q');
ylabel('T[°C]');
hold on
plot(-Q, T2)
end
Basicaly, I want to iterate two variables T1 and T2. I want to count the the next step value of both variables T1 and T2:
T1(i+1)=T1(i)-(delta_t*k*A*delta_T(i))/(M1*cp1)
T2(i+1)=T2(i)+(delta_t*k*A*delta_T(i))/(M2(j)*cp2(i));
Both variables are dependent on the delta_T(i) which is a difference between T1 and T2:
delta_T(i+1)=T1(i)-T2(i);
So the values T1(i+1) and T2(i+1) in the next time step depend on the difference between them in the prevoius time step. Which is quite tricky. I tried the For Iterate Block, but it doesn't work. However if I don't take into consideration the changing value of delta_T and I set it as constant it works correctly. The error is:
Simulink cannot solve the algebraic loop containing 'model2/Switch' at time 0.0 using the TrustRegion-based algorithm due to one of the following reasons: the model is ill-defined i.e., the system equations do not have a solution; or the nonlinear equation solver failed to converge due to numerical issues. To rule out solver convergence as the cause of this error, either a) switch to LineSearch-based algorithm using set_param('model2','AlgebraicLoopSolver','LineSearch') b) reducing the fixed step size. If the error persists in spite of the above changes, then the model is likely ill-defined and requires modification.

Answers (0)

Categories

Find more on Programmatic Model Editing 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!