Embedded matlab function in simulink for mutiple inputs and outputs using ode15s

  • Follow


Hello everyone,

I am trying to represent a non linear system in simulink.  I'm using embedded matlab function. I have defined a vector dx in terms of combinations of states and inputs, and plan to use ode15s to solve thisfunction dx = f(L,x)

%define dx as a vector 
   dx=zeros(4,1);
   dx(1)=x(3);
   dx(2)=x(4);
   dx(3)=x(1)-2*x(4)-0.9*(x(1)+0.1)/-0.1*(x(1)-0.9);
   dx(4)=x(2)-2*x(3)-0.9*x(2)/-0.1*x(2);

Then type:

[L,x]=ode15s('platefincondenser',ht,x0)
(%x0 and ht is defined.)
in the command window, then it appears to work.

My problem has been in implementing this in simulink. I even addded the integrator block, but where should I mention about the ode15s in simulink, with 4 intial conditions and 4 outputs.

error:
Attempted to access local or constant data 'ode15s' (#140). This is not supported.  Please remove this data from the model explorer.
Function 'Embedded MATLAB Function' (#124.484.523), line 24, column 1:
"[L,x]= ode15s('platefincondenser',L,x0)"
Launch diagnostic report.

Any kind of help is appreciated.
Thank you in advance for your time and consideration.

cheers
VJ
0
Reply Vinz 4/30/2010 9:52:03 PM

Iam getting an error after I introduced extrinsic
Embedded MATLAB Interface Error: Call to MATLAB function aborted: Error using ==> feval
Undefined function or method 'platefincondenser' for input arguments of type 'double'.
Block Embedded MATLAB Function (#125)
While executing: none

My code is:
function serratedfins

%#eml
global Qexp Two 

Pt=599240;           %N/m^2
y=0;
QT=0.0;                 %W/m
%fl=0.00000001;          %kg/s
Qexp=975900;             %W
ifgnh3=1236*10^3;        %J/kg
fg=Qexp/ifgnh3;               % kg/s
fl=0.001;                %kg/s
Two=280.17;                 % K

maxL  = 2.344;            %m

x0 = [Pt y QT fl fg Two];
L = [0 maxL];
%options=odeset('initialslope',2.4,'maxstep',1);
%options=odeset('initialslope',2.4,'maxstep',0.004);
eml.extrinsic('ode15s');
[L,x]= ode15s('platefincondenser',L,x0);
 
function dx=platefincondenser(L,x,x0)
global Qexp Two 
  % iam using fzero too for calculations of parameters with Qexp , Two etc.
 dx=zeros(4,1);
   dx(1)=x(3);
   dx(2)=x(4);
   dx(3)=x(1)-2*x(4)-0.9*(x(1)+0.1)/-0.1*(x(1)-0.9);
   dx(4)=x(2)-2*x(3)-0.9*x(2)/-0.1*x(2);

Thank you.

Cheers
Vinz
0
Reply Vinz 4/30/2010 10:05:19 PM


You need to consider that the MATLAB and Embedded MATLAB execution 
environments are separate, and the one only knows about the other insofar as 
the information is passed as an argument.  Passing a string representing a 
function in the Embedded MATLAB execution context just passes a string.  As 
far as the MATLAB execution context knows, that function doesn't exist. 
Furthermore, one cannot send functions as arguments from one execution 
context to the other by any means.  So what you need to do is write a MATLAB 
function that will not be compiled with Embedded MATLAB.  This function 
should accept all the necessary data required to use ode15s.  Define the 
problem (derivative, limits, etc.) and call ode15s from within this 
function.  Next declare *that* function extrinsic in the Embedded MATLAB 
block and call it rather than ode15s.
--
Mike

"Vinz A" <vinzav@gmail.com> wrote in message 
news:hrfk6v$reg$1@fred.mathworks.com...
> Iam getting an error after I introduced extrinsic
> Embedded MATLAB Interface Error: Call to MATLAB function aborted: Error 
> using ==> feval
> Undefined function or method 'platefincondenser' for input arguments of 
> type 'double'.
> Block Embedded MATLAB Function (#125)
> While executing: none
>
> My code is:
> function serratedfins
>
> %#eml
> global Qexp Two
> Pt=599240;           %N/m^2
> y=0;
> QT=0.0;                 %W/m
> %fl=0.00000001;          %kg/s
> Qexp=975900;             %W
> ifgnh3=1236*10^3;        %J/kg
> fg=Qexp/ifgnh3;               % kg/s
> fl=0.001;                %kg/s
> Two=280.17;                 % K
>
> maxL  = 2.344;            %m
>
> x0 = [Pt y QT fl fg Two];
> L = [0 maxL];
> %options=odeset('initialslope',2.4,'maxstep',1);
> %options=odeset('initialslope',2.4,'maxstep',0.004);
> eml.extrinsic('ode15s');
> [L,x]= ode15s('platefincondenser',L,x0);
>
> function dx=platefincondenser(L,x,x0)
> global Qexp Two % iam using fzero too for calculations of parameters with 
> Qexp , Two etc.
> dx=zeros(4,1);
>   dx(1)=x(3);
>   dx(2)=x(4);
>   dx(3)=x(1)-2*x(4)-0.9*(x(1)+0.1)/-0.1*(x(1)-0.9);
>   dx(4)=x(2)-2*x(3)-0.9*x(2)/-0.1*x(2);
>
> Thank you.
>
> Cheers
> Vinz
> 


0
Reply Michael 5/3/2010 2:51:26 PM

Hi Michael Hosea:

Thank you for your response for Embedded Matlab functions.
As you said, I tried writing everthing as a single function, but still I am getting an error.
Could you please guide me for the below  simple example,

function dx = newcheckfcn
%#eml
eml.extrinsic('newcheckfcn','ode15s');
a=5;
b=25;
L = [0 2];
dx= ode15s('newcheckfcn',L,a,b);
 dx = zeros(2,1); 
dx(1)=a+b;
dx(2)=a/b;

Cheers
vinz
0
Reply Vinz 5/3/2010 5:16:12 PM

Hi,

Can anyone please help me with the embedded matlab functions.
Is there any particular way to define ode15s in Simulink.
Below is a simple example, that I will be using

function [x,y] = inputsnum
%#eml
eml.extrinsic('ode15s');
a=5;
b=25;
L = [0 2];
[x,y]= ode15s('newcheckfcn',L,a,b);
function [x,y] = newcheckfcn(L,a,b)
x=a+b;
y=a/b;

Thank you 
Vinz
0
Reply Vinz 5/3/2010 6:31:26 PM

No, that's not what I suggested, not even close.  You have to move the 
ode15s call outside Embedded MATLAB.  You have a function that only runs in 
MATLAB and will not compile in Embedded MATLAB.  Now, if you *only* pass 
data to such a function, you can declare it extrinsic and call it from 
Embedded MATLAB (see the doc for details).  However, if you must pass a 
function name or handle to the this function, as is the case here, you 
simply cannot compile the function that *calls* this function.  So don't 
try.  Suppose, in some larger Embedded MATLAB function, I need to integrate 
a probability density function MYPDF, depending on two parameters, mu and 
sigma, from x=a to x=b.  In MATLAB that might look like this, all in one 
file, say foo.m:

%----------------------------------------
function p = foo(level,a,b,mu,sigma)
y = quad(@(x)mypdf(x,mu,sigma),a,b);
p = y > level;

function y = mypdf(x,mu,sigma)
x = x - mu;
t = -(x.*x)./(2*sigma*sigma);
y = exp(t)./(sqrt(2*pi)*sigma);
%----------------------------------------

Now, because QUAD isn't supported by Embedded MATLAB and requires either a 
function name or handle or some such, you simply cannot compile this 
function for Embedded MATLAB.  Declaring QUAD extrinsic doesn't work because 
you can't pass a function or a function name to an extrinsic function. 
Instead, create your own extrinsic function

%----------------------------------------
function y = mycdf(a,b,mu,sigma)
y = quad(@(x)mypdf(x,mu,sigma),a,b);

function y = mypdf(x,mu,sigma)
x = x - mu;
t = -(x.*x)./(2*sigma*sigma);
y = exp(t)./(sqrt(2*pi)*sigma);
%----------------------------------------

and put it on the MATLAB path.  Notice there is NO %#eml for this file.  Do 
not try to compile it.  Do not pass its name on the command line to emlc or 
emlmex.  Instead, just write your Embedded MATLAB driver

function p = foo(level,a,b,mu,sigma) %#eml
eml.extrinsic('mycdf');
y = 0;
y = mycdf(a,b,mu,sigma);
p = y > level;

Now *this* function can be compiled.  It doesn't matter how much *data* you 
need to pass to MATLAB in order for MYCDF to do its work.  What's important 
here is that MYCDF is extrinsic (not compiled), and we do not need to pass 
function handles or names from Embedded MATLAB to MATLAB.

I just used QUAD as an example of a function that takes a function handle as 
input.  I should note for completeness here that Embedded MATLAB supports 
QUADGK already.  Unfortunately, Embedded MATLAB does not support any ODE 
integrators right now.
--
Mike


"Vinz A" <vinzav@gmail.com> wrote in message 
news:hrn0cs$n51$1@fred.mathworks.com...
> Hi Michael Hosea:
>
> Thank you for your response for Embedded Matlab functions.
> As you said, I tried writing everthing as a single function, but still I 
> am getting an error.
> Could you please guide me for the below  simple example,
>
> function dx = newcheckfcn
> %#eml
> eml.extrinsic('newcheckfcn','ode15s');
> a=5;
> b=25;
> L = [0 2];
> dx= ode15s('newcheckfcn',L,a,b);
> dx = zeros(2,1); dx(1)=a+b;
> dx(2)=a/b;
>
> Cheers
> vinz
> 


0
Reply Michael 5/3/2010 7:27:33 PM

Hi Michael Hosea:

Thank you very much for the detailed explaination.
I tried solving a  simple example and it worked.

I have one more question, how to define the size of the variable.
For example: 
If I have only one differential equation I will mention as
x=zeros(1,1);
but it is asking to reframe the size of the variable to 
x=zeros(11,1);
I am working with steady state, not varying with time, in that case how can I obtain the value of the last value after the differentation is done.

Hope I am clear.
Thank you for your time and consideration.

Regards

Vinz
0
Reply Vinz 5/3/2010 9:34:06 PM

Hi Michael Hosea:

Thank you very much for the detailed explaination.
I tried solving a  simple example and it worked.

I have one more question, how to define the size of the variable.
For example: 
If I have only one differential equation I will mention as
x=zeros(1,1);
but it is asking to reframe the size of the variable to 
x=zeros(11,1);
I am working with steady state, not varying with time, in that case how can I obtain the value of the last value after the differentation is done.

Hope I am clear.
Thank you for your time and consideration.

Regards

Vinz
0
Reply Vinz 5/3/2010 9:39:04 PM

7 Replies
462 Views

(page loaded in 0.077 seconds)

Similiar Articles:






7/21/2012 6:28:06 AM


Reply: