Info
This question is closed. Reopen it to edit or answer.
How to get values of all the variables written in a specific function which was used in a S-Function to get it to Simulink?
1 view (last 30 days)
Show older comments
Dear All,
I have a system of 36 ODEs which all the model have been written in a form of a Matlab Function. Something like this:
function dx=myFunction(t,p,Input);
global a1,a2,a3,....,an %these are the variables which are used in the model and I need some of their values ex. a1 and a6
a1=Input(1);
a2=Input(2);
a3=Input(3);
a4=Input(4);
a5=Input(5);
a6=Input(6);
.
.
. % there's a lot of equations here. the important note is that p and v are related by equations like:
v1=(....p(1)..*...)/(...^(...*...))
.
dx=[v1,v2,...,v36]
Then, there's a s-function written based on this. It looks like:
function [sys,x0,str,ts]= myFunction_sfcn(t,x,u,flag,z1,z2,....,z36);
.
.
switch flag
case 0 % initialize
str=[] ;
ts = [0 0] ;
.
.
s.NumContStates = 36;% NUMBER OF EQUATIONS (STATES)
s.NumDiscStates = 0 ;
s.NumOutputs = 36 ;
s.NumInputs = 6 ;%IN ASM3MODEL:[uo Qc Tref Tcat eth but hyd]
s.DirFeedthrough = 0 ;
s.NumSampleTimes = 1 ;
sys = simsizes(s) ;
x0=[z1,......z36] %initial conditions of differential equations
.
.
case 1 % derivatives
Input=u;
sys = myFunction(t,x,Input) ;
case 3 % output
sys = x;
case {2 4 9} % 2:discrete
% 4:calcTimeHit
% 9:termination
sys =[];
otherwise
error(['unhandled flag =',num2str(flag)]) ;
end
Now that we have both function and s-function, we go to simulink and put an s-function block. The name of the block will be myFunction_sfcn and there will be 36 parameters(numbers) in the parameters field as initial values. Then I've put a demux (6 streams) and a mux (36 streams) before and after the block respectively to insert the 6 inputs and get 36 output. everything is fine by running the simulink and I get the 36 outputs.
What I want, is the values of some variables which are in the main function eg. a10, a22,....,an or v1,v2,...,vn in the workspace after the simulation is completed.
I'll appreciate your help.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!