Azzera filtri
Azzera filtri

Coder.Extrinsic Syntax Issues with TF property

8 visualizzazioni (ultimi 30 giorni)
abhijit chakraborty
abhijit chakraborty il 11 Feb 2022
Risposto: Jaynik il 11 Gen 2024
Hello,
I am gettign the following error while using matlab function block in simulink.
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
to the following code snippet:
%#codegen
coder.extrinsic('tf');
P = 1; I= 1; dtsys = 0.1
z = tf('z',dtsys);
iterm = tf()
Iterm = tf(1,[1 -1],dtsys)
K = P + I*dtsys*Iterm;
I did find some related questions in the forum but they are handled by pre-assigining the value with the class, such as double(0) etc. In this case, how do I assign the tf class.

Risposte (1)

Jaynik
Jaynik il 11 Gen 2024
Hi Abhijit,
It seems that the error you're encountering is because the "tf" function does not support code generation. The function produces a "mxArray", which is not a codegen-compatible type.
To resolve this issue, you'll need to avoid using "tf" and other functions that are not supported for code generation within the "MATLAB Function" block. Instead, you should define your transfer function using a discrete-time representation that is compatible with code generation, such as using state-space, zero-pole-gain models, etc.
If your application just needs the numerator and the denominator values of the variable "K", you can check the following code:
% Define your constants
P = 1;
I = 1;
dtsys = 0.1;
num_Iterm = I * dtsys; % Numerator of Iterm (multiplied by z^0)
den_Iterm = [1, -1]; % Denominator of Iterm (1 - z^-1)
num_P = P; % Numerator of Pterm (P multiplied by z^0)
den_P = 1; % Denominator of Pterm (1)
% Multiply the proportional term by the denominator of the integral term
num_P_aligned = num_P * den_Iterm; % P * (1 - z^-1)
% Now we can add the numerators directly
num_combined = num_P_aligned + [0, num_Iterm]; % Adding zero to align with z^-1 term
% The denominator remains the same as the denominator of the integral term
den_combined = den_Iterm;
You can use these coefficients to implement the transfer function using a "Discrete Transfer Function" block or a "Discrete State-Space" block in Simulink, which are both compatible with code generation.
You can check the following documentation to learn more about each block:
Hope this helps!

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by