Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

671 views
Skip to first unread message

Vinz A

unread,
Apr 30, 2010, 5:52:03 PM4/30/10
to

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

Vinz A

unread,
Apr 30, 2010, 6:05:19 PM4/30/10
to
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

Michael Hosea

unread,
May 3, 2010, 10:51:26 AM5/3/10
to
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" <vin...@gmail.com> wrote in message
news:hrfk6v$reg$1...@fred.mathworks.com...

Vinz A

unread,
May 3, 2010, 1:16:12 PM5/3/10
to
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

Vinz A

unread,
May 3, 2010, 2:31:26 PM5/3/10
to
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

Michael Hosea

unread,
May 3, 2010, 3:27:33 PM5/3/10
to
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" <vin...@gmail.com> wrote in message

news:hrn0cs$n51$1...@fred.mathworks.com...

Vinz A

unread,
May 3, 2010, 5:34:06 PM5/3/10
to
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

Vinz A

unread,
May 3, 2010, 5:39:04 PM5/3/10
to
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

gouri...@gmail.com

unread,
May 8, 2014, 2:47:45 PM5/8/14
to
On Saturday, May 1, 2010 3:22:03 AM UTC+5:30, Vinz A wrote:
> Hello everyone,
> I'm facing the same problem while working with Embedded MATLAB function for a nonlinear system implmentation.
I did as Michael told in the example for quad. i'm getting an error as:
Function output 'x' cannot be of MATLAB type.

Function 'Embedded MATLAB Function' (#30.23.271), line 3, column 1:
"function [x,y,z]= functioncall_impulsiverossler(noi,x10,x20,x30)"
The code i've written is
embedded matlab function code

function [x,y,z]= functioncall_impulsiverossler(noi,x10,x20,x30)
noi=noi;
x10=x10;
x20=x20;
x30=x30;
eml.extrinsic('impulsiverosslerlike');
[x1,x2,x3]= impulsiverosslerlike(noi,x10,x20,x30);
x=x1;
y=x2;
z=x3;

and the extrinsic function created is:
function [x1,x2,x3]= impulsiverosslerlike(noi,x10,x20,x30);
[T,X]= ode45(@impulsivesynch_rosslerlike,[0 noi],[x10 x20 x30]);
x1=X(:,1);
x2=X(:,2);
x3=X(:,3);
end

function dx=impulsivesynch_rosslerlike(t,x)
dx=zeros(3,1);
dx(1)=-x(1)-4*x(2);
dx(2)=x(1)+(x(3)^2);
dx(3)=1+x(1);
end


I'm not getting what went wrong? can u please please find the solution?


Gouri.


0 new messages