Matlab Function Block ode45 solver Event option

Hey everyone,
I have a big Problem with my simulink model.
I have include a Matlab function block into my model.
These function block should integrate the following differential equation.
function dfdt=df_dz_Validierung(tspan,y0)
T=453;
R=8.314;
rho_b=1100;
P_total=1;
Ac=(0.16^2*pi/4);
k_SR=6e6*exp(-77000/(R*T));
k_rWGS=4.8e8*exp(-108500/(R*T));
k_WGS=8e6*exp(-72000/(R*T));
dfadt=(k_rWGS*(P_total*(y0(3)/M_c)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))*...
(P_total*(y0(4)/M_d)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))-k_WGS*...
(P_total*(y0(2)/M_b)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))*...
(P_total*(y0(1)/M_a)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e)))*m_cat*M_a
dfbdt=(k_rWGS*(P_total*(y0(3)/M_c)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))*...
(P_total*(y0(4)/M_d)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))-k_WGS*...
(P_total*(y0(2)/M_b)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))*...
(P_total*(y0(1)/M_a)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))-k_SR*...
(P_total*(y0(5)/M_e)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))^0.6*...
(P_total*(y0(2)/M_b)/(y0(1)/M_a+y0(2)/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))^0.4)*m_cat*M_b
dfcdt=(k_SR*(P_total*(y0(5)/M_e)/(y0(1)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/M_b+y0(3)/M_c+y0(4)/M_d+y0(5)/M_e))^0.4)*m_cat*M_e
dfdt=[dfadt dfbdt dfcdt dfddt dfedt];
dfdt=dfdt(:)
end
It works fine if it runs out of a matlab Skript. It determines with the Event function if dfedt gets Zero. But it doesn´t work out of my simulink model. I get this error.
To use an event function with ode45, you must enable variable sizing and dynamic memory allocation.
Function 'MATLAB Function1' (#23.86.144), line 5, column 1: "[t,y,te,ye,ie] =ode45(@df_dz_Validierung,tspan,y0,options)" Launch diagnostic report.
Can anyone help me with my Problem ?

7 Comments

Up to what size your output is going to be at most?
The Maximum size of the Output depends on the inlet flow
function f = d(y0,T,P)
mcat=2e-3;
t_Verweilzeit=mcat/sum(y0);
tspan=[1e-6:1e-5:t_Verweilzeit];
options=odeset('Events',@Abbruch);
[t,y,te,ye,ie] =ode45(@df_dz_Validierung,tspan,y0,options);
f=ye(:,end);
But simulink should only get the Parameters of the last column.
The actual Task of Event function is to control the ode-solver.
y0 is a 5x1 Array,which represents the flow out of different substances
the solver calculates now the turnovers of these substances over a Retention time.
If None of these flows getting Zero Simulink should transmit the composition from the last Retention time (t_Verweilzeit). But if one of these flows crossing Zero, the Event function should stop the calculation and transmit ye to simulink.
@Birdman Do you get what i want achieve with this ? And can you help me ?
Can you share your model please?
@Birdman
Can you see the Share.zip
It´s my fitst time here and so I´m not sure if the upload works
I saw it but Thermolib is not found in my library. Can you share the library as well?
Sorry ist not a Freeware library
But you dont Need the thermolib library to solve the Problem.
I upload you another file. This should run without thermolib.

Sign in to comment.

Answers (1)

To solve this problem, do this:
Click on your MATLAB Function in your model once and while it is selected, click Ctrl+H to go to Model Explorer. There, select your model Reformer->MATLAB Function and then select your Output. On the right side, you will see Size option which is -1 by default. Change that to an upper limit which you are sure your output can not exceed it in size. For instance, 100. Then check the Variable size tickbox. Go back to your MATLAB Function page on Model Explorer and again on right side, you will see Update method already set to Inherited. Change it to Discrete and specify your sampling time accordingly. Then, click Apply at the bottom of the page and go back to your model. The problem should be solved.

Asked:

on 22 Mar 2018

Answered:

on 3 Apr 2018

Community Treasure Hunt

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

Start Hunting!