Effacer les filtres
Effacer les filtres

m-code S-function don't works with MATLAB Function "int" (definte or indefinite integral in MATLAB code)

2 vues (au cours des 30 derniers jours)
Hi,
I need to create a s-function block in simulink, to integrate a function but not respect to time (respect to an other function). I make this code:
function msfuntmpl_basic(block)
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 2; %
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
block.InputPort(2).Dimensions = 1; %
block.InputPort(2).DatatypeID = 0; % double
block.InputPort(2).Complexity = 'Real';
block.InputPort(2).DirectFeedthrough = true;
% Override output port properties
block.OutputPort(1).Dimensions = 1; %
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
% Register parameters
block.NumDialogPrms = 0;
block.SampleTimes = [0 0]; % <<<<--------------------- SAMPLE TIME
block.RegBlockMethod('Outputs', @Outputs); % Required %
block.RegBlockMethod('Terminate', @Terminate); % Required %
function Outputs(block)
%block.OutputPort(1).Data = block.InputPort(1).Data + block.InputPort(2).Data;
block.OutputPort(1).Data = int(block.InputPort(1).Data); %integrate function from Symbolic Math Toolbox.
function Terminate(block)
But simulink does this error message: "Error evaluating registered method 'Outputs' of M-S-Function (name of m-file.m)in 'untitled/Level-2 M-file S-Function1'. Undefined function or method 'int' for input arguments of type 'double'."
I think that s-function don't call "int" function like matlab editor (from Symbolic Math Toolbox). What function can i use for this purpose ?

Réponses (3)

Kaustubha Govind
Kaustubha Govind le 15 Mar 2011
If you're trying to implement an integrator (w.r.t time) using MATLAB-file S-functions, see the demo msfcndemo_limintm.mdl. You can just type "msfcndemo_limintm" at the MATLAB prompt to open it up. Unless you are performing custom operations, you can simply use the Integrator block from the Simulink library instead.
  4 commentaires
cyberdyne
cyberdyne le 16 Mar 2011
Yes. But this time i got a help about s-functions creation and i thought to try in this way.
Kaustubha Govind
Kaustubha Govind le 16 Mar 2011
In that case, Walter's alternate suggestion for integrating step-by-step might be more appropriate.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 15 Mar 2011
int is for symbolic integration, not for numeric integration. trapz would be more appropriate, if you can somehow gather the complete input signal up.
  2 commentaires
cyberdyne
cyberdyne le 15 Mar 2011
Thanks! This is very good!
But i have to convert input signal to vector. Or not?
Walter Roberson
Walter Roberson le 15 Mar 2011
Yes, that is the more difficult part, and is why you should likely follow Kaustubha's suggestions. Either that or integrate step by step, with some kind of signal to tell it to zero the total, and each signal after that gets added to the total; this might call for one or two persistent variables.

Connectez-vous pour commenter.


Gigi Dioda
Gigi Dioda le 27 Juin 2011
Hi, all! I have a simpler issue. I want to integrate an S-function with ode45. It seemed to work. However it's not the result I was expecting. After that I took the code from the S-function and plugged into a regular dx = f(t,x,u) function and when I applied ode45 to f I got what I was looking for.
So, my question: what's wrong with ode45 and the S-function?

Community Treasure Hunt

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

Start Hunting!

Translated by