Writing S-Functions | ![]() ![]() |
The Direct-Index Lookup Table Algorithm
The 1-D lookup table block provided in the Simulink library uses interpolation or extrapolation when computing outputs. This extra accuracy is not needed in all situations. In this example, you will create a lookup table that directly indexes the output vector (y-data vector) based on the current input (x-data) point.
This direct 1-D lookup example computes an approximate solution, p(x), to a partially known function f(x) at x=x0, given data point pairs (x,y) in the form of an x data vector and a y data vector. For a given data pair (e.g., the i'th pair), y_i = f(x_i). It is assumed that the x-data values are monotonically increasing. If x0 is outside of the range of the x-data vector, then the first or last point will be returned.
The parameters to the S-function are
XData, YData, XEvenlySpaced
XData
and YData
are double vectors of equal length representing the values of the unknown function. XDataEvenlySpaced
is a scalar, 0.0 for false and 1.0 for true. If the XData
vector is evenly spaced, then more efficient code is generated.
The following graph illustrates how the parameters XData=[1:6]
, YData=[1,2,7,4,5,9
] are handled. For example, if the input (x-value) to the S-function block is 3, then the output (y-value) is 7.
Figure 8-3: Typical Output from a Lookup Table Example
![]() | S-Function RTWdata for Generating Code with Real-Time Workshop | The Direct-Index Lookup Table Example | ![]() |