Reverse 2D-lookup table in Simulink

조회 수: 16 (최근 30일)
Bruce
Bruce 2025년 4월 12일
편집: VBBV 2025년 4월 13일
I tried to implement a reverse 2D lookup table in Simulink according to the following description by Fangjun Jiang:
First, I wrote the following Matlab code for testing and it does exactly what I expect:
% Absolute eccentricity range
e = 0:1e-06:1.5e-05;
% Piston phase angle range
rad = 0:2*pi/400:2*pi;
% Load capacity matrix
load('Tragkraft_alpha_AK_kpl_V03.mat');
load_cap = Tragkraft_alpha_AK_kpl_V03;
% Instant piston phase angle
rad_dyn = 3.12588469032184;
% Reverse 2D lookup table for load capacity
lookup = interp2(e,rad,load_cap,e,rad_dyn);
% Instant load capacity
load_dyn = 659.331131439451;
% Instant absolute eccentricity
e_dyn = interp1(lookup,e,load_dyn);
After that, I tried to implement it in Simulink using a MATLAB function block with the following code:
function e_dyn = fcn(rad_dyn, load_dyn, load_cap)
% absolute eccentricity range
e = 0:1e-06:1.5e-05;
% piston phase angle range
rad = 0:2*pi/400:2*pi;
% 2D lookup table for load capacity
lookup = interp2(e,rad,load_cap,e,rad_dyn);
% instant absolute eccentricity
e_dyn=interp1(lookup,e,load_dyn);
However, this leads to the following error message that I don't really understand. In my understanding the Simulink model should do exactly the same as the Matlab code. Can anybody tell me how to fix that in Simulink?
  댓글 수: 3
VBBV
VBBV 2025년 4월 13일
이동: VBBV 2025년 4월 13일
That difference is probably due to Simulink block functionality which puts constraints on input data as opposed to Matlab coder. Moreover you can also use mixed array sizes for interp2 function as you want (scalar) for the purpose of reverse look up table . You can browse through the doc page of interp2 function output argument section for interpolated values (Vq).
Bruce
Bruce 2025년 4월 13일
편집: Bruce 2025년 4월 13일
Thanks, I just tried that, but it results in the same error message. I still don't understand why the code works in Matlab but not in Simulink.
... attached are the original files for testing (if required)

댓글을 달려면 로그인하십시오.

채택된 답변

Bruce
Bruce 2025년 4월 13일
I noticed that there was already an explanation of this issue in the link I mentioned above. The MATLAB function block in the Simulink model requires the following changes in order to work:
  • use of "griddata" instead of "interp2"
  • add "coder.extrinsic('griddata');" at the top of the Matlab function
  • e_dyn = 1; to Initialize the output at the top of the Matlab function
  • change e from a row vector to a column vector
Finally, the code inside the MATLAB function block looks as follows:
function e_dyn = fcn(rad_dyn, load_cap, load_dyn)
coder.extrinsic('griddata');
e_dyn = 1;
% absolute eccentricity range
e = (0:1e-06:1.5e-05)';
% piston phase angle range
rad = 0:2*pi/400:2*pi;
% 2D lookup table for load capacity
lookup = griddata(e,rad,load_cap,e,rad_dyn);
% instant absolute eccentricity
e_dyn=interp1(lookup,e,load_dyn);
Although I have absolutely no idea why this works, the result is the same as the output of the Matlab function, so it seems to make sense. Also, for whatever reason, the output calculation becomes extremely slow.
  댓글 수: 1
VBBV
VBBV 2025년 4월 13일
편집: VBBV 2025년 4월 13일
Ok. The key thing (line) is invoking the matlab built in function inside the Simulink function block although it was not recommended by @Fangjun Jiang
coder.extrinsic('griddata')
The rest of changes are not significant. You can try the same with interp2

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

VBBV
VBBV 2025년 4월 12일
@Bruce Xq, Yq must be vectors , in your one is scalar and other is vector.
e = 0:1e-6:1.5e-05
e = 1×16
1.0e-04 * 0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rad_dyn = 3.12588469032184
rad_dyn = 3.1259
% piston phase angle range
rad = 0:2*pi/400:2*pi
rad = 1×401
0 0.0157 0.0314 0.0471 0.0628 0.0785 0.0942 0.1100 0.1257 0.1414 0.1571 0.1728 0.1885 0.2042 0.2199 0.2356 0.2513 0.2670 0.2827 0.2985 0.3142 0.3299 0.3456 0.3613 0.3770 0.3927 0.4084 0.4241 0.4398 0.4555
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% piston phase angle range rad = 0:2*pi/400:2*pi;
load_cap = rand(401,16)
load_cap = 401×16
0.9390 0.7404 0.0375 0.4748 0.5578 0.6625 0.5913 0.8934 0.5613 0.6913 0.5698 0.7728 0.0915 0.6569 0.2616 0.7420 0.5898 0.3545 0.1735 0.8986 0.2358 0.3774 0.2710 0.3005 0.3594 0.7609 0.9310 0.2273 0.0780 0.8546 0.3428 0.1685 0.7407 0.6071 0.3289 0.3032 0.8605 0.3039 0.3185 0.0074 0.0197 0.2289 0.6588 0.1055 0.7994 0.7097 0.3390 0.9664 0.0773 0.7881 0.2613 0.0009 0.1449 0.3495 0.3655 0.7452 0.7094 0.7467 0.5933 0.4940 0.2385 0.9605 0.5026 0.7729 0.0445 0.4355 0.5632 0.1824 0.6726 0.5115 0.8234 0.6539 0.3724 0.1295 0.5974 0.7898 0.0420 0.1607 0.4936 0.3356 0.8658 0.5376 0.6603 0.7400 0.4651 0.4632 0.2891 0.1489 0.7257 0.9054 0.9649 0.8331 0.3219 0.9556 0.5288 0.6005 0.4665 0.5594 0.4608 0.6730 0.9842 0.8845 0.0180 0.4513 0.4014 0.4267 0.4705 0.8162 0.5023 0.2621 0.2733 0.6841 0.1051 0.8237 0.5663 0.0619 0.1383 0.9129 0.2330 0.1048 0.6653 0.6792 0.1665 0.9511 0.4487 0.1645 0.9818 0.9896 0.1134 0.5379 0.3903 0.0331 0.6306 0.6620 0.4031 0.7992 0.4596 0.3366 0.7462 0.0770 0.5829 0.2536 0.5996 0.1671 0.0019 0.2616 0.1290 0.7993 0.0818 0.5686 0.7396 0.6163 0.9185 0.4611 0.7098 0.9392 0.2135 0.0523 0.2622 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lookup = interp2(e,rad,load_cap,e,rad_dyn*ones(1,length(e)))
lookup = 1×16
0.5883 0.9483 0.0752 0.8895 0.3999 0.4260 0.4265 0.6782 0.3920 0.4980 0.5230 0.6050 0.0885 0.7243 0.5768 0.0679
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 1
Bruce
Bruce 2025년 4월 12일
Thanks for the answer, but then I do not understand why it does work in Matlab with essentially the same code. Futher, providing a scalar as one input is actually the intention of the reverse lookup table. It shall provide one breakpoint as an output based on the table data and a dynamic changing (scalar) breakpoint value as an input.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Lookup Tables에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by