Simscape Component Block: Error using logical/relational command in the programming code
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nicole Faulhaber
le 23 Mar 2021
Déplacé(e) : Sabin
le 11 Juin 2024
Hi, I'm trying to program a Simscape Component Block. It's an adaptation of a PS Lookup Table (2D).
I would like to change the defaults for entering the parameters. My adaptation shall include, that the parameters x1 and x2 don't have to be the same length and that the parameter x2 may be an array from which a row is selected depending on the input I1.
relationship between parameter x1 und x2:
x1(1) belongs to x2(1,:)
x1(2) belongs to x2(2,:) etc.
I tried to use a logical/relational command, but an error occurs. I get the error: Error compiling Simscape network. Caused by: "Invalid use of a logical value to index a value with unit." allIdx(x1 == I1) = [1 2 3], x1 = {[4 5 6], 'bar'}, I1 = {[1x1 double],'Pa'}
Does anyone know how I can solve this problem/error or has a different approach for the code?
Here is a minimal expample of my code:
component example
inputs
I1; %x1:left
I2; %x2:left
end
outputs
O; %f: right
end
parameters
x1 = [4 5 6]; % Table grid vector
x2 = [0 9 48 61 84; 0 21 39 67 86; 0 20 46 60 85]; % Table grid matrix
v = [0 1 2; 1 2 3; 2 3 4; 3 4 5; 4 5 6]; % 2D array of table values
interp_method = simscape.enum.interpolation.linear; % Interpolation method
extrap_method = simscape.enum.extrapolation.linear; % Extrapolation method
end
parameters (Access=private)
x1_interp = [min(x1):(max(x1)-min(x1))/9:max(x1)];
end
intermediates (Access=private)
allIdx = 1:numel(x1);
idx = allIdx(x1==I1);
x2_neu = x2(idx,:);
x2_interp = [min(x2_neu):(max(x2_neu)-min(x2_neu))/9:max(x2_neu)];
v_interp = interp2(x1,x2_neu',v,x1_interp,x2_interp');
end
equations
O == tablelookup(x1_interp, x2_interp, v_interp, I1, I2, interpolation=interp_method, extrapolation=extrap_method);
end
end
0 commentaires
Réponse acceptée
Lucas Lebert
le 11 Juin 2024
Déplacé(e) : Sabin
le 11 Juin 2024
Hi Nicole,
to answer your direct question, logical indexing is not supported in this context (which is the root cause of the error you are seeing).
To give advice on possible next steps I would need more information reg. the goal of the implementation.
E.g. in the line throwing the error idx = allIdx(x1==I1); returns an empty array for all other cases than I1 is 4,5 or 6. Hence there would not be any breakpoints for the subsequent interpolation.
Possible ways forward might be using a 3d implementation using tablelookup or possibly scatteredlookup.
Thanks,
~Lucas
0 commentaires
Plus de réponses (1)
Jinal
le 16 Mai 2024
Hi Nicole,
The error message you are getting indicates that there is an issue with the indexing operation "allIdx(x1==I1)". The variables x1 and I1 have different units attached to them, which is causing the error.
You can try the following solutions:
1) Ensure that x1 and I1 have same unit before performing the indexing opeartion. You can do this using the "unitConvert" function. A sample code snippet for converting x1 from "bar" to "Pa" is as follows:
unit = symunit;
x1 = unitConvert(x1, unit.Pa);
To know more about "unitConvert" and "symunit" check out these documentation pages:
- https://www.mathworks.com/help/symbolic/unitconvert.html
- https://www.mathworks.com/help/symbolic/symunit.html
2) Convert both, x1 and I1 to "double" before performing the indexing opeartion as per the following code snippet:
I1 = double(separateUnits(I1));
x1 = double(separateUnits(x1));
You can refer to the following link to know more about "separateUnits": https://www.mathworks.com/help/symbolic/separateunits.html
0 commentaires
Voir également
Catégories
En savoir plus sur Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!