LookupTableND
This block approximates a non-linear function using interpolation on data provided as an "n-dimensional table".
Library
Activate/LookupTables
Description
Data of the LookupTableND are n input vectors and table data. Each input vector is a row vector, table data is a also row vector, the results varying according to the order of the n input vectors. Table data is a combination of the data for a variation of the first input over a variation of the second input over a variation of the third input and so on.
For example, for 2 input vectors, the table data varies by all of values the input vector 1 for *only* the first value of input vectors 2. After this is exhausted, the next values in Table Data varies by incrementing to the second value of input vector 2 while also incrementing through all of the vales of input vector 1 at this second value. This continues until all of the values of the second input vector are completed. With additional input vectors, the same pattern persists to increment through the next input vector values while incrementing through the prior input vector values.
Input vector 1: [x1 x2 x3]
Input vector 2: [y1 y2]
Input vector 3: [z1 z2]
Table data: [x1y1z1, x2y1z1, x3y1z1, x1y2z1, x2y2z1, x3y2z1, x1y1z2, x2y1z2, x3y1z2, x1y2z2, x2y2z2, x3y2z2]
It may be easier in some cases to use script or function to convert from a different format. For example, OML also supports the 'ndgrid' and 'meshgrid' formats. If using an ndgrid you can convert to the LookupTable ND grid by using the reshape() OML command.
Here’s some code to show how this works:
if (input_vector_1_exists)
input_vector_1_length = length((input_vector_1);
end
if (input_vector_2_exists)
input_vector_2_length = length((input_vector_2);
end
if (input_vector_3_exists)
input_vector_3_length = length((input_vector_3);
end
% (…and so on)
LuT_data = […] % define according to ndgrid format
% convert to ndim vector for LookupTableND block
LookupTableND block_Table_Data = reshape(LuT_data,[1, (input_vector_1_length * input_vector_2_length * input_vector_3_length)]); % second term should be product of number of independent variable values
Parameters
Name | Label | Description | Data Type | Valid Values |
---|---|---|---|---|
nDims | Number of dimensions | Number of dimensions of the lookup table. One dimension corresponds to one input. | Number | |
dims | Input vectors | Structure | ||
dims/dimi | Input vector for each dimension | A vector indicating input break points for each dimension. | Cell of vectors | |
ff | Table data | A vector or an n (nDims) dimensional matrix containing data for n-dimensional table. | ||
Method | Interpolation method | Interpolation method. Default: Akima. | String | 'ZeroOrder(floor)' |
DoOutside | Result outside the table range | Action to be taken with the results outside of the table range. | String | 'Hold' |
derivative | Provide partial derivative vector | If selected, the block provides the partial derivative vector of the table with respect to each dimension. In this case, inputs should be row vector. The directional derivative output will be a matrix of row size nDim and the same column size as inputs. | Number | 0 |
externalActivation | External activation | Specifies whether the block receives an external activation or inherits its activation through its regular input ports. When External Activation is selected, an additional activation port is added to the block. By default, external activation is not selected. | Number | 0 |
Ports
Name | Type | Description | IO Type | Number |
---|---|---|---|---|
Port 1 | explicit | output | 1 | |
Port 2 | explicit | output | derivative | |
Port 3 | explicit | input | nDims | |
Port 4 | activation | input | externalActivation |
Example
The demo model LookupTableND.scm is available in demo browser in folder Activate/Basics/LookupTable.
Input vectors and table data are defined in model context:
% Define input range for each dimension
rU1 = [1, 2];
rU2 = [3, 4, 5];
rU3 = [6, 7, 8, 9];
% All possible values for z1
% All possible values for y1 (z1)
x1_y1_z1 = 136;
x2_y1_z1 = 236;
% All possible values for y2 (z1)
x1_y2_z1 = 146;
x2_y2_z1 = 246;
% All possible values for y3 (z1)
x1_y3_z1 = 156;
x2_y3_z1 = 256;
% All possible values for z2
% All possible values for y1 (z2)
x1_y1_z2 = 137;
x2_y1_z2 = 237;
% All possible values for y2 (z2)
x1_y2_z2 = 147;
x2_y2_z2 = 247;
% All possible values for y3 (z4)
x1_y3_z2 = 157;
x2_y3_z2 = 257;
% All possible values for z3
% All possible values for y1 (z3)
x1_y1_z3 = 138;
x2_y1_z3 = 238;
% All possible values for y2 (z3)
x1_y2_z3 = 148;
x2_y2_z3 = 248;
% All possible values for y3 (z4)
x1_y3_z3 = 158;
x2_y3_z3 = 258;
% All possible values for z4
% All possible values for y1 (z4)
x1_y1_z4 = 139;
x2_y1_z4 = 239;
% All possible values for y2 (z4)
x1_y2_z4 = 149;
x2_y2_z4 = 249;
% All possible values for y3 (z4)
x1_y3_z4 = 159;
x2_y3_z4 = 259;
% Create matrices for each Z values in the form:
% x1y1 x1y2 x1y3
% x2y1 x2y2 x2y3
v1 = [x1_y1_z1, x1_y2_z1 x1_y3_z1; x2_y1_z1, x2_y2_z1, x2_y3_z1];
v2 = [x1_y1_z2, x1_y2_z2 x1_y3_z2; x2_y1_z2, x2_y2_z2, x2_y3_z2];
v3 = [x1_y1_z3, x1_y2_z3 x1_y3_z3; x2_y1_z3, x2_y2_z3, x2_y3_z3];
v4 = [x1_y1_z4, x1_y2_z4 x1_y3_z4; x2_y1_z4, x2_y2_z4, x2_y3_z4];
% Reshape matrices into single-row vectors
V1 = reshape(v1, 1, []);
V2 = reshape(v2, 1, []);
V3 = reshape(v3, 1, []);
V4 = reshape(v4, 1, []);
% Merge all vectors into table data
TD = [V1, V2, V3, V4];
The second variant is to read the TableData from a text file:
file = fopen([fileparts(bdeGetCurrentModelFilePath), '/tableData.csv'], 'r');
values = freadtext(file, ',', 2);
fclose(file);
TDcsv = values(:, 4);