Definite integral in (embedded) Matlab function, with passing additional parameters to the integrand

5 views (last 30 days)
Ehsan Asadi
Ehsan Asadi on 18 Dec 2014
Commented: Mike Hosea on 19 Dec 2014
Hi,
I have a simulation file to model the dynamic behaviour of system, at each time step , I need to evaluate a time-independent integral. To do so I use (embedded) Matlab function but I got many errors. Any help would be appreciated.
inside my (embedded) Matlab function is:
%%________________ method one
function forces=my_embeded_func(positions)
y1=position(1);
y2=position(2);
forces=integral(@(theta),y1+y2*theta,0,2*pi)
end
%_______________
Notes: 1-my integrand is actually very complicated but I put something simple here.
2-I have been told to use "quadgk" instead of "inregra;", but it did not worked
3- I also tried the following method but it did not helped!
%%________________ method two
function forces=my_embeded_func(positions)
coder.extrinsic('myFunc')
y1=position(1);
y2=position(2);
forces=myFunc(y1,y2)
end
% where
function a=myFunc(y1,y2)
a=quadgk(@(theta),y1+y2*theta,0,2*pi)
end
%_______________
Thanks,
Ehsan

Accepted Answer

Mike Hosea
Mike Hosea on 18 Dec 2014
Edited: Mike Hosea on 18 Dec 2014
If you don't need to generate code for your model, I think you can use your second approach, but you need to fix a few unrelated errors. Put myFunc into a MATLAB file myFunc.m. There is a syntax error. Remove the comma between @(theta) and y1:
function a=myFunc(y1,y2)
a=quadgk(@(theta)y1+y2*theta,0,2*pi)
end
Now let's consider your block code. I didn't know whether you meant for your variable to be called "positions" or "position". I just picked one. Second, to call an extrinsic function, you have to pre-allocate the output to tell the compiler how to interpret the mxArray that comes back from MATLAB. The block code should look something like this:
function forces=my_embeded_func(position)
coder.extrinsic('myFunc')
y1=position(1);
y2=position(2);
% Define the output type for myFunc, in this case a scalar double.
forces=0;
% Evaluate myFunc in MATLAB.
forces=myFunc(y1,y2)
end
If you do need to generate code, the way I tell people to do this is to use persistent variables to store the parameters of the function. The whole thing could go into the block.
function forces=my_embeded_func(position)
y1=position(1);
y2=position(2);
forces=myFunc(y1,y2);
end
function a=myFunc(y1,y2)
integrand('set positions',y1,y2);
a=quadgk(@integrand,0,2*pi);
end
function v = integrand(theta,y1set,y2set)
persistent y1 y2
if isempty(y1)
y1 = 0;
y2 = 0;
end
% More than one input means we are setting the persistent
% parameter values, not evaluating the integrand function
% on this particular call. The theta argument is ignored.
% We make it a string 'set positions' in the calling code
% solely for readability.
if nargin > 1
y1 = y1set;
y2 = y2set;
% nargout had better be zero, since we haven't set v.
return
end
v = y1 + y2*theta;
end
  2 Comments
Mike Hosea
Mike Hosea on 19 Dec 2014
It could go either way depending on the integrand, but my guess would be the persistent approach rather than the extrinsic.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!