From: Shakeel on
I have one embedded matlab function in simulink(my.mdl)file which has the following lines of code
for j = 1:nInt
x = RK4(@FRBwRIG, x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG, nBRIG, betaRIG);
x(1:4) = QUnit(x(1:4));
t = t + dT/nInt;

end
the first argument to RK4 is 'FRBwRIG',which is another function defined in my.mdl. all function that are being called in my.mdl are defined inside the my.mdl code file i.e and
the matlab code of the rk4 function is as follows. I need to make this RK4 fucntion embedded matlab compliant.

Fourth order Runge-Kutta. Called function is of the form:
%
% Fun(x,{t,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10})
%
% Accepts up to 10 optional arguments that are passed through to Fun
%
%-------------------------------------------------------------------------------
% Form:
% x = RK4( Fun, x, h, t, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 )
%-------------------------------------------------------------------------------
%
% ------
% Inputs
% ------
% Fun Function Fun(x,{t,...})
% x State (column vector)
% h Independent variable step
% t Current time
% p1...10 Optional arguments
%
% -------
% Outputs
% -------
% x Updated state
%
%-------------------------------------------------------------------------------

%-------------------------------------------------------------------------------
% Copyright 1993-1994 Princeton Satellite Systems, Inc.
% All rights reserved.
%-------------------------------------------------------------------------------

if( nargin < 4 )
t = [];
end
ho2 = 0.5*h;

if nargin > 3 & ~isempty(t)
arg1 = '(x,t';
arg2 = '(x + ho2*k1,t+ho2';
arg3 = '(x + ho2*k2,t+ho2';
arg4 = '(x + h *k3,t+h';
else
arg1 = '(x';
arg2 = '(x + ho2*k1';
arg3 = '(x + ho2*k2';
arg4 = '(x + h *k3';
end

args = [];
for k = 1:nargin - 4
args = [args,',p',int2str(k)];
end

k1 = eval([Fun arg1 args ')']);
k2 = eval([Fun arg2 args ')']);
k3 = eval([Fun arg3 args ')']);
k4 = eval([Fun arg4 args ')']);

x = x + h*(k1 + 2*(k2+k3) + k4)/6;
%end of RK4 function code
*****************************************************************
the above code has string catenation error as well eval function error I declared eval extrinsic but then the following error message is
"non-scalars of class 'function_handle is unsupported", I am using MATLAB 2007.

If anyone can convert this RK4 function compliant to embedded matlab then I shall be grateful.
From: Michael Hosea on
Well, this is pretty old MATLAB code and not written in a good style.

For starters, don't cobble together strings and call "eval". Function
handles work. Just use them directly--no strings. Use varargin to handle
the extra parameters. You'll probably have better luck writing something
from scratch. For some examples of using function handles for things of
this nature, see

http://www.mathworks.com/moler/ncm/ode23tx.m

There are things in there that are not Embedded MATLAB compliant, and it is
a variable-step size implementation, which makes it a lot more complicated
than RK4, but especially observe how the stages s1, s2, and s3 are computed,
e.g.

s1 = F(t, y, varargin{:});
....
s2 = F(t+h/2, y+h/2*s1, varargin{:});
s3 = F(t+3*h/4, y+3*h/4*s2, varargin{:});
tnew = t + h;
ynew = y + h*(2*s1 + 3*s2 + 4*s3)/9;


Your code should look something like that, except that it should embody the
4th order Runge-Kutta method instead of the Bogacki-Shampine pair.
--
Mike


"Shakeel" <shakeelj2k(a)yahoo.com> wrote in message
news:hvs0d6$dvl$1(a)fred.mathworks.com...
>I have one embedded matlab function in simulink(my.mdl)file which has the
>following lines of code for j = 1:nInt
> x = RK4(@FRBwRIG, x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG,
> nBRIG, betaRIG);
> x(1:4) = QUnit(x(1:4));
> t = t + dT/nInt;
>
> end
> the first argument to RK4 is 'FRBwRIG',which is another function defined
> in my.mdl. all function that are being called in my.mdl are defined inside
> the my.mdl code file i.e and
> the matlab code of the rk4 function is as follows. I need to make this RK4
> fucntion embedded matlab compliant.
>
> Fourth order Runge-Kutta. Called function is of the form:
> %
> % Fun(x,{t,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10})
> %
> % Accepts up to 10 optional arguments that are passed through to Fun
> %
> %-------------------------------------------------------------------------------
> % Form:
> % x = RK4( Fun, x, h, t, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 )
> %-------------------------------------------------------------------------------
> %
> % ------
> % Inputs
> % ------
> % Fun Function Fun(x,{t,...})
> % x State (column vector)
> % h Independent variable step
> % t Current time
> % p1...10 Optional arguments
> %
> % -------
> % Outputs
> % -------
> % x Updated state
> %
> %-------------------------------------------------------------------------------
>
> %-------------------------------------------------------------------------------
> % Copyright 1993-1994 Princeton Satellite Systems, Inc.
> % All rights reserved.
> %-------------------------------------------------------------------------------
>
> if( nargin < 4 )
> t = [];
> end
> ho2 = 0.5*h;
>
> if nargin > 3 & ~isempty(t)
> arg1 = '(x,t';
> arg2 = '(x + ho2*k1,t+ho2';
> arg3 = '(x + ho2*k2,t+ho2';
> arg4 = '(x + h *k3,t+h';
> else
> arg1 = '(x';
> arg2 = '(x + ho2*k1';
> arg3 = '(x + ho2*k2';
> arg4 = '(x + h *k3';
> end
>
> args = [];
> for k = 1:nargin - 4
> args = [args,',p',int2str(k)];
> end
>
> k1 = eval([Fun arg1 args ')']); k2 = eval([Fun arg2 args ')']); k3 =
> eval([Fun arg3 args ')']); k4 = eval([Fun arg4 args ')']);
> x = x + h*(k1 + 2*(k2+k3) + k4)/6;
> %end of RK4 function code
> *****************************************************************
> the above code has string catenation error as well eval function error I
> declared eval extrinsic but then the following error message is
> "non-scalars of class 'function_handle is unsupported", I am using MATLAB
> 2007.
>
> If anyone can convert this RK4 function compliant to embedded matlab then
> I shall be grateful.
>


From: Shakeel on
"Michael Hosea" <Michael.Hosea(a)mathworks.com> wrote in message <hvtsa7$3q4$1(a)fred.mathworks.com>...
> Well, this is pretty old MATLAB code and not written in a good style.
>
> For starters, don't cobble together strings and call "eval". Function
> handles work. Just use them directly--no strings. Use varargin to handle
> the extra parameters. You'll probably have better luck writing something
> from scratch. For some examples of using function handles for things of
> this nature, see
>
> http://www.mathworks.com/moler/ncm/ode23tx.m
>
> There are things in there that are not Embedded MATLAB compliant, and it is
> a variable-step size implementation, which makes it a lot more complicated
> than RK4, but especially observe how the stages s1, s2, and s3 are computed,
> e.g.
>
> s1 = F(t, y, varargin{:});
> ...
> s2 = F(t+h/2, y+h/2*s1, varargin{:});
> s3 = F(t+3*h/4, y+3*h/4*s2, varargin{:});
> tnew = t + h;
> ynew = y + h*(2*s1 + 3*s2 + 4*s3)/9;
>
>
> Your code should look something like that, except that it should embody the
> 4th order Runge-Kutta method instead of the Bogacki-Shampine pair.
> --
> Mike
>
>
thanks mike for your help.please guide further

let suppose I further simplify my RK4 file to make at least compliant so that I can get code compile in simulink for matlab 2007 for my DSP board.
my.mdl is as now has the line below in the first argument ' ' instead of @.
x = RK4('FRBwRIG', x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG, nBRIG, betaRIG);

I have changed RK4.m is as follows

k1=zeros(13,1);
k2=zeros(13,1);
k3=zeros(13,1);
k4=zeros(13,1);
k1 = feval([FRBwRIG(x,t,p1,p2,p3,p4,p5,p6,p7)]);
k2 = feval([FRBwRIG(x + ho2*k1,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
k3 = feval([FRBwRIG(x + ho2*k2,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
k4 = eval([FRBwRIG(x + ho2*k3,t+h,p1,p2,p3,p4,p5,p6,p7)]);
x = x + h*(k1 + 2*(k2+k3) + k4)/6;
%end of RK4 function code

I also used eval instead of feval and make it eml.extrinsic('eval')
but still I am getting the following error
assertion error: state flow error: ------------------>differ_ones_only(message)
I shall be grateful for your help
thanks and best regards
From: Michael Hosea on
Well, if FRBwRIG is defined in MATLAB and not in the Embedded MATLAB block,
I might write it something like:

function [x,t] = RK4step(dfun,x,h,t,varargin)
ho2 = h/2;
k1 = dfun(x,t,varargin{:});
k2 = dfun(x + ho2*k1,t+ho2,varargin{:});
k3 = dfun(x + ho2*k2,t+ho2,varargin{:});
k4 = dfun(x + ho2*k3,t+h,varargin{:});
x = x + h*(k1 + 2*(k2+k3) + k4)/6;
t = t + h;

Then, assuming the function FRBwRIG is only defined in MATLAB (i.e.,
assuming you can't just inline its definition in Embedded MATLAB compliant
code in the definition of dfun), I would define (in the same Embedded
MATLAB block):

function dy = dfun(t,y,varargin)
eml.extrinsic('FRBwRIG');
dy = zeros(size(y));
dy = FRBwRIG(t,y,varargin{:});

The call site at the top level in the Embedded MATLAB block would look
something like

[x,t] = RK4step(@dfun, x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG,
nBRIG, betaRIG);

--
Mike

"Shakeel" <shakeelj2k(a)yahoo.com> wrote in message
news:hvvfkp$hcf$1(a)fred.mathworks.com...
> "Michael Hosea" <Michael.Hosea(a)mathworks.com> wrote in message
> <hvtsa7$3q4$1(a)fred.mathworks.com>...
>> Well, this is pretty old MATLAB code and not written in a good style.
>>
>> For starters, don't cobble together strings and call "eval". Function
>> handles work. Just use them directly--no strings. Use varargin to
>> handle the extra parameters. You'll probably have better luck writing
>> something from scratch. For some examples of using function handles for
>> things of this nature, see
>>
>> http://www.mathworks.com/moler/ncm/ode23tx.m
>>
>> There are things in there that are not Embedded MATLAB compliant, and it
>> is a variable-step size implementation, which makes it a lot more
>> complicated than RK4, but especially observe how the stages s1, s2, and
>> s3 are computed, e.g.
>>
>> s1 = F(t, y, varargin{:});
>> ...
>> s2 = F(t+h/2, y+h/2*s1, varargin{:});
>> s3 = F(t+3*h/4, y+3*h/4*s2, varargin{:});
>> tnew = t + h;
>> ynew = y + h*(2*s1 + 3*s2 + 4*s3)/9;
>>
>>
>> Your code should look something like that, except that it should embody
>> the 4th order Runge-Kutta method instead of the Bogacki-Shampine pair.
>> --
>> Mike
>>
>>
> thanks mike for your help.please guide further
>
> let suppose I further simplify my RK4 file to make at least compliant so
> that I can get code compile in simulink for matlab 2007 for my DSP board.
> my.mdl is as now has the line below in the first argument ' ' instead of
> @.
> x = RK4('FRBwRIG', x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG,
> nBRIG, betaRIG);
>
> I have changed RK4.m is as follows
>
> k1=zeros(13,1);
> k2=zeros(13,1);
> k3=zeros(13,1);
> k4=zeros(13,1);
> k1 = feval([FRBwRIG(x,t,p1,p2,p3,p4,p5,p6,p7)]); k2 = feval([FRBwRIG(x +
> ho2*k1,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
> k3 = feval([FRBwRIG(x + ho2*k2,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
> k4 = eval([FRBwRIG(x + ho2*k3,t+h,p1,p2,p3,p4,p5,p6,p7)]);
> x = x + h*(k1 + 2*(k2+k3) + k4)/6;
> %end of RK4 function code
> I also used eval instead of feval and make it eml.extrinsic('eval')
> but still I am getting the following error
> assertion error: state flow
> error: ------------------>differ_ones_only(message)
> I shall be grateful for your help
> thanks and best regards


From: Shakeel on
"Michael Hosea" <Michael.Hosea(a)mathworks.com> wrote in message <i00156$1ua$1(a)fred.mathworks.com>...
> Well, if FRBwRIG is defined in MATLAB and not in the Embedded MATLAB block,
> I might write it something like:
>
> function [x,t] = RK4step(dfun,x,h,t,varargin)
> ho2 = h/2;
> k1 = dfun(x,t,varargin{:});
> k2 = dfun(x + ho2*k1,t+ho2,varargin{:});
> k3 = dfun(x + ho2*k2,t+ho2,varargin{:});
> k4 = dfun(x + ho2*k3,t+h,varargin{:});
> x = x + h*(k1 + 2*(k2+k3) + k4)/6;
> t = t + h;
>
> Then, assuming the function FRBwRIG is only defined in MATLAB (i.e.,
> assuming you can't just inline its definition in Embedded MATLAB compliant
> code in the definition of dfun), I would define (in the same Embedded
> MATLAB block):
>
> function dy = dfun(t,y,varargin)
> eml.extrinsic('FRBwRIG');
> dy = zeros(size(y));
> dy = FRBwRIG(t,y,varargin{:});
>
> The call site at the top level in the Embedded MATLAB block would look
> something like
>
> [x,t] = RK4step(@dfun, x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG,
> nBRIG, betaRIG);
>
> --
> Mike
>
> "Shakeel" <shakeelj2k(a)yahoo.com> wrote in message
> news:hvvfkp$hcf$1(a)fred.mathworks.com...
> > "Michael Hosea" <Michael.Hosea(a)mathworks.com> wrote in message
> > <hvtsa7$3q4$1(a)fred.mathworks.com>...
> >> Well, this is pretty old MATLAB code and not written in a good style.
> >>
> >> For starters, don't cobble together strings and call "eval". Function
> >> handles work. Just use them directly--no strings. Use varargin to
> >> handle the extra parameters. You'll probably have better luck writing
> >> something from scratch. For some examples of using function handles for
> >> things of this nature, see
> >>
> >> http://www.mathworks.com/moler/ncm/ode23tx.m
> >>
> >> There are things in there that are not Embedded MATLAB compliant, and it
> >> is a variable-step size implementation, which makes it a lot more
> >> complicated than RK4, but especially observe how the stages s1, s2, and
> >> s3 are computed, e.g.
> >>
> >> s1 = F(t, y, varargin{:});
> >> ...
> >> s2 = F(t+h/2, y+h/2*s1, varargin{:});
> >> s3 = F(t+3*h/4, y+3*h/4*s2, varargin{:});
> >> tnew = t + h;
> >> ynew = y + h*(2*s1 + 3*s2 + 4*s3)/9;
> >>
> >>
> >> Your code should look something like that, except that it should embody
> >> the 4th order Runge-Kutta method instead of the Bogacki-Shampine pair.
> >> --
> >> Mike
> >>
> >>
> > thanks mike for your help.please guide further
> >
> > let suppose I further simplify my RK4 file to make at least compliant so
> > that I can get code compile in simulink for matlab 2007 for my DSP board.
> > my.mdl is as now has the line below in the first argument ' ' instead of
> > @.
> > x = RK4('FRBwRIG', x, dT/nInt, t,inr, invInr, torque, uRIG, nRWRIG,
> > nBRIG, betaRIG);
> >
> > I have changed RK4.m is as follows
> >
> > k1=zeros(13,1);
> > k2=zeros(13,1);
> > k3=zeros(13,1);
> > k4=zeros(13,1);
> > k1 = feval([FRBwRIG(x,t,p1,p2,p3,p4,p5,p6,p7)]); k2 = feval([FRBwRIG(x +
> > ho2*k1,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
> > k3 = feval([FRBwRIG(x + ho2*k2,t+ho2,p1,p2,p3,p4,p5,p6,p7)]);
> > k4 = eval([FRBwRIG(x + ho2*k3,t+h,p1,p2,p3,p4,p5,p6,p7)]);
> > x = x + h*(k1 + 2*(k2+k3) + k4)/6;
> > %end of RK4 function code
> > I also used eval instead of feval and make it eml.extrinsic('eval')
> > but still I am getting the following error
> > assertion error: state flow
> > error: ------------------>differ_ones_only(message)
> > I shall be grateful for your help
> > thanks and best regards
>
Hi Mike

Thanks a lot for your help

I have done what you suggested in above ,all the code is now compiling but the now simulation stopped due to array read bound error.My code uses myfind function as you know embedded matlab does not support find function that is why i have used the following in-line myfind function
%%%%%myfind function%%%%%%
function [idx,n] = myfind(b)
% FIND-like function.
% Input B is a logical array.
% Output IDX is a column vector with numel(b) elements.
% Output N is the number of elements of IDX in use.
n = double(0);
idx = zeros(numel(b),1,'double');
for k = 1:numel(b)
if b(k)
n = n + 1;
idx(n) = k;
end
end
%%%%%%end%%%%%%%%%%%%%%%
this function causes array read bound error i.e I am running simulation for 4800 times and during the running of the simulation a variable(is actually structure) named sensorData.valid=[ 1 1] is accessing 0 index ,that is why I am getting array bound read error.

will you help to correct this myfind function.If I use matlab find function then there is problem of mxarray indexing is not allowed,that is why I have used myfind function

any help will be appreciated
Thanks in Advance again