- Create a new simulink model
- Add and configure the s-function block
- Add input signals and connect the output signals to "scope" block for display
- Run the simulation and Check whether the result is as expected or not.
Algebraric equation output from s function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I am going to solve a system of ODEs in simulink and i am going to use the state variables in an algebraric equation. My question is if it is possible to write the algebraric equation in the s function and send is as an output of the s_function block in simulink.
Right now i have stated them under function sys=mdlDerivatives which is porobably not right..
here is an example:
function [sys,x0,str,ts] = sfun_TEST(t,x,u,flag,xinit_evap,p)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(xinit_evap);
case 1,
sys=mdlDerivatives(t,x,u,p);
case 3,
sys=mdlOutputs(t,x,u);
case { 2, 4, 9 }
sys = []; % Unused flags
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes(xinit_evap)
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 7;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % At least one sample time is needed
sys = simsizes(sizes);
% initialize the initial conditions
x0 = xinit_evap; % xinit are the initial values that are sent to the S-function, see first line of the S-function
% str is always an empty matrix
str = [];
% initialize the array of sample times
ts = [0 0];
function sys=mdlDerivatives(t,x,u,p)
% Model inputs:
F1 = u(1); % Feed flowrate
F2 = u(2); % Product flowrate
F4 = u(3); % Vapor flowrate
F5 = u(4); % Condensate flowrate
X1 = u(5); % Feed composition
Q100 = u(6); % Heater duty
T1 = u(7); % Feed temperature
% Algebraric equations % UNIT
% ODEs
dxdt(1) = F1 * X1 - F2 * x(1);
dxdt(2) = F4 - F5
!!!I WANT THESE AS MY OUTPUT ALONG WITH THE STATES!!!
T2 = 0.5616 * x(2) + 0.3126 * x(1) + 48.43;
T3 = 0.507 * x(2) + 55.0;
sys = [dxdt(1) dxdt(2)];
function sys=mdlOutputs(t,x,u)
% Calculate the outputs of the model block
sys = [x(1) x(2)];
0 comentarios
Respuestas (1)
Aditya
el 22 de Dic. de 2023
Hello Andreas,
I understand that you want to output algebraic equations alongside state variables from an S-function block in Ssimulink, for this you need to update some parts of your code:
1. Update mdlOutputs to compute and include T2 and T3:
function sys = mdlOutputs(t,x,u)
T2 = 0.5616 * x(2) + 0.3126 * x(1) + 48.43;
T3 = 0.507 * x(2) + 55.0;
sys = [x(1); x(2); T2; T3];
end
2. Modify mdlInitializeSizes to indicate four outputs:
function [sys,x0,str,ts] = mdlInitializeSizes(xinit_evap)
sizes = simsizes;
sizes.NumOutputs = 4; % 2 states + 2 algebraic equations
% ... (rest of your code)
end
To test this function in simulink:
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Interactive Model Editing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!